Projects
Browse Source     Search     Timeline     Wiki

Changeset 23478

Show
Ignore:
Timestamp:
01/09/08 09:35:12 (11 months ago)
Author:
zarzycki@…
Message:

More misc. branch hints, plus basic (and untested) "StandardInPath" support.

Location:
trunk/launchd/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/launchd/src/launchd.plist.5

    r23406 r23478  
    227227The month on which this job will be run. 
    228228.El 
     229.It Sy StandardInPath <string> 
     230This optional key specifies what file should be used for data being supplied to stdin when using 
     231.Xr stdio 3 . 
    229232.It Sy StandardOutPath <string> 
    230233This optional key specifies what file should be used for data being sent to stdout when using 
  • trunk/launchd/src/launchd_core_logic.c

    r23477 r23478  
    332332        char *username; 
    333333        char *groupname; 
     334        char *stdinpath; 
    334335        char *stdoutpath; 
    335336        char *stderrpath; 
     
    344345        int argc; 
    345346        int last_exit_status; 
    346         int forkfd; 
     347        int stdin_fd; 
     348        int fork_fd; 
    347349        int log_redirect_fd; 
    348350        int nice; 
     
    459461static mach_port_t the_exception_server; 
    460462static bool did_first_per_user_launchd_BootCache_hack; 
    461 #define JOB_BOOTCACHE_HACK_CHECK(j)     (j->per_user && !did_first_per_user_launchd_BootCache_hack && (j->mach_uid >= 500) && (j->mach_uid != (uid_t)-2)) 
     463#define JOB_BOOTCACHE_HACK_CHECK(j)     (unlikely(j->per_user && !did_first_per_user_launchd_BootCache_hack && (j->mach_uid >= 500) && (j->mach_uid != (uid_t)-2))) 
    462464static jobmgr_t background_jobmgr; 
    463465static job_t workaround_5477111; 
     
    576578        if (j->prog && (tmp = launch_data_new_string(j->prog))) { 
    577579                launch_data_dict_insert(r, tmp, LAUNCH_JOBKEY_PROGRAM); 
     580        } 
     581        if (j->stdinpath && (tmp = launch_data_new_string(j->stdinpath))) { 
     582                launch_data_dict_insert(r, tmp, LAUNCH_JOBKEY_STANDARDINPATH); 
    578583        } 
    579584        if (j->stdoutpath && (tmp = launch_data_new_string(j->stdoutpath))) { 
     
    811816        } 
    812817 
    813         if (!job_assumes(j, j->forkfd == 0)) { 
    814                 job_assumes(j, runtime_close(j->forkfd) != -1); 
     818        if (!job_assumes(j, j->fork_fd == 0)) { 
     819                job_assumes(j, runtime_close(j->fork_fd) != -1); 
     820        } 
     821 
     822        if (j->stdin_fd) { 
     823                job_assumes(j, runtime_close(j->stdin_fd) != -1); 
    815824        } 
    816825 
     
    872881        if (j->groupname) { 
    873882                free(j->groupname); 
     883        } 
     884        if (j->stdinpath) { 
     885                free(j->stdinpath); 
    874886        } 
    875887        if (j->stdoutpath) { 
     
    15001512                } else if (strcasecmp(key, LAUNCH_JOBKEY_STANDARDERRORPATH) == 0) { 
    15011513                        where2put = &j->stderrpath; 
     1514                } else if (strcasecmp(key, LAUNCH_JOBKEY_STANDARDINPATH) == 0) { 
     1515                        where2put = &j->stdinpath; 
     1516                        j->stdin_fd = _fd(open(value, O_RDONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, DEFFILEMODE)); 
     1517                        if (job_assumes(j, j->stdin_fd != -1)) { 
     1518                                /* open() should not block, but regular IO by the job should */ 
     1519                                job_assumes(j, fcntl(j->stdin_fd, F_SETFL, 0) != -1); 
     1520                                /* XXX -- EV_CLEAR should make named pipes happy? */ 
     1521                                job_assumes(j, kevent_mod(j->stdin_fd, EVFILT_READ, EV_ADD|EV_CLEAR, 0, 0, j) != -1); 
     1522                        } else { 
     1523                                j->stdin_fd = 0; 
     1524                        } 
    15021525                } else if (strcasecmp(key, LAUNCH_JOBKEY_SANDBOXPROFILE) == 0) { 
    15031526                        where2put = &j->seatbelt_profile; 
     
    20952118        } 
    20962119 
    2097         if (j->forkfd) { 
    2098                 job_assumes(j, runtime_close(j->forkfd) != -1); 
    2099                 j->forkfd = 0; 
     2120        if (j->fork_fd) { 
     2121                job_assumes(j, runtime_close(j->fork_fd) != -1); 
     2122                j->fork_fd = 0; 
    21002123        } 
    21012124 
     
    24612484        if (ident == j->log_redirect_fd) { 
    24622485                job_log_stdouterr(j); 
     2486        } else if (ident == j->stdin_fd) { 
     2487                job_dispatch(j, true); 
    24632488        } else { 
    24642489                socketgroup_callback(j); 
     
    25812606        } 
    25822607 
    2583         if (job_active(j)) { 
     2608        if (unlikely(job_active(j))) { 
    25842609                job_log(j, LOG_DEBUG, "Already started"); 
    25852610                return; 
     
    26122637        j->sent_sigterm_time = 0; 
    26132638 
    2614         if (!j->legacy_mach_job) { 
     2639        if (likely(!j->legacy_mach_job)) { 
    26152640                sipc = (!SLIST_EMPTY(&j->sockets) || !SLIST_EMPTY(&j->machservices)); 
    26162641        } 
     
    26242649        job_assumes(j, socketpair(AF_UNIX, SOCK_STREAM, 0, execspair) != -1); 
    26252650 
    2626         if (!j->legacy_mach_job && job_assumes(j, pipe(oepair) != -1)) { 
     2651        if (likely(!j->legacy_mach_job) && job_assumes(j, pipe(oepair) != -1)) { 
    26272652                j->log_redirect_fd = _fd(oepair[0]); 
    26282653                job_assumes(j, fcntl(j->log_redirect_fd, F_SETFL, O_NONBLOCK) != -1); 
     
    26412666                        job_assumes(j, runtime_close(spair[1]) == 0); 
    26422667                } 
    2643                 if (!j->legacy_mach_job) { 
     2668                if (likely(!j->legacy_mach_job)) { 
    26442669                        job_assumes(j, runtime_close(oepair[0]) != -1); 
    26452670                        job_assumes(j, runtime_close(oepair[1]) != -1); 
     
    26482673                break; 
    26492674        case 0: 
    2650                 if (_vproc_post_fork_ping()) { 
     2675                if (unlikely(_vproc_post_fork_ping())) { 
    26512676                        _exit(EXIT_FAILURE); 
    26522677                } 
     
    26802705                } 
    26812706 
    2682                 if (!j->legacy_mach_job) { 
     2707                if (likely(!j->legacy_mach_job)) { 
    26832708                        job_assumes(j, runtime_close(oepair[1]) != -1); 
    26842709                } 
    26852710                j->p = c; 
    2686                 if (j->hopefully_exits_first) { 
     2711                if (unlikely(j->hopefully_exits_first)) { 
    26872712                        j->mgr->hopefully_first_cnt++; 
    2688                 } else if (!j->hopefully_exits_last) { 
     2713                } else if (likely(!j->hopefully_exits_last)) { 
    26892714                        j->mgr->normal_active_cnt++; 
    26902715                } 
    2691                 j->forkfd = _fd(execspair[0]); 
     2716                j->fork_fd = _fd(execspair[0]); 
    26922717                job_assumes(j, runtime_close(execspair[1]) == 0); 
    26932718                if (sipc) { 
     
    27012726                } 
    27022727 
    2703                 if (!j->stall_before_exec) { 
     2728                if (likely(!j->stall_before_exec)) { 
    27042729                        job_uncork_fork(j); 
    27052730                } 
     
    27462771        job_setup_attributes(j); 
    27472772 
    2748         if (j->argv && j->globargv) { 
     2773        if (unlikely(j->argv && j->globargv)) { 
    27492774                g.gl_offs = 1; 
    27502775                for (i = 0; i < j->argc; i++) { 
     
    27732798        } 
    27742799 
    2775         if (!j->inetcompat) { 
     2800        if (likely(!j->inetcompat)) { 
    27762801                argv++; 
    27772802        } 
    27782803 
    2779         if (j->wait4debugger) { 
     2804        if (unlikely(j->wait4debugger)) { 
    27802805                job_log(j, LOG_WARNING, "Spawned and waiting for the debugger to attach before continuing..."); 
    27812806                spflags |= POSIX_SPAWN_START_SUSPENDED; 
     
    27842809        job_assumes(j, posix_spawnattr_setflags(&spattr, spflags) == 0); 
    27852810 
    2786         if (j->j_binpref_cnt) { 
     2811        if (unlikely(j->j_binpref_cnt)) { 
    27872812                job_assumes(j, posix_spawnattr_setbinpref_np(&spattr, j->j_binpref_cnt, j->j_binpref, &binpref_out_cnt) == 0); 
    27882813                job_assumes(j, binpref_out_cnt == j->j_binpref_cnt); 
     
    28122837        psf = j->prog ? posix_spawn : posix_spawnp; 
    28132838 
    2814         if (!j->inetcompat) { 
     2839        if (likely(!j->inetcompat)) { 
    28152840                file2exec = j->prog ? j->prog : argv[0]; 
    28162841        } 
     
    29652990        strlcpy(homedir, pwe->pw_dir, sizeof(homedir)); 
    29662991 
    2967         if (pwe->pw_expire && time(NULL) >= pwe->pw_expire) { 
     2992        if (unlikely(pwe->pw_expire && time(NULL) >= pwe->pw_expire)) { 
    29682993                job_log(j, LOG_ERR, "Expired account"); 
    29692994                _exit(EXIT_FAILURE); 
     
    29712996 
    29722997 
    2973         if (j->username && strcmp(j->username, loginname) != 0) { 
     2998        if (unlikely(j->username && strcmp(j->username, loginname) != 0)) { 
    29742999                job_log(j, LOG_WARNING, "Suspicious setup: User \"%s\" maps to user: %s", j->username, loginname); 
    2975         } else if (j->mach_uid && (j->mach_uid != desired_uid)) { 
     3000        } else if (unlikely(j->mach_uid && (j->mach_uid != desired_uid))) { 
    29763001                job_log(j, LOG_WARNING, "Suspicious setup: UID %u maps to UID %u", j->mach_uid, desired_uid); 
    29773002        } 
     
    29803005                struct group *gre; 
    29813006 
    2982                 if ((gre = getgrnam(j->groupname)) == NULL) { 
     3007                if (unlikely((gre = getgrnam(j->groupname)) == NULL)) { 
    29833008                        job_log(j, LOG_ERR, "getgrnam(\"%s\") failed", j->groupname); 
    29843009                        _exit(EXIT_FAILURE); 
     
    30013026         */ 
    30023027 
    3003         if (!j->no_init_groups) { 
     3028        if (likely(!j->no_init_groups)) { 
    30043029                if (!job_assumes(j, initgroups(loginname, desired_gid) != -1)) { 
    30053030                        _exit(EXIT_FAILURE); 
     
    30133038        r = confstr(_CS_DARWIN_USER_TEMP_DIR, tmpdirpath, sizeof(tmpdirpath)); 
    30143039 
    3015         if (r > 0 && r < sizeof(tmpdirpath)) { 
     3040        if (likely(r > 0 && r < sizeof(tmpdirpath))) { 
    30163041                setenv("TMPDIR", tmpdirpath, 0); 
    30173042        } 
     
    30293054        struct envitem *ei; 
    30303055 
    3031         if (j->setnice) { 
     3056        if (unlikely(j->setnice)) { 
    30323057                job_assumes(j, setpriority(PRIO_PROCESS, 0, j->nice) != -1); 
    30333058        } 
     
    30523077        } 
    30533078 
    3054         if (!j->inetcompat && j->session_create) { 
     3079        if (unlikely(!j->inetcompat && j->session_create)) { 
    30553080                launchd_SessionCreate(); 
    30563081        } 
    30573082 
    3058         if (j->low_pri_io) { 
     3083        if (unlikely(j->low_pri_io)) { 
    30593084                job_assumes(j, setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_THROTTLE) != -1); 
    30603085        } 
    3061         if (j->rootdir) { 
     3086        if (unlikely(j->rootdir)) { 
    30623087                job_assumes(j, chroot(j->rootdir) != -1); 
    30633088                job_assumes(j, chdir(".") != -1); 
     
    30663091        job_postfork_become_user(j); 
    30673092 
    3068         if (j->workingdir) { 
     3093        if (unlikely(j->workingdir)) { 
    30693094                job_assumes(j, chdir(j->workingdir) != -1); 
    30703095        } 
    30713096 
    3072         if (j->setmask) { 
     3097        if (unlikely(j->setmask)) { 
    30733098                umask(j->mask); 
    30743099        } 
    30753100 
    3076         job_setup_fd(j, STDOUT_FILENO, j->stdoutpath, O_WRONLY|O_APPEND|O_CREAT); 
    3077         job_setup_fd(j, STDERR_FILENO, j->stderrpath, O_WRONLY|O_APPEND|O_CREAT); 
     3101        if (j->stdin_fd) { 
     3102                job_assumes(j, dup2(j->stdin_fd, STDIN_FILENO) != -1); 
     3103        } else { 
     3104                job_setup_fd(j, STDIN_FILENO,  j->stdinpath,  O_RDONLY|O_CREAT); 
     3105        } 
     3106        job_setup_fd(j, STDOUT_FILENO, j->stdoutpath, O_WRONLY|O_CREAT|O_APPEND); 
     3107        job_setup_fd(j, STDERR_FILENO, j->stderrpath, O_WRONLY|O_CREAT|O_APPEND); 
    30783108 
    30793109        jobmgr_setup_env_from_other_jobs(j->mgr); 
     
    31203150        bool r = 0; 
    31213151 
    3122         if (!dd) { 
     3152        if (unlikely(!dd)) { 
    31233153                return -1; 
    31243154        } 
     
    31863216                time_string_len = strlen(time_string); 
    31873217 
    3188                 if (time_string_len && time_string[time_string_len - 1] == '\n') { 
     3218                if (likely(time_string_len && time_string[time_string_len - 1] == '\n')) { 
    31893219                        time_string[time_string_len - 1] = '\0'; 
    31903220                } 
     
    32883318        } 
    32893319 
    3290         if (j->debug) { 
     3320        if (unlikely(j->debug)) { 
    32913321                oldmask = setlogmask(LOG_UPTO(LOG_DEBUG)); 
    32923322        } 
     
    32943324        runtime_vsyslog(&attr, newmsg, ap); 
    32953325 
    3296         if (j->debug) { 
     3326        if (unlikely(j->debug)) { 
    32973327                setlogmask(oldmask); 
    32983328        } 
     
    44154445        /* this unblocks the child and avoids a race 
    44164446         * between the above fork() and the kevent_mod() */ 
    4417         job_assumes(j, write(j->forkfd, &c, sizeof(c)) == sizeof(c)); 
    4418         job_assumes(j, runtime_close(j->forkfd) != -1); 
    4419         j->forkfd = 0; 
     4447        job_assumes(j, write(j->fork_fd, &c, sizeof(c)) == sizeof(c)); 
     4448        job_assumes(j, runtime_close(j->fork_fd) != -1); 
     4449        j->fork_fd = 0; 
    44204450} 
    44214451 
  • trunk/launchd/src/liblaunch_public.h

    r23406 r23478  
    8686#define LAUNCH_JOBKEY_SOFTRESOURCELIMITS        "SoftResourceLimits" 
    8787#define LAUNCH_JOBKEY_HARDRESOURCELIMITS        "HardResourceLimits" 
     88#define LAUNCH_JOBKEY_STANDARDINPATH            "StandardInPath" 
    8889#define LAUNCH_JOBKEY_STANDARDOUTPATH           "StandardOutPath" 
    8990#define LAUNCH_JOBKEY_STANDARDERRORPATH         "StandardErrorPath"