From 1511b7b7154dcd6b08eef59d55a6642fa0aaaef7 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Thu, 26 Feb 2004 13:07:15 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1009 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/syscall_createcontext-v13.hc | 29 +++ util-vserver/lib/syscall_createcontext.c | 37 ++++ util-vserver/lib/syscall_getflags-v13.hc | 40 ++++ util-vserver/lib/syscall_getflags.c | 37 ++++ util-vserver/lib/syscall_migratecontext-v13.hc | 27 +++ util-vserver/lib/syscall_migratecontext.c | 37 ++++ util-vserver/lib/syscall_setflags-v13.hc | 38 ++++ util-vserver/lib/syscall_setflags.c | 37 ++++ util-vserver/lib_internal/util-cast.h | 33 +++ util-vserver/lib_internal/util-commonstrings.h | 32 +++ util-vserver/lib_internal/util-declarecmd.h | 27 +++ util-vserver/lib_internal/util-dimof.h | 24 ++ util-vserver/lib_internal/util-exitlikeprocess.c | 44 ++++ util-vserver/lib_internal/util-exitlikeprocess.h | 25 +++ util-vserver/lib_internal/util.h | 33 +++ util-vserver/src/vcontext.c | 271 +++++++++++++++++++++++ 16 files changed, 771 insertions(+) create mode 100644 util-vserver/lib/syscall_createcontext-v13.hc create mode 100644 util-vserver/lib/syscall_createcontext.c create mode 100644 util-vserver/lib/syscall_getflags-v13.hc create mode 100644 util-vserver/lib/syscall_getflags.c create mode 100644 util-vserver/lib/syscall_migratecontext-v13.hc create mode 100644 util-vserver/lib/syscall_migratecontext.c create mode 100644 util-vserver/lib/syscall_setflags-v13.hc create mode 100644 util-vserver/lib/syscall_setflags.c create mode 100644 util-vserver/lib_internal/util-cast.h create mode 100644 util-vserver/lib_internal/util-commonstrings.h create mode 100644 util-vserver/lib_internal/util-declarecmd.h create mode 100644 util-vserver/lib_internal/util-dimof.h create mode 100644 util-vserver/lib_internal/util-exitlikeprocess.c create mode 100644 util-vserver/lib_internal/util-exitlikeprocess.h create mode 100644 util-vserver/lib_internal/util.h create mode 100644 util-vserver/src/vcontext.c diff --git a/util-vserver/lib/syscall_createcontext-v13.hc b/util-vserver/lib/syscall_createcontext-v13.hc new file mode 100644 index 0000000..a4a634e --- /dev/null +++ b/util-vserver/lib/syscall_createcontext-v13.hc @@ -0,0 +1,29 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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); +} diff --git a/util-vserver/lib/syscall_createcontext.c b/util-vserver/lib/syscall_createcontext.c new file mode 100644 index 0000000..7a94017 --- /dev/null +++ b/util-vserver/lib/syscall_createcontext.c @@ -0,0 +1,37 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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 diff --git a/util-vserver/lib/syscall_getflags-v13.hc b/util-vserver/lib/syscall_getflags-v13.hc new file mode 100644 index 0000000..5d8be82 --- /dev/null +++ b/util-vserver/lib/syscall_getflags-v13.hc @@ -0,0 +1,40 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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; +} diff --git a/util-vserver/lib/syscall_getflags.c b/util-vserver/lib/syscall_getflags.c new file mode 100644 index 0000000..e52eb1a --- /dev/null +++ b/util-vserver/lib/syscall_getflags.c @@ -0,0 +1,37 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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 diff --git a/util-vserver/lib/syscall_migratecontext-v13.hc b/util-vserver/lib/syscall_migratecontext-v13.hc new file mode 100644 index 0000000..9c42625 --- /dev/null +++ b/util-vserver/lib/syscall_migratecontext-v13.hc @@ -0,0 +1,27 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +static inline ALWAYSINLINE int +vc_migrate_context_v13(xid_t xid) +{ + return vserver(VCMD_migrate_context, CTX_USER2KERNEL(xid), 0); +} diff --git a/util-vserver/lib/syscall_migratecontext.c b/util-vserver/lib/syscall_migratecontext.c new file mode 100644 index 0000000..796298d --- /dev/null +++ b/util-vserver/lib/syscall_migratecontext.c @@ -0,0 +1,37 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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 diff --git a/util-vserver/lib/syscall_setflags-v13.hc b/util-vserver/lib/syscall_setflags-v13.hc new file mode 100644 index 0000000..92a3a80 --- /dev/null +++ b/util-vserver/lib/syscall_setflags-v13.hc @@ -0,0 +1,38 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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); +} diff --git a/util-vserver/lib/syscall_setflags.c b/util-vserver/lib/syscall_setflags.c new file mode 100644 index 0000000..fab0ae8 --- /dev/null +++ b/util-vserver/lib/syscall_setflags.c @@ -0,0 +1,37 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#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 diff --git a/util-vserver/lib_internal/util-cast.h b/util-vserver/lib_internal/util-cast.h new file mode 100644 index 0000000..e9fa78d --- /dev/null +++ b/util-vserver/lib_internal/util-cast.h @@ -0,0 +1,33 @@ +// $Id$ --*- 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. + + +#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 +# define static_cast(X) static_cast +# define const_cast(X) const_cast +#endif + +#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_CAST_H diff --git a/util-vserver/lib_internal/util-commonstrings.h b/util-vserver/lib_internal/util-commonstrings.h new file mode 100644 index 0000000..d7053ab --- /dev/null +++ b/util-vserver/lib_internal/util-commonstrings.h @@ -0,0 +1,32 @@ +// $Id$ --*- 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. + + +#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 diff --git a/util-vserver/lib_internal/util-declarecmd.h b/util-vserver/lib_internal/util-declarecmd.h new file mode 100644 index 0000000..0c253a2 --- /dev/null +++ b/util-vserver/lib_internal/util-declarecmd.h @@ -0,0 +1,27 @@ +// $Id$ --*- 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. + + +#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 diff --git a/util-vserver/lib_internal/util-dimof.h b/util-vserver/lib_internal/util-dimof.h new file mode 100644 index 0000000..0fbe435 --- /dev/null +++ b/util-vserver/lib_internal/util-dimof.h @@ -0,0 +1,24 @@ +// $Id$ --*- 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. + + +#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 diff --git a/util-vserver/lib_internal/util-exitlikeprocess.c b/util-vserver/lib_internal/util-exitlikeprocess.c new file mode 100644 index 0000000..9aa80c4 --- /dev/null +++ b/util-vserver/lib_internal/util-exitlikeprocess.c @@ -0,0 +1,44 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include +#include + +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); + } +} diff --git a/util-vserver/lib_internal/util-exitlikeprocess.h b/util-vserver/lib_internal/util-exitlikeprocess.h new file mode 100644 index 0000000..746ae54 --- /dev/null +++ b/util-vserver/lib_internal/util-exitlikeprocess.h @@ -0,0 +1,25 @@ +// $Id$ --*- 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. + + +#ifndef H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H +#define H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H + +#include +void exitLikeProcess(pid_t pid) NORETURN; + +#endif // H_UTILVSERVER_LIB_INTERNAL_UTIL_EXITLIKEPROCESS_H diff --git a/util-vserver/lib_internal/util.h b/util-vserver/lib_internal/util.h new file mode 100644 index 0000000..116cc37 --- /dev/null +++ b/util-vserver/lib_internal/util.h @@ -0,0 +1,33 @@ +// $Id$ --*- 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. + + +#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 diff --git a/util-vserver/src/vcontext.c b/util-vserver/src/vcontext.c new file mode 100644 index 0000000..59d1464 --- /dev/null +++ b/util-vserver/src/vcontext.c @@ -0,0 +1,271 @@ +// $Id$ --*- 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. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "util.h" +#include "lib/internal.h" + +#include +#include +#include + +#define ENSC_WRAPPERS_UNISTD 1 +#define ENSC_WRAPPERS_VSERVER 1 +#define ENSC_WRAPPERS_FCNTL 1 +#include + +#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 ] * [--] *\n" + " --create [--migrate] [--xid ] * [--] *\n" + " --migrate --xid * [--] *\n" + "\n" + " can be:\n" + " --chroot ... chroot into current directory\n" + " --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; +} -- 1.8.1.5