+static pid_t all_pids[10];
+static int num_pids;
+
+/** Fork and record the pid of the child. **/
+pid_t do_fork(void)
+{
+ pid_t newpid = fork();
+
+ if (newpid != 0 && newpid != -1) {
+ all_pids[num_pids++] = newpid;
+ }
+ return newpid;
+}
+
+/**
+ * Kill all children.
+ *
+ * @todo It would be kind of nice to make sure that they are actually
+ * all our children before we kill them, because their pids may have
+ * been recycled by some other process. Perhaps when we wait for a
+ * child, we should remove it from this array. Alternatively we could
+ * perhaps use process groups, but I think that would not work on
+ * ancient Unix versions that don't support them.
+ **/
+void kill_all(int sig)
+{
+ int i;
+
+ for (i = 0; i < num_pids; i++) {
+ /* Let's just be a little careful where we
+ * point that gun, hey? See kill(2) for the
+ * magic caused by negative values. */
+ pid_t p = all_pids[i];
+
+ if (p == getpid())
+ continue;
+ if (p <= 0)
+ continue;
+
+ kill(p, sig);
+ }
+}
+
+/** Turn a user name into a uid */
+int name_to_uid(const char *name, uid_t *uid)
+{
+ struct passwd *pass;
+ if (!name || !*name)
+ return 0;
+ pass = getpwnam(name);
+ if (pass) {
+ *uid = pass->pw_uid;
+ return 1;
+ }
+ return 0;
+}
+
+/** Turn a group name into a gid */
+int name_to_gid(const char *name, gid_t *gid)
+{
+ struct group *grp;
+ if (!name || !*name)
+ return 0;
+ grp = getgrnam(name);
+ if (grp) {
+ *gid = grp->gr_gid;
+ return 1;
+ }
+ return 0;
+}
+
+/** Lock a byte range in a open file */
+int lock_range(int fd, int offset, int len)
+{
+ struct flock lock;
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = offset;
+ lock.l_len = len;
+ lock.l_pid = 0;
+
+ return fcntl(fd,F_SETLK,&lock) == 0;
+}
+
+static int filter_server_path(char *arg)
+{
+ char *s;
+
+ if (server_filter_list.head) {
+ for (s = arg; (s = strchr(s, '/')) != NULL; ) {
+ *s = '\0';
+ if (check_filter(&server_filter_list, arg, 1) < 0) {
+ /* We must leave arg truncated! */
+ return 1;
+ }
+ *s++ = '/';
+ }
+ }
+ return 0;
+}
+
+void glob_expand(char *s, char ***argv_ptr, int *argc_ptr, int *maxargs_ptr)
+{
+ char **argv = *argv_ptr;
+ int argc = *argc_ptr;
+ int maxargs = *maxargs_ptr;
+#if !defined HAVE_GLOB || !defined HAVE_GLOB_H
+ if (argc == maxargs) {
+ maxargs += MAX_ARGS;
+ if (!(argv = realloc_array(argv, char *, maxargs)))
+ out_of_memory("glob_expand");
+ *argv_ptr = argv;
+ *maxargs_ptr = maxargs;
+ }
+ if (!*s)
+ s = ".";
+ s = argv[argc++] = strdup(s);
+ filter_server_path(s);
+#else
+ glob_t globbuf;
+
+ if (maxargs <= argc)
+ return;
+ if (!*s)
+ s = ".";
+
+ if (sanitize_paths)
+ s = sanitize_path(NULL, s, "", 0, NULL);
+ else
+ s = strdup(s);
+ if (!s)
+ out_of_memory("glob_expand");
+
+ memset(&globbuf, 0, sizeof globbuf);
+ if (!filter_server_path(s))
+ glob(s, 0, NULL, &globbuf);
+ if (MAX((int)globbuf.gl_pathc, 1) > maxargs - argc) {
+ maxargs += globbuf.gl_pathc + MAX_ARGS;
+ if (!(argv = realloc_array(argv, char *, maxargs)))
+ out_of_memory("glob_expand");
+ *argv_ptr = argv;
+ *maxargs_ptr = maxargs;
+ }
+ if (globbuf.gl_pathc == 0)
+ argv[argc++] = s;
+ else {
+ int i;
+ free(s);
+ for (i = 0; i < (int)globbuf.gl_pathc; i++) {
+ if (!(argv[argc++] = strdup(globbuf.gl_pathv[i])))
+ out_of_memory("glob_expand");
+ }
+ }
+ globfree(&globbuf);
+#endif
+ *argc_ptr = argc;
+}
+
+/* This routine is only used in daemon mode. */
+void glob_expand_module(char *base1, char *arg, char ***argv_ptr, int *argc_ptr, int *maxargs_ptr)
+{
+ char *p, *s;
+ char *base = base1;
+ int base_len = strlen(base);
+
+ if (!arg || !*arg)
+ return;
+
+ if (strncmp(arg, base, base_len) == 0)
+ arg += base_len;
+
+ if (!(arg = strdup(arg)))
+ out_of_memory("glob_expand_module");
+
+ if (asprintf(&base," %s/", base1) <= 0)
+ out_of_memory("glob_expand_module");
+ base_len++;
+
+ for (s = arg; *s; s = p + base_len) {
+ if ((p = strstr(s, base)) != NULL)
+ *p = '\0'; /* split it at this point */
+ glob_expand(s, argv_ptr, argc_ptr, maxargs_ptr);
+ if (!p)
+ break;
+ }
+
+ free(arg);
+ free(base);
+}
+
+/**
+ * Convert a string to lower case
+ **/
+void strlower(char *s)
+{
+ while (*s) {
+ if (isUpper(s))
+ *s = toLower(s);
+ s++;
+ }
+}
+
+/* Join strings p1 & p2 into "dest" with a guaranteed '/' between them. (If
+ * p1 ends with a '/', no extra '/' is inserted.) Returns the length of both
+ * strings + 1 (if '/' was inserted), regardless of whether the null-terminated
+ * string fits into destsize. */
+size_t pathjoin(char *dest, size_t destsize, const char *p1, const char *p2)
+{
+ size_t len = strlcpy(dest, p1, destsize);
+ if (len < destsize - 1) {
+ if (!len || dest[len-1] != '/')
+ dest[len++] = '/';
+ if (len < destsize - 1)
+ len += strlcpy(dest + len, p2, destsize - len);
+ else {
+ dest[len] = '\0';
+ len += strlen(p2);
+ }
+ }
+ else
+ len += strlen(p2) + 1; /* Assume we'd insert a '/'. */
+ return len;
+}
+
+/* Join any number of strings together, putting them in "dest". The return
+ * value is the length of all the strings, regardless of whether the null-
+ * terminated whole fits in destsize. Your list of string pointers must end
+ * with a NULL to indicate the end of the list. */
+size_t stringjoin(char *dest, size_t destsize, ...)
+{
+ va_list ap;
+ size_t len, ret = 0;
+ const char *src;
+
+ va_start(ap, destsize);
+ while (1) {
+ if (!(src = va_arg(ap, const char *)))
+ break;
+ len = strlen(src);
+ ret += len;
+ if (destsize > 1) {
+ if (len >= destsize)
+ len = destsize - 1;
+ memcpy(dest, src, len);
+ destsize -= len;
+ dest += len;
+ }
+ }
+ *dest = '\0';
+ va_end(ap);
+
+ return ret;
+}
+
+int count_dir_elements(const char *p)
+{
+ int cnt = 0, new_component = 1;
+ while (*p) {
+ if (*p++ == '/')
+ new_component = (*p != '.' || (p[1] != '/' && p[1] != '\0'));
+ else if (new_component) {
+ new_component = 0;
+ cnt++;
+ }
+ }
+ return cnt;
+}