int run_parts(char *dir, char *cmd) { struct dirent **e; int i, num; num = scandir(dir, &e, NULL, alphasort); if (num < 0) { _d("No files found in %s, skipping ...", dir); return -1; } for (i = 0; i < num; i++) { int j = 0; pid_t pid = 0; mode_t mode; char *args[NUM_ARGS]; char *name = e[i]->d_name; char path[CMD_SIZE]; snprintf(path, sizeof(path), "%s/%s", dir, name); mode = fmode(path); if (!S_ISEXEC(mode) || S_ISDIR(mode)) { _d("Skipping %s ...", path); continue; } /* Fill in args[], starting with full path to executable */ args[j++] = path; /* If the callee didn't supply a run_parts() argument */ if (!cmd) { /* Check if S<NUM>service or K<NUM>service notation is used */ _d("Checking if %s is a sysvinit startstop script ...", name); if (name[0] == 'S' && isdigit(name[1])) { args[j++] = "start"; } else if (name[0] == 'K' && isdigit(name[1])) { args[j++] = "stop"; } } else { args[j++] = cmd; } args[j++] = NULL; pid = fork(); if (!pid) { _d("Calling %s ...", path); sig_unblock(); execv(path, args); exit(0); } complete(path, pid); } while (num--) free(e[num]); free(e); return 0; }
/* Reload all *.conf in /etc/finit.d/ */ void conf_reload_dynamic(void) { int i, num; char *dir = rcsd; struct dirent **e; /* Mark and sweep */ svc_mark_dynamic(); num = scandir(dir, &e, NULL, alphasort); if (num < 0) { _d("No files found in %s, skipping ...", dir); return; } for (i = 0; i < num; i++) { char *name = e[i]->d_name; char path[CMD_SIZE]; struct stat st; snprintf(path, sizeof(path), "%s/%s", dir, name); /* Check that it's an actual file ... */ if (stat(path, &st)) { _d("Cannot even read .conf file %s, skipping ...", path); continue; } if (S_ISEXEC(st.st_mode) || S_ISDIR(st.st_mode)) continue; /* Check that file ends with '.conf' */ if (strcmp(&path[strlen(path) - 5], ".conf")) { _d("File %s is not a .conf, skipping ... ", path); continue; } parse_conf_dynamic(path, st.st_mtime); } while (num--) free(e[num]); free(e); set_hostname(&hostname); }