00001 /* 00002 Synchronize processes with signals 00003 AUP2, Sec. 6.02, 9.02.3 (not in book) 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include "pcsync_sig.h" 00023 00024 #define NO_OPx 00025 00026 /* 00027 In some systems, a signal sent to a child between the time that fork() returns to the parent and the time that the child starts executing may be lost. (It is pending when it is sent, and then pending signals are cleared.) This bug is so common that it is discussed in the POSIX rationale for fork(). 00028 00029 I can't think of a fix for this using only signals. But, see pcsync.c for a correct and efficient implementation using pipes, with no signals at all. 00030 00031 (Note: One might think that the answer is for the child to signal the parent when is is fully baked, with the parent waiting for that signal before signalling the child. But this method works only for a single child. It doesn't scale up for multiple children created all at once.) 00032 */ 00033 00034 static void signal_handler_child(int signo) 00035 { 00036 } 00037 00038 bool pcsync_init(struct pcsync *p) 00039 { 00040 #ifndef NO_OP 00041 sigset_t set; 00042 00043 (void)sigemptyset(&set); 00044 (void)sigaddset(&set, SIGUSR1); 00045 ec_neg1( sigprocmask(SIG_BLOCK, &set, &p->old_set) ) 00046 #endif /* NO_OP */ 00047 return true; 00048 00049 EC_CLEANUP_BGN 00050 return false; 00051 EC_CLEANUP_END 00052 } 00053 00054 bool pcsync_wait_for_parent(struct pcsync *p) 00055 { 00056 #ifndef NO_OP 00057 struct sigaction act; 00058 int r; 00059 sigset_t suspendset; 00060 00061 memset(&act, 0, sizeof(act)); 00062 act.sa_handler = signal_handler_child; 00063 ec_neg1( sigaction(SIGUSR1, &act, NULL) ) 00064 sigfillset(&suspendset); 00065 sigdelset(&suspendset, SIGUSR1); 00066 r = sigsuspend(&suspendset); 00067 if (r == -1 && errno != EINTR) 00068 EC_FAIL 00069 ec_neg1( sigprocmask(SIG_SETMASK, &p->old_set, NULL) ) 00070 #endif /* NO_OP */ 00071 return true; 00072 00073 EC_CLEANUP_BGN 00074 return false; 00075 EC_CLEANUP_END 00076 } 00077 00078 bool pcsync_unblock_children(struct pcsync *p, int children, 00079 pid_t *pid_child) 00080 { 00081 #ifndef NO_OP 00082 int i; 00083 00084 ec_neg1( sigprocmask(SIG_SETMASK, &p->old_set, NULL) ) 00085 for (i = 0; i < children; i++) 00086 ec_neg1( kill(pid_child[i], SIGUSR1) ) 00087 #endif /* NO_OP */ 00088 return true; 00089 00090 EC_CLEANUP_BGN 00091 return false; 00092 EC_CLEANUP_END 00093 } 00094 00095 bool pcsync_end(struct pcsync *p) 00096 { 00097 return true; 00098 } 00099