Projects
Browse Source     Search     Timeline     Wiki

Changeset 23481

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

Most of the time, when launchd calls getpid(), it simply wants to know whether it should do "PID 1 magic." This simplifies the code a bit.

Location:
trunk/launchd/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/launchd/src/launchd.c

    r23480 r23481  
    135135        } 
    136136 
    137         if (getpid() == 1) { 
     137        if (pid1_magic) { 
    138138                pid1_magic_init(); 
    139139        } else { 
     
    143143        monitor_networking_state(); 
    144144 
    145         if (getpid() == 1) { 
     145        if (pid1_magic) { 
    146146                handle_pid1_crashes_separately(); 
    147147        } 
     
    254254        shutdown_in_progress = true; 
    255255 
    256         if (getpid() == 1) { 
     256        if (pid1_magic) { 
    257257                /* 
    258258                 * When this changes to a more sustainable API, update this: 
  • trunk/launchd/src/launchd_core_logic.c

    r23480 r23481  
    715715        } 
    716716 
    717         if (do_apple_internal_logging() && jm->parentmgr == NULL && getpid() == 1) { 
     717        if (do_apple_internal_logging() && jm->parentmgr == NULL && pid1_magic) { 
    718718                runtime_set_timeout(still_alive_with_check, 5); 
    719719        } 
     
    757757                runtime_del_ref(); 
    758758                SLIST_REMOVE(&jm->parentmgr->submgrs, jm, jobmgr_s, sle); 
    759         } else if (getpid() == 1) { 
     759        } else if (pid1_magic) { 
    760760                jobmgr_log(jm, LOG_DEBUG, "About to call: sync()"); 
    761761                sync(); /* We're are going to rely on log timestamps to benchmark this call */ 
     
    11211121                break; 
    11221122        case 1: 
    1123                 if (getpid() != 1) { 
     1123                if (!pid1_magic) { 
    11241124                        /* we cannot possibly find a parent job_t that is useful in this function */ 
    11251125                        break; 
     
    25252525                        runtime_closelog(); /* HACK -- force 'start' time to be set */ 
    25262526 
    2527                         if (getpid() == 1) { 
     2527                        if (pid1_magic) { 
    25282528                                int64_t now = runtime_get_wall_time(); 
    25292529 
     
    42054205                job_assumes(j, task_set_exception_ports(target_task, EXC_MASK_CRASH, exc_port, 
    42064206                                        EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, f) == KERN_SUCCESS); 
    4207         } else if (getpid() == 1 && the_exception_server) { 
     4207        } else if (pid1_magic && the_exception_server) { 
    42084208                mach_port_t mhp = mach_host_self(); 
    42094209                job_assumes(j, host_set_exception_ports(mhp, EXC_MASK_CRASH, the_exception_server, 
     
    42554255                                break; 
    42564256                        } 
    4257                 } else if (strcasecmp(key, LAUNCH_JOBKEY_MACH_HOSTSPECIALPORT) == 0 && getpid() == 1) { 
     4257                } else if (strcasecmp(key, LAUNCH_JOBKEY_MACH_HOSTSPECIALPORT) == 0 && pid1_magic) { 
    42584258                        if (which_port > HOST_MAX_SPECIAL_KERNEL_PORT) { 
    42594259                                job_assumes(ms->job, (errno = host_set_special_port(mhp, which_port, ms->port)) == KERN_SUCCESS); 
     
    43884388        struct kinfo_proc *kp; 
    43894389 
    4390         if (likely(jm->parentmgr || getpid() != 1)) { 
     4390        if (likely(jm->parentmgr || !pid1_magic)) { 
    43914391                return; 
    43924392        } 
     
    44844484                jobmgr_assumes(jmr, jm != NULL); 
    44854485                jmr->jm_port = transfer_port; 
    4486         } else if (!jm && getpid() != 1) { 
     4486        } else if (!jm && !pid1_magic) { 
    44874487                char *trusted_fd = getenv(LAUNCHD_TRUSTED_FD_ENV); 
    44884488                name_t service_buf; 
     
    52775277        /* XXX - This code should go away once the per session launchd is integrated with the rest of the system */ 
    52785278#ifdef LET_MERE_MORTALS_ADD_SERVERS_TO_PID1 
    5279         if (getpid() == 1) { 
     5279        if (pid1_magic) { 
    52805280                if (unlikely(ldc.euid && server_uid && (ldc.euid != server_uid))) { 
    52815281                        job_log(j, LOG_WARNING, "Server create: \"%s\": Will run as UID %d, not UID %d as they told us to", 
     
    56635663        } 
    56645664 
    5665         if (unlikely(getpid() != 1)) { 
     5665        if (unlikely(!pid1_magic)) { 
    56665666                return BOOTSTRAP_NOT_PRIVILEGED; 
    56675667        } 
     
    57325732ensure_root_bkgd_setup(void) 
    57335733{ 
    5734         if (likely(background_jobmgr) || getpid() != 1) { 
     5734        if (likely(background_jobmgr) || !pid1_magic) { 
    57355735                return; 
    57365736        } 
     
    57585758        runtime_get_caller_creds(&ldc); 
    57595759 
    5760         if (unlikely(getpid() != 1)) { 
     5760        if (unlikely(!pid1_magic)) { 
    57615761                job_log(j, LOG_ERR, "Only PID 1 supports per user launchd lookups."); 
    57625762                return BOOTSTRAP_NOT_PRIVILEGED; 
     
    58975897         */ 
    58985898        if (unlikely(j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.uid != getuid() && ldc.uid != 92)) { 
    5899                 if (getpid() == 1) { 
     5899                if (pid1_magic) { 
    59005900                        return VPROC_ERR_TRY_PER_USER; 
    59015901                } else { 
     
    59425942        runtime_get_caller_creds(&ldc); 
    59435943 
    5944         if (unlikely(getpid() == 1 && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0)) { 
     5944        if (unlikely(pid1_magic && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0)) { 
    59455945                return VPROC_ERR_TRY_PER_USER; 
    59465946        } 
     
    59825982                /* The previous routine moved the reply port, we're forced to return MIG_NO_REPLY now */ 
    59835983                return MIG_NO_REPLY; 
    5984         } else if (getpid() == 1 && j->anonymous && ldc.euid >= 500 && strcasecmp(job_get_bs(j)->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { 
     5984        } else if (pid1_magic && j->anonymous && ldc.euid >= 500 && strcasecmp(job_get_bs(j)->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { 
    59855985                /* 
    59865986                 * 5240036 Should start background session when a lookup of CCacheServer occurs 
     
    61146114                } else if (strcasecmp(jmi->name, where) == 0) { 
    61156115                        goto jm_found; 
    6116                 } else if (strcasecmp(jmi->name, VPROCMGR_SESSION_BACKGROUND) == 0 && getpid() == 1) { 
     6116                } else if (strcasecmp(jmi->name, VPROCMGR_SESSION_BACKGROUND) == 0 && pid1_magic) { 
    61176117                        SLIST_FOREACH(jmi2, &jmi->submgrs, sle) { 
    61186118                                if (strcasecmp(jmi2->name, where) == 0) { 
     
    61636163 
    61646164                if (j->mgr->session_initialized) { 
    6165                         if (ldc.uid == 0 && getpid() == 1) { 
     6165                        if (ldc.uid == 0 && pid1_magic) { 
    61666166                                if (strcmp(j->mgr->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { 
    61676167                                        job_t ji, jn; 
     
    62016201                                goto out; 
    62026202                        } 
    6203                 } else if (ldc.uid == 0 && getpid() == 1 && strcmp(session_type, VPROCMGR_SESSION_STANDARDIO) == 0) { 
     6203                } else if (ldc.uid == 0 && pid1_magic && strcmp(session_type, VPROCMGR_SESSION_STANDARDIO) == 0) { 
    62046204                        ensure_root_bkgd_setup(); 
    62056205 
     
    63356335        jm = j->mgr; 
    63366336 
    6337         if (unlikely(getpid() != 1)) { 
     6337        if (unlikely(!pid1_magic)) { 
    63386338                job_log(j, LOG_ERR, "Only the system launchd will transfer Mach sub-bootstraps."); 
    63396339                return BOOTSTRAP_NOT_PRIVILEGED; 
     
    66076607        } 
    66086608 
    6609         if (unlikely(getpid() == 1 && ldc.euid && ldc.uid)) { 
     6609        if (unlikely(pid1_magic && ldc.euid && ldc.uid)) { 
    66106610                job_log(j, LOG_DEBUG, "Punting spawn to per-user-context"); 
    66116611                return VPROC_ERR_TRY_PER_USER; 
     
    66336633        job_reparent_hack(jr, NULL); 
    66346634 
    6635         if (getpid() == 1) { 
     6635        if (pid1_magic) { 
    66366636                jr->mach_uid = ldc.uid; 
    66376637        } 
     
    66716671jobmgr_init(bool sflag) 
    66726672{ 
    6673         const char *root_session_type = getpid() == 1 ? VPROCMGR_SESSION_SYSTEM : VPROCMGR_SESSION_BACKGROUND; 
     6673        const char *root_session_type = pid1_magic ? VPROCMGR_SESSION_SYSTEM : VPROCMGR_SESSION_BACKGROUND; 
    66746674 
    66756675        launchd_assert((root_jobmgr = jobmgr_new(NULL, MACH_PORT_NULL, MACH_PORT_NULL, sflag, root_session_type)) != NULL); 
  • trunk/launchd/src/launchd_runtime.c

    r23479 r23481  
    121121static sigset_t sigign_set; 
    122122static FILE *ourlogfile; 
    123  
     123bool pid1_magic; 
    124124 
    125125INTERNAL_ABI mach_port_t 
     
    11591159        struct runtime_syslog_attr attr = { 
    11601160                "com.apple.launchd", "com.apple.launchd", 
    1161                 getpid() == 1 ? "System" : "Background", 
     1161                pid1_magic ? "System" : "Background", 
    11621162                pri, getuid(), getpid(), getpid() 
    11631163        }; 
     
    13271327                launchd_assumes(STAILQ_EMPTY(&logmsg_queue)); 
    13281328                return; 
    1329         } else if (getpid() != 1) { 
     1329        } else if (!pid1_magic) { 
    13301330                if (runtime_log_pack(&outval, &outvalCnt) == 0) { 
    13311331                        launchd_assumes(_vprocmgr_log_forward(inherited_bootstrap_port, (void *)outval, outvalCnt) == NULL); 
     
    16061606        tbi_float_val = tbi.numer; 
    16071607        tbi_float_val /= tbi.denom; 
    1608 } 
     1608 
     1609        if (getpid() == 1) { 
     1610                pid1_magic = true; 
     1611        } 
     1612} 
  • trunk/launchd/src/launchd_runtime.h

    r23480 r23481  
    104104typedef INTERNAL_ABI void (*timeout_callback)(void); 
    105105 
     106extern bool pid1_magic; 
     107 
    106108INTERNAL_ABI mach_port_t runtime_get_kernel_port(void); 
    107109 
  • trunk/launchd/src/launchd_unix_ipc.c

    r23480 r23481  
    103103        sun.sun_family = AF_UNIX; 
    104104 
    105         if (getpid() == 1) { 
     105        if (pid1_magic) { 
    106106                strcpy(ourdir, LAUNCHD_SOCK_PREFIX); 
    107107                strncpy(sun.sun_path, LAUNCHD_SOCK_PREFIX "/sock", sizeof(sun.sun_path)); 
     
    459459                        } 
    460460 
    461                         if (/* XXX readcfg_pid && */ getpid() == 1 && (i == RLIMIT_NOFILE || i == RLIMIT_NPROC)) { 
     461                        if (/* XXX readcfg_pid && */ pid1_magic && (i == RLIMIT_NOFILE || i == RLIMIT_NPROC)) { 
    462462                                int gmib[] = { CTL_KERN, KERN_MAXPROC }; 
    463463                                int pmib[] = { CTL_KERN, KERN_MAXPROCPERUID };