2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Netatalk utility functions
22 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
37 #include <atalk/adouble.h>
39 #include <atalk/afp.h>
40 #include <atalk/logger.h>
41 #include <atalk/vfs.h>
42 #include <atalk/util.h>
43 #include <atalk/unix.h>
44 #include <atalk/compat.h>
45 #include <atalk/errchk.h>
47 /* close all FDs >= a specified value */
48 static void closeall(int fd)
50 int fdlimit = sysconf(_SC_OPEN_MAX);
57 * Run command in a child and wait for it to finish
59 int run_cmd(const char *cmd, char **cmd_argv)
63 sigset_t sigs, oldsigs;
67 pthread_sigmask(SIG_SETMASK, &sigs, &oldsigs);
69 if ((pid = fork()) < 0) {
70 LOG(log_error, logtype_default, "run_cmd: fork: %s", strerror(errno));
77 execvp("mv", cmd_argv);
81 while ((wpid = waitpid(pid, &status, 0)) < 0) {
87 LOG(log_error, logtype_default, "waitpid(%d): %s", (int)pid, strerror(errno));
91 if (WIFEXITED(status))
92 status = WEXITSTATUS(status);
93 else if (WIFSIGNALED(status))
94 status = WTERMSIG(status);
96 LOG(log_note, logtype_default, "run_cmd(\"%s\"): status: %d", cmd, status);
101 pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
108 * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
110 * returns -1 on failure, but you can't do much except exit in that case
111 * since we may already have forked
113 int daemonize(int nochdir, int noclose)
141 open("/dev/null",O_RDWR);
149 static uid_t saved_uid = -1;
152 * seteuid(0) and back, if either fails and panic != 0 we PANIC
154 void become_root(void)
157 saved_uid = geteuid();
159 AFP_PANIC("Can't seteuid(0)");
163 void unbecome_root(void)
166 if (saved_uid == -1 || seteuid(saved_uid) < 0)
167 AFP_PANIC("Can't seteuid back");
173 * @brief get cwd in static buffer
175 * @returns pointer to path or pointer to error messages on error
177 const char *getcwdpath(void)
179 static char cwd[MAXPATHLEN + 1];
182 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
185 return strerror(errno);
189 * @brief Request absolute path
191 * @returns Absolute filesystem path to object
193 const char *fullpathname(const char *name)
195 static char wd[MAXPATHLEN + 1];
200 if (getcwd(wd , MAXPATHLEN)) {
201 strlcat(wd, "/", MAXPATHLEN);
202 strlcat(wd, name, MAXPATHLEN);
204 strlcpy(wd, name, MAXPATHLEN);
211 * Takes a buffer with a path, strips slashs, returns basename
215 * "[/][dir/[...]]file"
217 * "[/][dir/[...]]dir/[/]"
218 * Result is "file" or "dir"
220 * @returns pointer to basename in path buffer, buffer is possibly modified
222 char *stripped_slashes_basename(char *p)
224 int i = strlen(p) - 1;
225 while (i > 0 && p[i] == '/')
227 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
231 * @brief symlink safe chdir replacement
233 * Only chdirs to dir if it doesn't contain symlinks.
235 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
237 int lchdir(const char *dir)
239 char buf[MAXPATHLEN+1];
240 char cwd[MAXPATHLEN+1];
245 dir is a canonical path (without "../" "./" "//" )
250 if (getcwd(cwd, MAXPATHLEN) == NULL)
258 * chdir request | realpath result | ret
259 * (after getwcwd) | |
260 * =======================================
263 * /a/b/. | /c/d/e/f | 1
265 if (getcwd(buf, MAXPATHLEN) == NULL)
270 /* relative path requested,
273 for (; cwd[i]; i++) {
274 if (buf[i] != cwd[i])
285 for (i = 0; test[i]; i++) {
286 if (test[i] != dir[i]) {
305 * Store n random bytes an buf
307 void randombytes(void *buf, int n)
309 char *p = (char *)buf;
313 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
314 /* generate from /dev/urandom */
315 if (read(fd, buf, n) != n) {
320 /* fd now != -1, so srandom wont be called below */
325 gettimeofday(&tv, NULL);
326 srandom((unsigned int)tv.tv_usec);
327 for (i=0 ; i < n ; i++)
328 p[i] = random() & 0xFF;
334 int gmem(gid_t gid, int ngroups, gid_t *groups)
338 for ( i = 0; i < ngroups; i++ ) {
339 if ( groups[ i ] == gid ) {