X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Funix.c;h=e4cb21e8c12e95e828afb608430a73c8c034ebf8;hb=be13d9b759b7b9a1c200efa88d4c7067224d5ae6;hp=9e71fcb4b71a3b7fd53479c4cebc2cef7cb3bb30;hpb=5d553a87713c1f9568a9bb6b4a942bbec3802ad2;p=netatalk.git diff --git a/libatalk/util/unix.c b/libatalk/util/unix.c index 9e71fcb4..e4cb21e8 100644 --- a/libatalk/util/unix.c +++ b/libatalk/util/unix.c @@ -41,6 +41,75 @@ #include #include +/* close all FDs >= a specified value */ +static void closeall(int fd) +{ + int fdlimit = sysconf(_SC_OPEN_MAX); + + while (fd < fdlimit) + close(fd++); +} + +/*! + * 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; + +void become_root(void) +{ + saved_uid = geteuid(); + if (seteuid(0) != 0) + AFP_PANIC("Can't seteuid(0)"); +} + +void unbecome_root(void) +{ + 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 +126,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 *