]> git.saurik.com Git - apple/launchd.git/blobdiff - launchd/src/StartupItems.c
launchd-392.36.tar.gz
[apple/launchd.git] / launchd / src / StartupItems.c
index 3fbd269715c5249f62976a19a36871fd357af80c..9fc2325a6e1f7191c88a9d8283e6ce653558eb3a 100644 (file)
@@ -112,9 +112,9 @@ void AddItemToFailedList(StartupContext aStartupContext, CFMutableDictionaryRef
 }
 
 /**
- * startupItemListGetMatches returns an array of items which contain the string aService in the key aKey
+ * startupItemListCopyMatches returns an array of items which contain the string aService in the key aKey
  **/
-static CFMutableArrayRef startupItemListGetMatches(CFArrayRef anItemList, CFStringRef aKey, CFStringRef aService)
+static CFMutableArrayRef startupItemListCopyMatches(CFArrayRef anItemList, CFStringRef aKey, CFStringRef aService)
 {
        CFMutableArrayRef aResult = NULL;
 
@@ -183,13 +183,13 @@ static void SpecialCasesStartupItemHandler(CFMutableDictionaryRef aConfig)
                                        break;
                        }
                        if (*c == NULL) {
-                               CFRetain(ci);
                                CFArrayAppendValue(aNewList, ci);
                                CF_syslog(LOG_DEBUG, CFSTR("%@: Keeping %@"), type, ci);
                        }
                }
 
                CFDictionaryReplaceValue(aConfig, type, aNewList);
+               CFRelease(aNewList);
        }
        if (type == kUsesKey)
                return;
@@ -216,7 +216,7 @@ CFIndex StartupItemListCountServices(CFArrayRef anItemList)
        return aResult;
 }
 
-static bool StartupItemSecurityCheck(const char *aPath)
+bool StartupItemSecurityCheck(const char *aPath)
 {
        static struct timeval boot_time;
        struct stat aStatBuf;
@@ -286,14 +286,14 @@ CFMutableArrayRef StartupItemListCreateWithMask(NSSearchPathDomainMask aMask)
        while ((aState = NSGetNextSearchPathEnumeration(aState, aPath))) {
                DIR *aDirectory;
 
-               strcpy(aPath + strlen(aPath), kStartupItemsPath);
+               strlcat(aPath, kStartupItemsPath, sizeof(aPath));
                ++aDomainIndex;
 
                /* 5485016
                 *
                 * Just in case...
                 */
-               mkdir(aPath, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
+               mkdir(aPath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
 
                if (!StartupItemSecurityCheck(aPath))
                        continue;
@@ -430,15 +430,17 @@ CFMutableArrayRef StartupItemListCreateWithMask(NSSearchPathDomainMask aMask)
 CFMutableDictionaryRef StartupItemListGetProvider(CFArrayRef anItemList, CFStringRef aService)
 {
        CFMutableDictionaryRef aResult = NULL;
-       CFMutableArrayRef aList = startupItemListGetMatches(anItemList, kProvidesKey, aService);
+       CFMutableArrayRef aList = startupItemListCopyMatches(anItemList, kProvidesKey, aService);
 
        if (aList && CFArrayGetCount(aList) > 0)
                aResult = (CFMutableDictionaryRef) CFArrayGetValueAtIndex(aList, 0);
 
+       if (aList) CFRelease(aList);
+
        return aResult;
 }
 
-CFArrayRef StartupItemListGetRunning(CFArrayRef anItemList)
+CFArrayRef StartupItemListCreateFromRunning(CFArrayRef anItemList)
 {
        CFMutableArrayRef aResult = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
        if (aResult) {
@@ -578,7 +580,7 @@ static int countDependantsPresent(CFArrayRef aWaitingList, CFArrayRef anItemList
 
        for (anItemIndex = 0; anItemIndex < anItemCount; anItemIndex++) {
                CFStringRef anItem = CFArrayGetValueAtIndex(anItemList, anItemIndex);
-               CFArrayRef aMatchesList = startupItemListGetMatches(aWaitingList, aKey, anItem);
+               CFArrayRef aMatchesList = startupItemListCopyMatches(aWaitingList, aKey, anItem);
 
                if (aMatchesList) {
                        aCount = aCount + CFArrayGetCount(aMatchesList);
@@ -604,7 +606,7 @@ pendingAntecedents(CFArrayRef aWaitingList, CFDictionaryRef aStatusDict, CFArray
        for (anAntecedentIndex = 0; anAntecedentIndex < anAntecedentCount; ++anAntecedentIndex) {
                CFStringRef anAntecedent = CFArrayGetValueAtIndex(anAntecedentList, anAntecedentIndex);
                CFStringRef aKey = (anAction == kActionStart) ? kProvidesKey : kUsesKey;
-               CFArrayRef aMatchesList = startupItemListGetMatches(aWaitingList, aKey, anAntecedent);
+               CFArrayRef aMatchesList = startupItemListCopyMatches(aWaitingList, aKey, anAntecedent);
 
                if (aMatchesList) {
                        CFIndex aMatchesListCount = CFArrayGetCount(aMatchesList);
@@ -655,7 +657,7 @@ static Boolean checkForDuplicates(CFArrayRef aWaitingList, CFDictionaryRef aStat
                 * might provide that service.
                 */
                else {
-                       CFArrayRef aMatchesList = startupItemListGetMatches(aWaitingList, kProvidesKey, aProvides);
+                       CFArrayRef aMatchesList = startupItemListCopyMatches(aWaitingList, kProvidesKey, aProvides);
                        if (aMatchesList) {
                                CFIndex aMatchesListCount = CFArrayGetCount(aMatchesList);
                                CFIndex aMatchesListIndex;
@@ -834,7 +836,7 @@ CFMutableDictionaryRef StartupItemListGetNext(CFArrayRef aWaitingList, CFDiction
        return aNextItem;
 }
 
-CFStringRef StartupItemGetDescription(CFMutableDictionaryRef anItem)
+CFStringRef StartupItemCreateDescription(CFMutableDictionaryRef anItem)
 {
        CFStringRef aString = NULL;
 
@@ -923,28 +925,17 @@ int StartupItemRun(CFMutableDictionaryRef aStatusDict, CFMutableDictionaryRef an
                anError = 0;
        } else {
                CFStringRef aBundlePathString = CFDictionaryGetValue(anItem, kBundlePathKey);
-               size_t aBundlePathCLength =
-                   CFStringGetMaximumSizeForEncoding(CFStringGetLength(aBundlePathString), kCFStringEncodingUTF8) + 1;
-               char *aBundlePath = (char *)malloc(aBundlePathCLength);
-               char anExecutable[PATH_MAX] = "";
+               char aBundlePath[PATH_MAX];
+               char anExecutable[PATH_MAX];
+               char *tmp;
 
-               if (!aBundlePath) {
-                       syslog(LOG_EMERG, "malloc() failed; out of memory while running item %s", aBundlePathString);
-                       return (anError);
-               }
-               if (!CFStringGetCString(aBundlePathString, aBundlePath, aBundlePathCLength, kCFStringEncodingUTF8)) {
+               if (!CFStringGetCString(aBundlePathString, aBundlePath, sizeof(aBundlePath), kCFStringEncodingUTF8)) {
                        CF_syslog(LOG_EMERG, CFSTR("Internal error while running item %@"), aBundlePathString);
                        return (anError);
                }
                /* Compute path to excecutable */
-               {
-                       char           *tmp;
-                       strncpy(anExecutable, aBundlePath, sizeof(anExecutable));       /* .../foo     */
-                       tmp = rindex(anExecutable, '/');        /* /foo        */
-                       strncat(anExecutable, tmp, strlen(tmp));        /* .../foo/foo */
-               }
-
-               free(aBundlePath);
+               tmp = rindex(aBundlePath, '/');
+               snprintf(anExecutable, sizeof(anExecutable), "%s%s", aBundlePath, tmp);
 
                /**
                 * Run the bundle