File: hackypar.c

package info (click to toggle)
secnet 0.6.8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,956 kB
  • sloc: ansic: 15,234; python: 1,057; perl: 966; sh: 596; tcl: 484; java: 231; asm: 114; yacc: 89; php: 64; makefile: 48; awk: 40
file content (145 lines) | stat: -rw-r--r-- 3,190 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Hacky parallelism */
/*
 * This file is part of secnet.
 * See README for full list of copyright holders.
 *
 * secnet is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * secnet is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * version 3 along with secnet; if not, see
 * https://www.gnu.org/licenses/gpl.html.
 */

#define _GNU_SOURCE

#include "secnet.h"
#include "util.h"
#include "hackypar.h"

#ifdef HACKY_PARALLEL

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <sys/wait.h>

#define HASHSIZE 16
#define CACHESIZE 16

typedef enum { hp_idle, hp_compute, hp_deferring, hp_fail } HPState;

static HPState state;
static pid_t child;

static void checkchild(void)
{
    int r, status;
  
    if (!child) return;

    r= waitpid(child,&status,WNOHANG); if (!r) return;
    if (r==-1) {
	Message(M_ERR,"hacky_par: waitpid: %s\n",strerror(errno));
	return;
    }
    child= 0;
  
    if (WIFSIGNALED(status)) {
	Message(M_ERR,"hacky_par: signaled! %s\n",strsignal(WTERMSIG(status)));
    } else if (!WIFEXITED(status)) {
	Message(M_ERR,"hacky_par: unexpected status! %d\n", r);
    }
}

static HPState start(void)
{
    assert(!child);

    child= fork();
    if (child == -1) {
	Message(M_ERR,"hacky_par: fork failed: %s\n",strerror(errno));
	return hp_fail;
    }

    if (!child) { /* we are the child */
	afterfork();
	return hp_compute;
    }

    Message(M_INFO,"hacky_par: started, punting\n");
    return hp_deferring;
}

int hacky_par_start_failnow(void)
{
    state= hp_idle;
    checkchild();
    if (child) {
	state= hp_deferring;
	Message(M_INFO,"hacky_par: busy, punting\n");
	return 1;
    }
    return 0;
}

int hacky_par_mid_failnow(void)
{
    state= start();
    return state != hp_compute;
}

bool_t (*packy_par_gen)(struct site *st);

void hacky_par_end(int *ok,
		   int32_t retries, int32_t timeout,
		   bool_t (*send_msg)(struct site *st), struct site *st)
{
    int i;
  
    switch (state) {
    case hp_deferring:
	assert(!*ok);
	*ok= 1;
	return;
    case hp_fail:
	assert(!*ok);
	return;
    case hp_idle:
	return;
    case hp_compute:
	if (!ok) {
	    Message(M_ERR,"hacky_par: compute failed\n");
	    _exit(2);
	}
	Message(M_INFO,"hacky_par: got result, sending\n");
	for (i=1; i<retries; i++) {
	    sleep((timeout + 999)/1000);
	    if (!send_msg(st)) {
		Message(M_ERR,"hacky_par: retry failed\n");
		_exit(1);
	    }
	}
	_exit(0);
    }
}

#else /*!HACKY_PARALLEL*/

int hacky_par_start_failnow(void) { return 0; }
int hacky_par_mid_failnow(void) { return 0; }
void hacky_par_end(int *ok,
		   int32_t retries, int32_t timeout,
		   bool_t (*send_msg)(struct site *st), struct site *st) { }

#endif /*HACKY_PARALLEL...else*/