X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=secnet.git;a=blobdiff_plain;f=hackypar.c;h=a93b3eef1b1f0389d0040225e4a15a384e3bb256;hp=5c5cd3d013e2022229a7ac3c8a8acaa81f3e5208;hb=ce53e0ea9aad729511e8b315dcbed7122272c2a1;hpb=1caa23ff879cec7f8f36b32a987f0610291ef177 diff --git a/hackypar.c b/hackypar.c index 5c5cd3d..a93b3ee 100644 --- a/hackypar.c +++ b/hackypar.c @@ -1,4 +1,22 @@ -/* Hacky parallelism; Ian Jackson */ +/* 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 d 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 @@ -24,90 +42,96 @@ 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; +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 (!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); - } + 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); +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; - } + 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 */ - return hp_compute; - } + if (!child) { /* we are the child */ + afterfork(); + return hp_compute; + } - Message(M_INFO,"hacky_par: started, punting\n"); - return hp_deferring; + 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_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; +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, uint32_t timeout, - bool_t (*send_msg)(struct site *st), struct site *st) { - int i; + 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