X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fcontext-sync.hc;fp=src%2Fcontext-sync.hc;h=6f786d5de064690c21c0fd226e548c448a77e852;hb=8cf13bb177d92c93eb73dc8939777150536c2d00;hp=0000000000000000000000000000000000000000;hpb=6bf3f95de36c804c97716b2d0bdf10680c559044;p=util-vserver.git diff --git a/src/context-sync.hc b/src/context-sync.hc new file mode 100644 index 0000000..6f786d5 --- /dev/null +++ b/src/context-sync.hc @@ -0,0 +1,89 @@ +// $Id: context-sync.hc,v 1.5 2005/04/10 01:01:33 ensc Exp $ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +/// \args p[0] used for parent -> child sync (child waits till parent is in deathrow) +/// \args p[1] used for child -> parent sync (recognize execv() errors) +static inline ALWAYSINLINE int +initSync(int p[2][2], bool do_disconnect) +{ + if (!do_disconnect) return 0; + + Epipe(p[0]); + Epipe(p[1]); + fcntl(p[1][1], F_SETFD, FD_CLOEXEC); + return Efork(); +} + +static inline ALWAYSINLINE void +doSyncStage0(int p[2][2], bool do_disconnect) +{ + char c; + if (!do_disconnect) return; + + Eclose(p[0][1]); + Eread (p[0][0], &c, 1); + Eclose(p[0][0]); +} + +static inline ALWAYSINLINE void +doSyncStage1(int p[2][2], bool do_disconnect) +{ + int fd; + + if (!do_disconnect) return; + + fd = EopenD("/dev/null", O_RDONLY|O_NONBLOCK, 0); + (void)setsid(); // ignore error when we are already a session-leader + Edup2(fd, 0); + Eclose(p[1][0]); + if (fd!=0) Eclose(fd); + Ewrite(p[1][1], ".", 1); +} + +static inline ALWAYSINLINE void +doSyncStage2(int p[2][2], bool do_disconnect) +{ + if (!do_disconnect) return; + + Ewrite(p[1][1], "X", 1); +} + +/// \args p[0] used for parent -> child sync (child waits till parent is in deathrow) +/// \args p[1] used for child -> parent sync (recognize execv() errors) +static void +waitOnSync(pid_t pid, int p[2][2], bool is_prevent_race) +{ + char c; + size_t l; + + if (is_prevent_race && + !jailIntoTempDir(0)) { + perror(ENSC_WRAPPERS_PREFIX "jailIntoTempDir()"); + exit(255); + } + + Eclose(p[0][0]); + Ewrite(p[0][1], "X", 1); + Eclose(p[0][1]); + + Eclose(p[1][1]); + l = Eread(p[1][0], &c, 1); + if (l!=1) exitLikeProcess(pid,0, wrapper_exit_code); + l = Eread(p[1][0], &c, 1); + if (l!=0) exitLikeProcess(pid,0, wrapper_exit_code); +}