summaryrefslogtreecommitdiff
path: root/privileged_startx
diff options
context:
space:
mode:
Diffstat (limited to 'privileged_startx')
-rwxr-xr-xprivileged_startx/10-tmpdirs.cpp37
-rwxr-xr-xprivileged_startx/20-font_cache.cpp35
-rw-r--r--privileged_startx/Makefile.am64
-rw-r--r--privileged_startx/client.c56
-rw-r--r--privileged_startx/org.x.privileged_startx.plist.cpp21
-rw-r--r--privileged_startx/privileged_startx.c59
-rw-r--r--privileged_startx/privileged_startx.defs40
-rw-r--r--privileged_startx/privileged_startx_types.h6
-rw-r--r--privileged_startx/server.c250
9 files changed, 568 insertions, 0 deletions
diff --git a/privileged_startx/10-tmpdirs.cpp b/privileged_startx/10-tmpdirs.cpp
new file mode 100755
index 0000000..dd5071c
--- /dev/null
+++ b/privileged_startx/10-tmpdirs.cpp
@@ -0,0 +1,37 @@
+XCOMM!/bin/sh
+XCOMM Copyright (c) 2008 Apple Inc.
+XCOMM
+XCOMM Permission is hereby granted, free of charge, to any person
+XCOMM obtaining a copy of this software and associated documentation files
+XCOMM (the "Software"), to deal in the Software without restriction,
+XCOMM including without limitation the rights to use, copy, modify, merge,
+XCOMM publish, distribute, sublicense, and/or sell copies of the Software,
+XCOMM and to permit persons to whom the Software is furnished to do so,
+XCOMM subject to the following conditions:
+XCOMM
+XCOMM The above copyright notice and this permission notice shall be
+XCOMM included in all copies or substantial portions of the Software.
+XCOMM
+XCOMM THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+XCOMM EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+XCOMM MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+XCOMM NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+XCOMM HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+XCOMM WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+XCOMM OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+XCOMM DEALINGS IN THE SOFTWARE.
+XCOMM
+XCOMM Except as contained in this notice, the name(s) of the above
+XCOMM copyright holders shall not be used in advertising or otherwise to
+XCOMM promote the sale, use or other dealings in this Software without
+XCOMM prior written authorization.
+
+XCOMM Make sure these are owned by root
+for dir in /tmp/.ICE-unix /tmp/.X11-unix /tmp/.font-unix ; do
+ # Use mktemp rather than mkdir to avoid possible security issue
+ # if $dir exists and is a symlink
+ if mktemp -d ${dir} > /dev/null ; then
+ chmod 1777 $dir
+ chown root:wheel $dir
+ fi
+done
diff --git a/privileged_startx/20-font_cache.cpp b/privileged_startx/20-font_cache.cpp
new file mode 100755
index 0000000..c13384b
--- /dev/null
+++ b/privileged_startx/20-font_cache.cpp
@@ -0,0 +1,35 @@
+XCOMM!/bin/sh
+XCOMM Copyright (c) 2008 Apple Inc.
+XCOMM
+XCOMM Permission is hereby granted, free of charge, to any person
+XCOMM obtaining a copy of this software and associated documentation files
+XCOMM (the "Software"), to deal in the Software without restriction,
+XCOMM including without limitation the rights to use, copy, modify, merge,
+XCOMM publish, distribute, sublicense, and/or sell copies of the Software,
+XCOMM and to permit persons to whom the Software is furnished to do so,
+XCOMM subject to the following conditions:
+XCOMM
+XCOMM The above copyright notice and this permission notice shall be
+XCOMM included in all copies or substantial portions of the Software.
+XCOMM
+XCOMM THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+XCOMM EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+XCOMM MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+XCOMM NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+XCOMM HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+XCOMM WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+XCOMM OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+XCOMM DEALINGS IN THE SOFTWARE.
+XCOMM
+XCOMM Except as contained in this notice, the name(s) of the above
+XCOMM copyright holders shall not be used in advertising or otherwise to
+XCOMM promote the sale, use or other dealings in this Software without
+XCOMM prior written authorization.
+
+if [ -x BINDIR/font_cache ] ; then
+ BINDIR/font_cache &
+elif [ -x BINDIR/font_cache.sh ] ; then
+ BINDIR/font_cache.sh -s &
+elif [ -x /usr/X11/bin/fc-cache ] ; then
+ BINDIR/fc-cache &
+fi
diff --git a/privileged_startx/Makefile.am b/privileged_startx/Makefile.am
new file mode 100644
index 0000000..0a3956d
--- /dev/null
+++ b/privileged_startx/Makefile.am
@@ -0,0 +1,64 @@
+# Copyright (c) 2008 Apple Inc.
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation files
+# (the "Software"), to deal in the Software without restriction,
+# including without limitation the rights to use, copy, modify, merge,
+# publish, distribute, sublicense, and/or sell copies of the Software,
+# and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+# HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+#
+# Except as contained in this notice, the name(s) of the above
+# copyright holders shall not be used in advertising or otherwise to
+# promote the sale, use or other dealings in this Software without
+# prior written authorization.
+
+include $(top_srcdir)/cpprules.in
+
+xinitrcdir = $(libdir)/X11/xinit
+privstartxdir = $(xinitrcdir)/privileged_startx.d
+
+xinitrc_PROGRAMS = privileged_startx
+privstartx_SCRIPTS = 10-tmpdirs 20-font_cache
+
+AM_CPPFLAGS = -DXINITDIR=\"$(xinitrcdir)\" -DSCRIPTDIR=\"$(privstartxdir)\" -DBINDIR=\"$(bindir)\"
+
+CPP_FILES_FLAGS = -DXINITDIR="$(xinitrcdir)" -DSCRIPTDIR="$(privstartxdir)" -DBINDIR="$(bindir)"
+
+privileged_startx_SOURCES = \
+ privileged_startxServer.c \
+ privileged_startxUser.c \
+ server.c \
+ client.c \
+ privileged_startx.c
+
+BUILT_SOURCES = \
+ privileged_startxServer.c \
+ privileged_startxUser.c \
+ privileged_startxServer.h \
+ privileged_startx.h
+
+launchdaemons_PRE = org.x.privileged_startx.plist.cpp
+launchdaemons_DATA = $(launchdaemons_PRE:plist.cpp=plist)
+
+10-tmpdirs: 10-tmpdirs.cpp Makefile
+20-font_cache: 20-font_cache.cpp Makefile
+
+CLEANFILES = $(privstartx_SCRIPTS) $(BUILT_SOURCES) $(launchdaemons_DATA)
+
+EXTRA_DIST = 10-tmpdirs.cpp 20-font_cache.cpp org.x.privileged_startx.plist.cpp privileged_startx.defs
+
+$(BUILT_SOURCES): privileged_startx.defs
+ mig -sheader privileged_startxServer.h privileged_startx.defs
diff --git a/privileged_startx/client.c b/privileged_startx/client.c
new file mode 100644
index 0000000..2a24a70
--- /dev/null
+++ b/privileged_startx/client.c
@@ -0,0 +1,56 @@
+/* Copyright (c) 2008 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#include <mach/mach.h>
+#include <mach/mach_error.h>
+#include <servers/bootstrap.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "privileged_startx.h"
+
+int client_main(void) {
+ kern_return_t kr;
+ mach_port_t mp;
+
+ kr = bootstrap_look_up(bootstrap_port, BOOTSTRAP_NAME, &mp);
+ if (kr != KERN_SUCCESS) {
+ fprintf(stderr, "bootstrap_look_up(): %s\n", bootstrap_strerror(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ kr = privileged_startx(mp);
+ if (kr != KERN_SUCCESS) {
+ fprintf(stderr, "privileged_startx client: %s\n", mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ exit(EXIT_SUCCESS);
+}
diff --git a/privileged_startx/org.x.privileged_startx.plist.cpp b/privileged_startx/org.x.privileged_startx.plist.cpp
new file mode 100644
index 0000000..e878dc3
--- /dev/null
+++ b/privileged_startx/org.x.privileged_startx.plist.cpp
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>Label</key>
+ <string>org.x.privileged_startx</string>
+ <key>MachServices</key>
+ <dict>
+ <key>org.x.privileged_startx</key>
+ <true/>
+ </dict>
+ <key>ProgramArguments</key>
+ <array>
+ <string>XINITDIR/privileged_startx</string>
+ <string>-d</string>
+ <string>SCRIPTDIR</string>
+ </array>
+ <key>TimeOut</key>
+ <integer>60</integer>
+</dict>
+</plist>
diff --git a/privileged_startx/privileged_startx.c b/privileged_startx/privileged_startx.c
new file mode 100644
index 0000000..725a1b0
--- /dev/null
+++ b/privileged_startx/privileged_startx.c
@@ -0,0 +1,59 @@
+/* Copyright (c) 2008 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#ifndef SCRIPTDIR
+#define SCRIPTDIR="/usr/X11/lib/X11/xinit/privileged_startx.d"
+#endif
+
+static void usage(const char *prog) {
+ fprintf(stderr, "%s: usage\n", prog);
+ fprintf(stderr, " %s [-d [<script dir>]]\n\n", prog);
+ fprintf(stderr, " -d: Passed when called from launchd to denote server-mode.\n");
+ fprintf(stderr, " <script dir>: Directory to use instead of %s\n", SCRIPTDIR);
+}
+
+int client_main(void);
+int server_main(const char *dir);
+
+int main(int argc, char *argv[]) {
+
+ if(argc == 1) {
+ return client_main();
+ } else if(!strncmp(argv[1], "-d", 2)) {
+ if(argc == 2)
+ return server_main(NULL);
+ else if(argc == 3)
+ return server_main(argv[2]);
+ }
+
+ usage(argv[0]);
+ return 1;
+}
diff --git a/privileged_startx/privileged_startx.defs b/privileged_startx/privileged_startx.defs
new file mode 100644
index 0000000..86f2b3a
--- /dev/null
+++ b/privileged_startx/privileged_startx.defs
@@ -0,0 +1,40 @@
+/* Copyright (c) 2008 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#include <mach/std_types.defs>
+#include <mach/mach_types.defs>
+import "privileged_startx_types.h";
+
+subsystem privileged_startx 12345;
+serverprefix do_;
+
+routine privileged_startx(
+ port : mach_port_t);
+
+routine idle_exit(
+ port : mach_port_t);
diff --git a/privileged_startx/privileged_startx_types.h b/privileged_startx/privileged_startx_types.h
new file mode 100644
index 0000000..fef7195
--- /dev/null
+++ b/privileged_startx/privileged_startx_types.h
@@ -0,0 +1,6 @@
+#ifndef _PRIV_STARTX_TYPES_H_
+#define _PRIV_STARTX_TYPES_H_
+
+#define BOOTSTRAP_NAME "org.x.privileged_startx"
+
+#endif
diff --git a/privileged_startx/server.c b/privileged_startx/server.c
new file mode 100644
index 0000000..7afd424
--- /dev/null
+++ b/privileged_startx/server.c
@@ -0,0 +1,250 @@
+/* Copyright (c) 2008 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#include <mach/mach.h>
+#include <mach/mach_error.h>
+#include <servers/bootstrap.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fts.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/time.h>
+#include <launch.h>
+#include <asl.h>
+#include <pthread.h>
+#include <errno.h>
+
+#include "privileged_startx.h"
+#include "privileged_startxServer.h"
+
+union MaxMsgSize {
+ union __RequestUnion__privileged_startx_subsystem req;
+ union __ReplyUnion__privileged_startx_subsystem rep;
+};
+
+/* globals to trigger idle exit */
+#define DEFAULT_IDLE_TIMEOUT 60 /* 60 second timeout, then the server exits */
+
+struct idle_globals {
+ mach_port_t mp;
+ long timeout;
+ struct timeval lastmsg;
+};
+
+struct idle_globals idle_globals;
+
+#ifndef SCRIPTDIR
+#define SCRIPTDIR="/usr/X11/lib/X11/xinit/privileged_startx.d"
+#endif
+
+/* Default script dir */
+const char *script_dir = SCRIPTDIR;
+
+static void* idle_thread(void* param __attribute__((unused)));
+
+int server_main(const char *dir) {
+ mach_msg_size_t mxmsgsz = sizeof(union MaxMsgSize) + MAX_TRAILER_SIZE;
+ mach_port_t mp;
+ kern_return_t kr;
+ long idle_timeout = DEFAULT_IDLE_TIMEOUT;
+
+ launch_data_t config = NULL, checkin = NULL;
+ checkin = launch_data_new_string(LAUNCH_KEY_CHECKIN);
+ config = launch_msg(checkin);
+ if (!config || launch_data_get_type(config) == LAUNCH_DATA_ERRNO) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "launchd checkin failed");
+ exit(EXIT_FAILURE);
+ }
+
+ launch_data_t tmv;
+ tmv = launch_data_dict_lookup(config, LAUNCH_JOBKEY_TIMEOUT);
+ if (tmv) {
+ idle_timeout = launch_data_get_integer(tmv);
+ asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
+ "idle timeout set: %ld seconds", idle_timeout);
+ }
+
+ if(dir) {
+ script_dir = dir;
+ asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
+ "script directory set: %s", script_dir);
+ }
+
+ launch_data_t svc;
+ svc = launch_data_dict_lookup(config, LAUNCH_JOBKEY_MACHSERVICES);
+ if (!svc) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "no mach services");
+ exit(EXIT_FAILURE);
+ }
+
+ svc = launch_data_dict_lookup(svc, BOOTSTRAP_NAME);
+ if (!svc) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "no mach service: %s",
+ BOOTSTRAP_NAME);
+ exit(EXIT_FAILURE);
+ }
+
+ mp = launch_data_get_machport(svc);
+ if (mp == MACH_PORT_NULL) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "NULL mach service: %s",
+ BOOTSTRAP_NAME);
+ exit(EXIT_FAILURE);
+ }
+
+ /* insert a send right so we can send our idle exit message */
+ kr = mach_port_insert_right(mach_task_self(), mp, mp,
+ MACH_MSG_TYPE_MAKE_SEND);
+ if (kr != KERN_SUCCESS) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "send right failed: %s",
+ mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ /* spawn a thread to monitor our idle timeout */
+ pthread_t thread;
+ idle_globals.mp = mp;
+ idle_globals.timeout = idle_timeout;
+ gettimeofday(&idle_globals.lastmsg, NULL);
+ pthread_create(&thread, NULL, &idle_thread, NULL);
+
+ /* Main event loop */
+ kr = mach_msg_server(privileged_startx_server, mxmsgsz, mp, 0);
+ if (kr != KERN_SUCCESS) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR,
+ "mach_msg_server(mp): %s\n", mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ exit(EXIT_SUCCESS);
+}
+
+static int ftscmp(const FTSENT **a, const FTSENT **b) {
+ return strcmp((**a).fts_name, (**b).fts_name);
+}
+
+kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))) {
+ kern_return_t retval = KERN_SUCCESS;
+ char fn_buf[PATH_MAX + 1];
+ char *s;
+ int error_code;
+ FTS *ftsp;
+ FTSENT *ftsent;
+
+ const char * path_argv[2] = {script_dir, NULL};
+
+ /* Store that we were called, so the idle timer will reset */
+ gettimeofday(&idle_globals.lastmsg, NULL);
+
+ /* script_dir contains a set of files to run with root privs when X11 starts */
+ ftsp = fts_open(path_argv, FTS_PHYSICAL, ftscmp);
+ if(!ftsp) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR,
+ "do_privileged_startx: fts_open(%s): %s\n",
+ script_dir, strerror(errno));
+ return KERN_FAILURE;
+ }
+
+ /* Grab our dir */
+ ftsent = fts_read(ftsp);
+ if(!ftsent) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR,
+ "do_privileged_startx: fts_read(): %s\n", strerror(errno));
+ fts_close(ftsp);
+ return KERN_FAILURE;
+ }
+
+ /* Get a list of the files in this directory */
+ ftsent = fts_children(ftsp, 0);
+ if(!ftsent) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR,
+ "do_privileged_startx: fts_children(): %s\n", strerror(errno));
+ fts_close(ftsp);
+ return KERN_FAILURE;
+ }
+
+ /* Setup the buffer to have the path to the script dir */
+ strncpy(fn_buf, script_dir, PATH_MAX-1);
+ strcat(fn_buf, "/");
+ s = strrchr(fn_buf, 0);
+
+ /* Itterate over these files in alphabetical order */
+ for(; ftsent; ftsent = ftsent->fts_link) {
+ /* We only source regular files that are executable */
+ /* Note: This assumes we own them, which should always be the case */
+ if((ftsent->fts_statp->st_mode & S_IFREG) &&
+ (ftsent->fts_statp->st_mode & S_IXUSR)) {
+
+ /* Complete the full path filename in fn_buf */
+ strcpy(s, ftsent->fts_name);
+
+ /* Run it */
+ error_code = system(fn_buf);
+ if(error_code != 0) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR,
+ "do_privileged_startx: %s: exited with status %d\n",
+ fn_buf, error_code);
+ retval = KERN_FAILURE;
+ }
+ }
+ }
+
+ fts_close(ftsp);
+ return retval;
+}
+
+kern_return_t do_idle_exit(mach_port_t test_port __attribute__((unused))) {
+ struct timeval now;
+ gettimeofday(&now, NULL);
+
+ long delta = now.tv_sec - idle_globals.lastmsg.tv_sec;
+ if (delta >= idle_globals.timeout) {
+ exit(EXIT_SUCCESS);
+ }
+
+ return KERN_SUCCESS;
+}
+
+static void *idle_thread(void* param __attribute__((unused))) {
+ for(;;) {
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ long delta = (now.tv_sec - idle_globals.lastmsg.tv_sec);
+ if (delta < idle_globals.timeout) {
+ /* sleep for remainder of timeout */
+ sleep(idle_globals.timeout - delta);
+ } else {
+ /* timeout has elapsed, attempt to idle exit */
+ idle_exit(idle_globals.mp);
+ }
+ }
+ return NULL;
+}