--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+static inline ALWAYSINLINE int
+vc_create_context_v13(xid_t xid)
+{
+ xid_t res = vserver(VCMD_create_context, CTX_USER2KERNEL(xid), 0);
+
+ return CTX_KERNEL2USER(res);
+}
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "vserver.h"
+#include "vserver-internal.h"
+#include "linuxvirtual.h"
+
+#if defined(VC_ENABLE_API_V13)
+# include "syscall_createcontext-v13.hc"
+#endif
+
+#if defined(VC_ENABLE_API_V13)
+xid_t
+vc_create_context(xid_t xid)
+{
+ CALL_VC(CALL_VC_V13A(vc_create_context, xid));
+}
+#endif
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+static inline ALWAYSINLINE int
+vc_get_flags_v13(xid_t xid, struct vc_ctx_flags *flags)
+
+{
+ struct vcmd_ctx_flags_v0 k_flags;
+ int res;
+
+ if (flags==0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ res = vserver(VCMD_get_flags, CTX_USER2KERNEL(xid), &k_flags);
+ flags->flagword = k_flags.flagword;
+ flags->mask = k_flags.mask;
+
+ return res;
+}
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "vserver.h"
+#include "vserver-internal.h"
+#include "linuxvirtual.h"
+
+#if defined(VC_ENABLE_API_V13)
+# include "syscall_getflags-v13.hc"
+#endif
+
+#if defined(VC_ENABLE_API_V13)
+int
+vc_get_flags(xid_t xid, struct vc_ctx_flags *flags)
+{
+ CALL_VC(CALL_VC_V13A(vc_get_flags, xid, flags));
+}
+#endif
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+static inline ALWAYSINLINE int
+vc_migrate_context_v13(xid_t xid)
+{
+ return vserver(VCMD_migrate_context, CTX_USER2KERNEL(xid), 0);
+}
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "vserver.h"
+#include "vserver-internal.h"
+#include "linuxvirtual.h"
+
+#if defined(VC_ENABLE_API_V13)
+# include "syscall_migratecontext-v13.hc"
+#endif
+
+#if defined(VC_ENABLE_API_V13)
+int
+vc_migrate_context(xid_t xid)
+{
+ CALL_VC(CALL_VC_V13A(vc_migrate_context, xid));
+}
+#endif
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+static inline ALWAYSINLINE int
+vc_set_flags_v13(xid_t xid, struct vc_ctx_flags const *flags)
+
+{
+ struct vcmd_ctx_flags_v0 k_flags;
+
+ if (flags==0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ k_flags.flagword = flags->flagword;
+ k_flags.mask = flags->mask;
+
+ return vserver(VCMD_set_flags, CTX_USER2KERNEL(xid), &k_flags);
+}
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "vserver.h"
+#include "vserver-internal.h"
+#include "linuxvirtual.h"
+
+#if defined(VC_ENABLE_API_V13)
+# include "syscall_setflags-v13.hc"
+#endif
+
+#if defined(VC_ENABLE_API_V13)
+int
+vc_set_flags(xid_t xid, struct vc_ctx_flags const *flags)
+{
+ CALL_VC(CALL_VC_V13A(vc_set_flags, xid, flags));
+}
+#endif
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_CAST_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_CAST_H
+
+#ifndef __cplusplus
+# define cAsT_(X) (X))
+# define reinterpret_cast(X) ((X) cAsT_
+# define static_cast(X) ((X) cAsT_
+# define const_cast(X) ((X) cAsT_
+#else /* __cplusplus */
+# define reinterpret_cast(X) reinterpret_cast<X>
+# define static_cast(X) static_cast<X>
+# define const_cast(X) const_cast<X>
+#endif
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_CAST_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_COMMONSTRINGS_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_COMMONSTRINGS_H
+
+#ifndef PACKAGE_BUGREPORT
+# define PACKAGE_BUGREPORT "???"
+#endif
+
+#ifndef VERSION_COPYRIGHT_DISCLAIMER
+# define VERSION_COPYRIGHT_DISCLAIMER \
+ "This program is free software; you may redistribute it under the terms of\n" \
+ "the GNU General Public License. This program has absolutely no warranty.\n"
+#endif
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_COMMONSTRINGS_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_DECLARECMD_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_DECLARECMD_H
+
+#define VSERVER_DECLARE_CMD(CMD) \
+ char buf[strlen(CMD)+1]; \
+ memcpy(buf, (CMD), strlen(CMD)+1); \
+ CMD = basename(buf);
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_DECLARECMD_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_DIMOF_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_DIMOF_H
+
+#define DIM_OF(X) (sizeof(X)/sizeof((X)[0]))
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_DIMOF_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <signal.h>
+
+void
+exitLikeProcess(int pid)
+{
+ int status;
+
+ if (wait4(pid, &status, 0,0)==-1) return;
+
+ if (WIFEXITED(status))
+ exit(WEXITSTATUS(status));
+
+ if (WIFSIGNALED(status)) {
+ kill(getpid(), WTERMSIG(status));
+ exit(1);
+ }
+}
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H
+
+#include <sys/types.h>
+void exitLikeProcess(pid_t pid) NORETURN;
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_H
+#define H_UTILVSERVER_LIB_INTERNAL_UTIL_H
+
+#include "util-cast.h"
+#include "util-commonstrings.h"
+#include "util-declarecmd.h"
+#include "util-dimof.h"
+#include "util-dotfile.h"
+#include "util-exitlikeprocess.h"
+#include "util-io.h"
+#include "util-mem.h"
+#include "util-perror.h"
+#include "util-safechdir.h"
+
+#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_H
--- /dev/null
+// $Id$ --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "util.h"
+#include "lib/internal.h"
+
+#include <vserver.h>
+#include <getopt.h>
+#include <fcntl.h>
+
+#define ENSC_WRAPPERS_UNISTD 1
+#define ENSC_WRAPPERS_VSERVER 1
+#define ENSC_WRAPPERS_FCNTL 1
+#include <wrappers.h>
+
+#define CMD_HELP 0x1000
+#define CMD_VERSION 0x1001
+#define CMD_XID 0x4000
+#define CMD_CREATE 0x4001
+#define CMD_MIGRATE 0x4003
+#define CMD_FAKEINIT 0x4002
+#define CMD_DISCONNECT 0x4004
+#define CMD_UID 0x4005
+#define CMD_CHROOT 0x4006
+#define CMD_SILENT 0x4007
+
+struct option const
+CMDLINE_OPTIONS[] = {
+ { "help", no_argument, 0, CMD_HELP },
+ { "version", no_argument, 0, CMD_VERSION },
+ { "ctx", required_argument, 0, CMD_XID },
+ { "xid", required_argument, 0, CMD_XID },
+ { "create", no_argument, 0, CMD_CREATE },
+ { "migrate", no_argument, 0, CMD_MIGRATE },
+ { "fakeinit", no_argument, 0, CMD_FAKEINIT },
+ { "disconnect", no_argument, 0, CMD_DISCONNECT },
+ { "silent", no_argument, 0, CMD_SILENT },
+ { "uid", no_argument, 0, CMD_UID },
+ { "chroot", no_argument, 0, CMD_CHROOT },
+ { 0,0,0,0 },
+};
+
+struct Arguments {
+ bool do_create;
+ bool do_migrate;
+ bool do_disconnect;
+ bool is_fakeinit;
+ int verbosity;
+ bool do_chroot;
+ uid_t uid;
+ xid_t xid;
+};
+
+int wrapper_exit_code = 255;
+
+static void
+showHelp(int fd, char const *cmd, int res)
+{
+ WRITE_MSG(fd, "Usage: ");
+ WRITE_STR(fd, cmd);
+ WRITE_MSG(fd,
+ " [--xid <xid>] <opts>* [--] <program> <args>*\n"
+ " --create [--migrate] [--xid <xid>] <opts>* [--] <program> <args>*\n"
+ " --migrate --xid <xid> <opts>* [--] <program> <args>*\n"
+ "\n"
+ "<opts> can be:\n"
+ " --chroot ... chroot into current directory\n"
+ " --uid <uid> ... change uid\n"
+ " --fakeinit ... set current process as general process reaper\n"
+ " for ctx (possible for --migrate only)\n"
+ " --silent ... be silent\n"
+ "\n"
+ "Please report bugs to " PACKAGE_BUGREPORT "\n");
+
+ exit(res);
+}
+
+static void
+showVersion()
+{
+ WRITE_MSG(1,
+ "vcontext " VERSION " -- manages the creation of security contexts\n"
+ "This program is part of " PACKAGE_STRING "\n\n"
+ "Copyright (C) 2003,2004 Enrico Scholz\n"
+ VERSION_COPYRIGHT_DISCLAIMER);
+ exit(0);
+}
+
+static inline ALWAYSINLINE int
+initSync(int p[2], bool do_disconnect)
+{
+ if (!do_disconnect) return 0;
+
+ Epipe(p);
+ fcntl(p[1], F_SETFD, FD_CLOEXEC);
+ return Efork();
+}
+
+static inline ALWAYSINLINE void
+doSyncStage1(int p[2], bool do_disconnect)
+{
+ int fd;
+
+ if (!do_disconnect) return;
+
+ fd = Eopen("/dev/null", O_RDONLY, 0);
+ Esetsid();
+ Edup2(fd, 0);
+ Eclose(p[0]);
+ if (fd!=0) Eclose(fd);
+ Ewrite(p[1], ".", 1);
+}
+
+static inline ALWAYSINLINE void
+doSyncStage2(int p[2], bool do_disconnect)
+{
+ if (!do_disconnect) return;
+
+ Ewrite(p[1], "X", 1);
+}
+
+static void
+waitOnSync(pid_t pid, int p[2])
+{
+ int c;
+ size_t l;
+
+ Eclose(p[1]);
+ l = Eread(p[0], &c, 1);
+ if (l!=1) exitLikeProcess(pid);
+ l = Eread(p[0], &c, 1);
+ if (l!=0) exitLikeProcess(pid);
+}
+
+static inline ALWAYSINLINE void
+tellContext(xid_t ctx, bool do_it)
+{
+ char buf[sizeof(xid_t)*3+2];
+ size_t l;
+
+ if (!do_it) return;
+
+ l = utilvserver_fmt_long(buf,ctx);
+
+ WRITE_MSG(2, "New security context is ");
+ write(2, buf, l);
+ WRITE_MSG(2, "\n");
+}
+
+int main (int argc, char *argv[])
+{
+ pid_t pid;
+ int p[2];
+ struct Arguments args = {
+ .do_create = false,
+ .do_migrate = false,
+ .do_disconnect = false,
+ .is_fakeinit = false,
+ .verbosity = 1,
+ .uid = -1,
+ .xid = VC_DYNAMIC_XID,
+ };
+
+ while (1) {
+ int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
+ if (c==-1) break;
+
+ switch (c) {
+ case CMD_HELP : showHelp(1, argv[0], 0);
+ case CMD_VERSION : showVersion();
+ case CMD_CREATE : args.do_create = true; break;
+ case CMD_MIGRATE : args.do_migrate = true; break;
+ case CMD_DISCONNECT : args.do_disconnect = true; break;
+ case CMD_FAKEINIT : args.is_fakeinit = true; break;
+ case CMD_CHROOT : args.do_chroot = true; break;
+ case CMD_SILENT : --args.verbosity; break;
+ case CMD_XID : args.xid = atol(optarg); break;
+ case CMD_UID : args.uid = atol(optarg); break;
+
+ default :
+ WRITE_MSG(2, "Try '");
+ WRITE_STR(2, argv[0]);
+ WRITE_MSG(2, " --help\" for more information.\n");
+ return 255;
+ break;
+ }
+ }
+
+ if (optind>=argc) {
+ WRITE_MSG(2, "No command given; use '--help' for more information.\n");
+ exit(255);
+ }
+
+ if (!args.do_create && !args.do_migrate)
+ args.do_create = args.do_migrate = true;
+
+ if (!args.do_migrate && args.is_fakeinit) {
+ WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
+ exit(255);
+ }
+
+ if (!args.do_create && args.xid==VC_DYNAMIC_XID) {
+ WRITE_MSG(2, "vcontext: Can not migrate to an unknown context\n");
+ exit(255);
+ }
+
+ pid = initSync(p, args.do_disconnect);
+ if (pid==0) {
+ xid_t xid;
+ if (args.do_create) {
+ xid = Evc_create_context(args.xid);
+ tellContext(xid, args.verbosity>=1);
+ }
+ else
+ xid = args.xid;
+
+ if (args.do_chroot)
+ Echroot(".");
+
+ if (args.uid!=(uid_t)(-1) && getuid()!=args.uid) {
+ Esetuid(args.uid);
+ if (getuid()!=args.uid) {
+ WRITE_MSG(2, "vcontext: Something went wrong while changing the UID\n");
+ exit(255);
+ }
+ }
+
+ if (args.is_fakeinit) {
+ struct vc_ctx_flags flags;
+ Evc_get_flags(xid, &flags);
+ #warning !!! IMPLEMENT ME !!!
+ //if (flags.mask
+ if (0) {
+ WRITE_MSG(2, "vcontext: context has already a fakeinit-process\n");
+ exit(255);
+ }
+ }
+
+ if (args.do_migrate)
+ Evc_migrate_context(xid);
+
+ doSyncStage1(p, args.do_disconnect);
+ execvp (argv[optind],argv+optind);
+ doSyncStage2(p, args.do_disconnect);
+
+ PERROR_Q("chcontext: execvp", argv[optind]);
+ exit(255);
+ }
+
+ waitOnSync(pid, p);
+ return EXIT_SUCCESS;
+}