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>
36 #include <atalk/adouble.h>
38 #include <atalk/afp.h>
39 #include <atalk/logger.h>
40 #include <atalk/vfs.h>
41 #include <atalk/util.h>
42 #include <atalk/unix.h>
44 /* close all FDs >= a specified value */
45 static void closeall(int fd)
47 int fdlimit = sysconf(_SC_OPEN_MAX);
56 * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
58 * returns -1 on failure, but you can't do much except exit in that case
59 * since we may already have forked
61 int daemonize(int nochdir, int noclose)
89 open("/dev/null",O_RDWR);
97 static uid_t saved_uid = -1;
99 void become_root(void)
101 saved_uid = geteuid();
103 AFP_PANIC("Can't seteuid(0)");
106 void unbecome_root(void)
108 if (saved_uid == -1 || seteuid(saved_uid) < 0)
109 AFP_PANIC("Can't seteuid back");
114 * @brief get cwd in static buffer
116 * @returns pointer to path or pointer to error messages on error
118 const char *getcwdpath(void)
120 static char cwd[MAXPATHLEN + 1];
123 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
126 return strerror(errno);
130 * @brief Request absolute path
132 * @returns Absolute filesystem path to object
134 const char *fullpathname(const char *name)
136 static char wd[MAXPATHLEN + 1];
141 if (getcwd(wd , MAXPATHLEN)) {
142 strlcat(wd, "/", MAXPATHLEN);
143 strlcat(wd, name, MAXPATHLEN);
145 strlcpy(wd, name, MAXPATHLEN);
152 * Takes a buffer with a path, strips slashs, returns basename
156 * "[/][dir/[...]]file"
158 * "[/][dir/[...]]dir/[/]"
159 * Result is "file" or "dir"
161 * @returns pointer to basename in path buffer, buffer is possibly modified
163 char *stripped_slashes_basename(char *p)
165 int i = strlen(p) - 1;
166 while (i > 0 && p[i] == '/')
168 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
172 * @brief symlink safe chdir replacement
174 * Only chdirs to dir if it doesn't contain symlinks.
176 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
178 int lchdir(const char *dir)
180 char buf[MAXPATHLEN+1];
181 char cwd[MAXPATHLEN+1];
186 dir is a canonical path (without "../" "./" "//" )
191 if (getcwd(cwd, MAXPATHLEN) == NULL)
199 * chdir request | realpath result | ret
200 * (after getwcwd) | |
201 * =======================================
204 * /a/b/. | /c/d/e/f | 1
206 if (getcwd(buf, MAXPATHLEN) == NULL)
211 /* relative path requested,
214 for (; cwd[i]; i++) {
215 if (buf[i] != cwd[i])
226 for (i = 0; test[i]; i++) {
227 if (test[i] != dir[i]) {
246 * Store n random bytes an buf
248 void randombytes(void *buf, int n)
250 char *p = (char *)buf;
254 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
255 /* generate from /dev/urandom */
256 if (read(fd, buf, n) != n) {
261 /* fd now != -1, so srandom wont be called below */
266 gettimeofday(&tv, NULL);
267 srandom((unsigned int)tv.tv_usec);
268 for (i=0 ; i < n ; i++)
269 p[i] = random() & 0xFF;