#include <dirent.h>
#include <sys/time.h>
#include <time.h>
+#include <sys/wait.h>
+#include <libgen.h>
#include <atalk/adouble.h>
#include <atalk/ea.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)
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
*
}
return( 0 );
}
+
+/*
+ * realpath() replacement that always allocates storage for returned path
+ */
+char *realpath_safe(const char *path)
+{
+ char *resolved_path;
+
+#ifdef REALPATH_TAKES_NULL
+ if ((resolved_path = realpath(path, NULL)) == NULL) {
+ LOG(log_error, logtype_afpd, "realpath() cannot resolve path \"%s\"", path);
+ return NULL;
+ }
+ return resolved_path;
+#else
+ if ((resolved_path = malloc(MAXPATHLEN+1)) == NULL)
+ return NULL;
+ if (realpath(path, resolved_path) == NULL) {
+ free(resolved_path);
+ LOG(log_error, logtype_afpd, "realpath() cannot resolve path \"%s\"", path);
+ return NULL;
+ }
+ /* Safe some memory */
+ char *tmp;
+ if ((tmp = strdup(resolved_path)) == NULL) {
+ free(resolved_path);
+ return NULL;
+ }
+ free(resolved_path);
+ resolved_path = tmp;
+ return resolved_path;
+#endif
+}
+
+/**
+ * Returns pointer to static buffer with basename of path
+ **/
+const char *basename_safe(const char *path)
+{
+ static char buf[MAXPATHLEN+1];
+ strlcpy(buf, path, MAXPATHLEN);
+ return basename(buf);
+}