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>
43 #include <atalk/compat.h>
45 /* close all FDs >= a specified value */
46 static void closeall(int fd)
48 int fdlimit = sysconf(_SC_OPEN_MAX);
57 * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
59 * returns -1 on failure, but you can't do much except exit in that case
60 * since we may already have forked
62 int daemonize(int nochdir, int noclose)
90 open("/dev/null",O_RDWR);
98 static uid_t saved_uid = -1;
100 void become_root(void)
102 saved_uid = geteuid();
104 AFP_PANIC("Can't seteuid(0)");
107 void unbecome_root(void)
109 if (saved_uid == -1 || seteuid(saved_uid) < 0)
110 AFP_PANIC("Can't seteuid back");
115 * @brief get cwd in static buffer
117 * @returns pointer to path or pointer to error messages on error
119 const char *getcwdpath(void)
121 static char cwd[MAXPATHLEN + 1];
124 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
127 return strerror(errno);
131 * @brief Request absolute path
133 * @returns Absolute filesystem path to object
135 const char *fullpathname(const char *name)
137 static char wd[MAXPATHLEN + 1];
142 if (getcwd(wd , MAXPATHLEN)) {
143 strlcat(wd, "/", MAXPATHLEN);
144 strlcat(wd, name, MAXPATHLEN);
146 strlcpy(wd, name, MAXPATHLEN);
153 * Takes a buffer with a path, strips slashs, returns basename
157 * "[/][dir/[...]]file"
159 * "[/][dir/[...]]dir/[/]"
160 * Result is "file" or "dir"
162 * @returns pointer to basename in path buffer, buffer is possibly modified
164 char *stripped_slashes_basename(char *p)
166 int i = strlen(p) - 1;
167 while (i > 0 && p[i] == '/')
169 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
173 * @brief symlink safe chdir replacement
175 * Only chdirs to dir if it doesn't contain symlinks.
177 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
179 int lchdir(const char *dir)
181 char buf[MAXPATHLEN+1];
182 char cwd[MAXPATHLEN+1];
187 dir is a canonical path (without "../" "./" "//" )
192 if (getcwd(cwd, MAXPATHLEN) == NULL)
200 * chdir request | realpath result | ret
201 * (after getwcwd) | |
202 * =======================================
205 * /a/b/. | /c/d/e/f | 1
207 if (getcwd(buf, MAXPATHLEN) == NULL)
212 /* relative path requested,
215 for (; cwd[i]; i++) {
216 if (buf[i] != cwd[i])
227 for (i = 0; test[i]; i++) {
228 if (test[i] != dir[i]) {
247 * Store n random bytes an buf
249 void randombytes(void *buf, int n)
251 char *p = (char *)buf;
255 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
256 /* generate from /dev/urandom */
257 if (read(fd, buf, n) != n) {
262 /* fd now != -1, so srandom wont be called below */
267 gettimeofday(&tv, NULL);
268 srandom((unsigned int)tv.tv_usec);
269 for (i=0 ; i < n ; i++)
270 p[i] = random() & 0xFF;