summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Huddleston <jeremyhu@freedesktop.org>2008-11-27 00:47:31 -0800
committerJeremy Huddleston <jeremyhu@freedesktop.org>2008-11-27 00:47:31 -0800
commited6dc88f5cbaaad08a282c44ae333a938f6f4216 (patch)
tree8dfaa089e89b48b59d572adb59c0a779537c53d8
parent9f761b5a8512a8ad8b1475a3684e1a23d953916f (diff)
OSX: Create privleged_startx on Tiger
Launchd in Tiger doesn't support OnDemand loading, so this is a KeepAlive idle daemon on Tiger.
-rw-r--r--configure.ac16
-rw-r--r--privileged_startx/Makefile.am5
-rw-r--r--privileged_startx/org.x.privileged_startx.plist.cpp15
-rw-r--r--privileged_startx/server.c70
4 files changed, 84 insertions, 22 deletions
diff --git a/configure.ac b/configure.ac
index 2d09cad..86bd1f2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -102,18 +102,17 @@ AC_ARG_WITH(launchdaemons-dir, AS_HELP_STRING([--with-launchdaemons-dir=PATH],
if test "x$LAUNCHD" = "xauto"; then
unset LAUNCHD
- case $host_os in
- darwin8*)
- LAUNCHD=no
- ;;
- *)
- AC_CHECK_PROG(LAUNCHD, [launchd], [yes], [no])
- ;;
- esac
+ AC_CHECK_PROG(LAUNCHD, [launchd], [yes], [no])
fi
+TIGER_LAUNCHD=no
if test "x$LAUNCHD" = "xyes" ; then
AC_DEFINE(HAVE_LAUNCHD, 1, [launchd support available])
+ case $host_os in
+ darwin8*)
+ TIGER_LAUNCHD=yes
+ ;;
+ esac
else
launchagentsdir=""
launchdaemonsdir=""
@@ -121,6 +120,7 @@ fi
AC_SUBST([launchagentsdir])
AC_SUBST([launchdaemonsdir])
AM_CONDITIONAL(LAUNCHD, [test "x$LAUNCHD" = "xyes"])
+AM_CONDITIONAL(TIGER_LAUNCHD, [test "x$TIGER_LAUNCHD" = "xyes"])
# Checks for pkg-config packages
PKG_CHECK_MODULES(XINIT, x11)
diff --git a/privileged_startx/Makefile.am b/privileged_startx/Makefile.am
index f561bf4..2d68544 100644
--- a/privileged_startx/Makefile.am
+++ b/privileged_startx/Makefile.am
@@ -34,9 +34,12 @@ 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)"
+if TIGER_LAUNCHD
+CPP_FILES_FLAGS += -DTIGER_LAUNCHD
+endif
+
dist_privileged_startx_SOURCES = \
server.c \
client.c \
diff --git a/privileged_startx/org.x.privileged_startx.plist.cpp b/privileged_startx/org.x.privileged_startx.plist.cpp
index fd87478..7400cc4 100644
--- a/privileged_startx/org.x.privileged_startx.plist.cpp
+++ b/privileged_startx/org.x.privileged_startx.plist.cpp
@@ -4,20 +4,25 @@
<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>
+#ifdef TIGER_LAUNCHD
+ <key>KeepAlive</key>
+ <true/>
+#else
+ <key>MachServices</key>
+ <dict>
+ <key>org.x.privileged_startx</key>
+ <true/>
+ </dict>
<key>TimeOut</key>
<integer>120</integer>
<key>EnableTransactions</key>
<true/>
+#endif
</dict>
</plist>
diff --git a/privileged_startx/server.c b/privileged_startx/server.c
index 7afd424..d0356b5 100644
--- a/privileged_startx/server.c
+++ b/privileged_startx/server.c
@@ -40,7 +40,6 @@
#include <sys/time.h>
#include <launch.h>
#include <asl.h>
-#include <pthread.h>
#include <errno.h>
#include "privileged_startx.h"
@@ -51,6 +50,10 @@ union MaxMsgSize {
union __ReplyUnion__privileged_startx_subsystem rep;
};
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
+#include <pthread.h>
+static void* idle_thread(void* param __attribute__((unused)));
+
/* globals to trigger idle exit */
#define DEFAULT_IDLE_TIMEOUT 60 /* 60 second timeout, then the server exits */
@@ -61,6 +64,7 @@ struct idle_globals {
};
struct idle_globals idle_globals;
+#endif
#ifndef SCRIPTDIR
#define SCRIPTDIR="/usr/X11/lib/X11/xinit/privileged_startx.d"
@@ -69,13 +73,46 @@ struct idle_globals idle_globals;
/* Default script dir */
const char *script_dir = SCRIPTDIR;
-static void* idle_thread(void* param __attribute__((unused)));
+#ifndef LAUNCH_JOBKEY_MACHSERVICES
+static mach_port_t checkin_or_register(char *bname) {
+ kern_return_t kr;
+ mach_port_t mp;
+
+ /* If we're started by launchd or the old mach_init */
+ kr = bootstrap_check_in(bootstrap_port, bname, &mp);
+ if (kr == KERN_SUCCESS)
+ return mp;
+
+ /* We probably were not started by launchd or the old mach_init */
+ kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
+ if (kr != KERN_SUCCESS) {
+ fprintf(stderr, "mach_port_allocate(): %s\n", mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ kr = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND);
+ if (kr != KERN_SUCCESS) {
+ fprintf(stderr, "mach_port_insert_right(): %s\n", mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ kr = bootstrap_register(bootstrap_port, bname, mp);
+ if (kr != KERN_SUCCESS) {
+ fprintf(stderr, "bootstrap_register(): %s\n", mach_error_string(kr));
+ exit(EXIT_FAILURE);
+ }
+
+ return mp;
+}
+#endif
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;
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
long idle_timeout = DEFAULT_IDLE_TIMEOUT;
+#endif
launch_data_t config = NULL, checkin = NULL;
checkin = launch_data_new_string(LAUNCH_KEY_CHECKIN);
@@ -85,6 +122,13 @@ int server_main(const char *dir) {
exit(EXIT_FAILURE);
}
+ if(dir) {
+ script_dir = dir;
+ asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
+ "script directory set: %s", script_dir);
+ }
+
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
launch_data_t tmv;
tmv = launch_data_dict_lookup(config, LAUNCH_JOBKEY_TIMEOUT);
if (tmv) {
@@ -93,12 +137,6 @@ int server_main(const char *dir) {
"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) {
@@ -114,6 +152,14 @@ int server_main(const char *dir) {
}
mp = launch_data_get_machport(svc);
+#else
+ kr = bootstrap_check_in(bootstrap_port, bname, &mp);
+ if (kr == KERN_SUCCESS)
+ return mp;
+
+ mp = checkin_or_register("org.x.privileged_startx");
+#endif
+
if (mp == MACH_PORT_NULL) {
asl_log(NULL, NULL, ASL_LEVEL_ERR, "NULL mach service: %s",
BOOTSTRAP_NAME);
@@ -129,12 +175,14 @@ int server_main(const char *dir) {
exit(EXIT_FAILURE);
}
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
/* 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);
+#endif
/* Main event loop */
kr = mach_msg_server(privileged_startx_server, mxmsgsz, mp, 0);
@@ -222,6 +270,7 @@ kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))
}
kern_return_t do_idle_exit(mach_port_t test_port __attribute__((unused))) {
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
struct timeval now;
gettimeofday(&now, NULL);
@@ -231,8 +280,12 @@ kern_return_t do_idle_exit(mach_port_t test_port __attribute__((unused))) {
}
return KERN_SUCCESS;
+#else
+ return KERN_FAILURE;
+#endif
}
+#ifdef LAUNCH_JOBKEY_MACHSERVICES
static void *idle_thread(void* param __attribute__((unused))) {
for(;;) {
struct timeval now;
@@ -248,3 +301,4 @@ static void *idle_thread(void* param __attribute__((unused))) {
}
return NULL;
}
+#endif