1 // $Id: context-sync.hc 2015 2005-04-10 01:01:33Z ensc $ --*- c -*--
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 /// \args p[0] used for parent -> child sync (child waits till parent is in deathrow)
20 /// \args p[1] used for child -> parent sync (recognize execv() errors)
21 static inline ALWAYSINLINE int
22 initSync(int p[2][2], bool do_disconnect)
24 if (!do_disconnect) return 0;
28 fcntl(p[1][1], F_SETFD, FD_CLOEXEC);
32 static inline ALWAYSINLINE void
33 doSyncStage0(int p[2][2], bool do_disconnect)
36 if (!do_disconnect) return;
39 Eread (p[0][0], &c, 1);
43 static inline ALWAYSINLINE void
44 doSyncStage1(int p[2][2], bool do_disconnect)
48 if (!do_disconnect) return;
50 fd = EopenD("/dev/null", O_RDONLY|O_NONBLOCK, 0);
51 (void)setsid(); // ignore error when we are already a session-leader
54 if (fd!=0) Eclose(fd);
55 Ewrite(p[1][1], ".", 1);
58 static inline ALWAYSINLINE void
59 doSyncStage2(int p[2][2], bool do_disconnect)
61 if (!do_disconnect) return;
63 Ewrite(p[1][1], "X", 1);
66 /// \args p[0] used for parent -> child sync (child waits till parent is in deathrow)
67 /// \args p[1] used for child -> parent sync (recognize execv() errors)
69 waitOnSync(pid_t pid, int p[2][2], bool is_prevent_race)
74 if (is_prevent_race &&
75 !jailIntoTempDir(0)) {
76 perror(ENSC_WRAPPERS_PREFIX "jailIntoTempDir()");
81 Ewrite(p[0][1], "X", 1);
85 l = Eread(p[1][0], &c, 1);
86 if (l!=1) exitLikeProcess(pid,0, wrapper_exit_code);
87 l = Eread(p[1][0], &c, 1);
88 if (l!=0) exitLikeProcess(pid,0, wrapper_exit_code);