Revision: 23544 http://trac.macosforge.org/projects/launchd/changeset/23544 Author: zarzycki@apple.com Date: 2008-03-10 14:09:42 -0700 (Mon, 10 Mar 2008) Log Message: ----------- <rdar://problem/5790562> dirname() is tricky Modified Paths: -------------- branches/SULeopard/launchd/src/launchd_core_logic.c Modified: branches/SULeopard/launchd/src/launchd_core_logic.c =================================================================== --- branches/SULeopard/launchd/src/launchd_core_logic.c 2008-03-10 20:49:07 UTC (rev 23543) +++ branches/SULeopard/launchd/src/launchd_core_logic.c 2008-03-10 21:09:42 UTC (rev 23544) @@ -170,7 +170,10 @@ SLIST_ENTRY(socketgroup) sle; int *fds; unsigned int junkfds:1, fd_cnt:31; - char name[0]; + union { + const char name[0]; + char name_init[0]; + }; }; static bool socketgroup_new(job_t j, const char *name, int *fds, unsigned int fd_cnt, bool junkfds); @@ -202,7 +205,10 @@ struct envitem { SLIST_ENTRY(envitem) sle; char *value; - char key[0]; + union { + const char key[0]; + char key_init[0]; + }; }; static bool envitem_new(job_t j, const char *k, const char *v, bool global); @@ -242,7 +248,10 @@ SLIST_ENTRY(semaphoreitem) sle; semaphore_reason_t why; int fd; - char what[0]; + union { + const char what[0]; + char what_init[0]; + }; }; struct semaphoreitem_dict_iter_context { @@ -279,7 +288,10 @@ unsigned int hopefully_first_cnt; unsigned int normal_active_cnt; unsigned int sent_stop_to_normal_jobs:1, sent_stop_to_hopefully_last_jobs:1, shutting_down:1, session_initialized:1; - char name[0]; + union { + const char name[0]; + char name_init[0]; + }; }; #define jobmgr_assumes(jm, e) \ @@ -3334,11 +3346,10 @@ void semaphoreitem_watch(job_t j, struct semaphoreitem *si) { - char parentdir_path[PATH_MAX], *which_path = si->what; + char *parentdir, tmp_path[PATH_MAX]; + const char *which_path = si->what; int saved_errno = 0; int fflags = 0; - - strlcpy(parentdir_path, dirname(si->what), sizeof(parentdir_path)); switch (si->why) { case PATH_EXISTS: @@ -3355,11 +3366,18 @@ return; } + /* dirname() may modify tmp_path */ + strlcpy(tmp_path, si->what, sizeof(tmp_path)); + + if (!job_assumes(j, (parentdir = dirname(tmp_path)))) { + return; + } + /* See 5321044 for why we do the do-while loop and 5415523 for why ENOENT is checked */ do { if (si->fd == -1) { if ((si->fd = _fd(open(which_path, O_EVTONLY|O_NOCTTY))) == -1) { - which_path = parentdir_path; + which_path = parentdir; si->fd = _fd(open(which_path, O_EVTONLY|O_NOCTTY)); } } @@ -3600,7 +3618,7 @@ } memcpy(sg->fds, fds, fd_cnt * sizeof(int)); - strcpy(sg->name, name); + strcpy(sg->name_init, name); SLIST_INSERT_HEAD(&j->sockets, sg, sle); @@ -3692,8 +3710,8 @@ return false; } - strcpy(ei->key, k); - ei->value = ei->key + strlen(k) + 1; + strcpy(ei->key_init, k); + ei->value = ei->key_init + strlen(k) + 1; strcpy(ei->value, v); if (global) { @@ -4401,7 +4419,7 @@ } jmr->kqjobmgr_callback = jobmgr_callback; - strcpy(jmr->name, name ? name : "Under construction"); + strcpy(jmr->name_init, name ? name : "Under construction"); jmr->req_port = requestorport; @@ -4449,7 +4467,7 @@ } if (!name) { - sprintf(jmr->name, "%u", MACH_PORT_INDEX(jmr->jm_port)); + sprintf(jmr->name_init, "%u", MACH_PORT_INDEX(jmr->jm_port)); } /* Sigh... at the moment, MIG has maxsize == sizeof(reply union) */ @@ -4889,7 +4907,7 @@ si->why = why; if (what) { - strcpy(si->what, what); + strcpy(si->what_init, what); } SLIST_INSERT_HEAD(&j->semaphores, si, sle); @@ -6168,7 +6186,7 @@ } jobmgr_log(j->mgr, LOG_DEBUG, "Renaming to: %s", session_type); - strcpy(j->mgr->name, session_type); + strcpy(j->mgr->name_init, session_type); if (job_assumes(j, (j2 = jobmgr_init_session(j->mgr, session_type, false)))) { job_assumes(j, job_dispatch(j2, true));