]> git.saurik.com Git - apple/system_cmds.git/blobdiff - atrun.tproj/atrun.c
system_cmds-431.tar.gz
[apple/system_cmds.git] / atrun.tproj / atrun.c
index 4eac2cc87b9916038f3a0c6be255be272c516a7f..40552c0af28f49c5d273120aef5c3cef6a6704bd 100644 (file)
@@ -48,14 +48,18 @@ static const char rcsid[] =
 #include <syslog.h>
 #include <time.h>
 #include <unistd.h>
-#include <utmp.h>
+#include <utmpx.h>
 #if 1
 #include <paths.h>
 #else
 #include <getopt.h>
 #endif
 
+#if (MAXLOGNAME-1) > _UTX_USERSIZE
+#define LOGNAMESIZE _UTX_USERSIZE
+#else
 #define LOGNAMESIZE (MAXLOGNAME-1)
+#endif
 
 /* Local headers */
 
@@ -144,6 +148,13 @@ run_file(const char *filename, uid_t uid, gid_t gid)
     else if (pid != 0)
        return;
 
+#ifdef __APPLE__
+    {
+       pid_t pg = setsid();
+       if (pg == -1) syslog(LOG_ERR,"setsid() failed: %m");
+    }
+#endif
+
     /* Let's see who we mail to.  Hopefully, we can read it from
      * the command file; if not, send it to the owner, or, failing that,
      * to root.
@@ -286,12 +297,12 @@ run_file(const char *filename, uid_t uid, gid_t gid)
 
         nice(tolower(queue) - 'a');
        
-       if (initgroups(pentry->pw_name,pentry->pw_gid))
-           perr("cannot delete saved userids");
-
        if (setgid(gid) < 0 || setegid(pentry->pw_gid) < 0)
            perr("cannot change group");
 
+       if (initgroups(pentry->pw_name,pentry->pw_gid))
+           perr("cannot delete saved userids");
+
        if (setlogin(pentry->pw_name))
            perr("cannot set login name");
 
@@ -324,12 +335,12 @@ run_file(const char *filename, uid_t uid, gid_t gid)
     {    
        PRIV_START
 
-       if (initgroups(pentry->pw_name,pentry->pw_gid))
-           perr("cannot delete saved userids");
-
        if (setgid(gid) < 0 || setegid(pentry->pw_gid) < 0)
            perr("cannot change group");
 
+       if (initgroups(pentry->pw_name,pentry->pw_gid))
+           perr("cannot delete saved userids");
+
        if (setlogin(pentry->pw_name))
            perr("cannot set login name");
 
@@ -485,6 +496,10 @@ main(int argc, char *argv[])
        run_file(batch_name, batch_uid, batch_gid);
 
     closelog();
+#if __APPLE__
+    // allow enough time for child processes to call setsid(2)
+    sleep(1);
+#endif
     exit(EXIT_SUCCESS);
 }