]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/util/unix.c
Merge 2-2
[netatalk.git] / libatalk / util / unix.c
index 9e71fcb4b71a3b7fd53479c4cebc2cef7cb3bb30..56c3e168eaea05c05705464a0f21830aedf60c03 100644 (file)
@@ -32,6 +32,7 @@
 #include <dirent.h>
 #include <sys/time.h>
 #include <time.h>
+#include <sys/wait.h>
 
 #include <atalk/adouble.h>
 #include <atalk/ea.h>
 #include <atalk/vfs.h>
 #include <atalk/util.h>
 #include <atalk/unix.h>
+#include <atalk/compat.h>
+#include <atalk/errchk.h>
+
+/* close all FDs >= a specified value */
+static void closeall(int fd)
+{
+    int fdlimit = sysconf(_SC_OPEN_MAX);
+
+    while (fd < fdlimit)
+        close(fd++);
+}
+
+/*!
+ * Run command in a child and wait for it to finish
+ */
+int run_cmd(const char *cmd, char **cmd_argv)
+{
+    EC_INIT;
+    pid_t pid, wpid;
+    sigset_t sigs, oldsigs;
+       int status = 0;
+
+    sigfillset(&sigs);
+    pthread_sigmask(SIG_SETMASK, &sigs, &oldsigs);
+
+    if ((pid = fork()) < 0) {
+        LOG(log_error, logtype_default, "run_cmd: fork: %s", strerror(errno));
+        return -1;
+    }
+
+    if (pid == 0) {
+        /* child */
+        closeall(3);
+        execvp("mv", cmd_argv);
+    }
+
+    /* parent */
+       while ((wpid = waitpid(pid, &status, 0)) < 0) {
+           if (errno == EINTR)
+            continue;
+           break;
+       }
+       if (wpid != pid) {
+           LOG(log_error, logtype_default, "waitpid(%d): %s", (int)pid, strerror(errno));
+        EC_FAIL;
+       }
+
+    if (WIFEXITED(status))
+        status = WEXITSTATUS(status);
+    else if (WIFSIGNALED(status))
+        status = WTERMSIG(status);
+
+    LOG(log_note, logtype_default, "run_cmd(\"%s\"): status: %d", cmd, status);
+
+EC_CLEANUP:
+    if (status != 0)
+        ret = status;
+    pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
+    EC_EXIT;
+}
+
+/*!
+ * Daemonize
+ *
+ * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
+ *
+ * returns -1 on failure, but you can't do much except exit in that case
+ * since we may already have forked
+ */
+int daemonize(int nochdir, int noclose)
+{
+    switch (fork()) {
+    case 0:
+        break;
+    case -1:
+        return -1;
+    default:
+        _exit(0);
+    }
+
+    if (setsid() < 0)
+        return -1;
+
+    switch (fork()) {
+    case 0: 
+        break;
+    case -1:
+        return -1;
+    default:
+        _exit(0);
+    }
+
+    if (!nochdir)
+        chdir("/");
+
+    if (!noclose) {
+        closeall(0);
+        open("/dev/null",O_RDWR);
+        dup(0);
+        dup(0);
+    }
+
+    return 0;
+}
+
+static uid_t saved_uid = -1;
+
+/*
+ * seteuid(0) and back, if either fails and panic != 0 we PANIC
+ */
+void become_root(void)
+{
+    if (getuid() == 0) {
+        saved_uid = geteuid();
+        if (seteuid(0) != 0)
+            AFP_PANIC("Can't seteuid(0)");
+    }
+}
+
+void unbecome_root(void)
+{
+    if (getuid() == 0) {
+        if (saved_uid == -1 || seteuid(saved_uid) < 0)
+            AFP_PANIC("Can't seteuid back");
+        saved_uid = -1;
+    }
+}
 
 /*!
  * @brief get cwd in static buffer
@@ -57,6 +185,28 @@ const char *getcwdpath(void)
         return strerror(errno);
 }
 
+/*!
+ * @brief Request absolute path
+ *
+ * @returns Absolute filesystem path to object
+ */
+const char *fullpathname(const char *name)
+{
+    static char wd[MAXPATHLEN + 1];
+
+    if (name[0] == '/')
+        return name;
+
+    if (getcwd(wd , MAXPATHLEN)) {
+        strlcat(wd, "/", MAXPATHLEN);
+        strlcat(wd, name, MAXPATHLEN);
+    } else {
+        strlcpy(wd, name, MAXPATHLEN);
+    }
+
+    return wd;
+}
+
 /*!
  * Takes a buffer with a path, strips slashs, returns basename
  *
@@ -180,3 +330,15 @@ void randombytes(void *buf, int n)
 
     return;
 }
+
+int gmem(gid_t gid, int ngroups, gid_t *groups)
+{
+    int                i;
+
+    for ( i = 0; i < ngroups; i++ ) {
+        if ( groups[ i ] == gid ) {
+            return( 1 );
+        }
+    }
+    return( 0 );
+}