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);
99 * @brief get cwd in static buffer
101 * @returns pointer to path or pointer to error messages on error
103 const char *getcwdpath(void)
105 static char cwd[MAXPATHLEN + 1];
108 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
111 return strerror(errno);
115 * Make argument path absoulte
117 * @returns pointer to path or pointer to error messages on error
119 const char *abspath(const char *name)
121 static char buf[MAXPATHLEN + 1];
128 if ((p = getcwd(buf, MAXPATHLEN)) == NULL)
129 return strerror(errno);
135 strlcpy(buf + n, name, MAXPATHLEN - n);
140 * Takes a buffer with a path, strips slashs, returns basename
144 * "[/][dir/[...]]file"
146 * "[/][dir/[...]]dir/[/]"
147 * Result is "file" or "dir"
149 * @returns pointer to basename in path buffer, buffer is possibly modified
151 char *stripped_slashes_basename(char *p)
153 int i = strlen(p) - 1;
154 while (i > 0 && p[i] == '/')
156 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
160 * @brief symlink safe chdir replacement
162 * Only chdirs to dir if it doesn't contain symlinks.
164 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
166 int lchdir(const char *dir)
168 char buf[MAXPATHLEN+1];
169 char cwd[MAXPATHLEN+1];
174 dir is a canonical path (without "../" "./" "//" )
179 if (getcwd(cwd, MAXPATHLEN) == NULL)
187 * chdir request | realpath result | ret
188 * (after getwcwd) | |
189 * =======================================
192 * /a/b/. | /c/d/e/f | 1
194 if (getcwd(buf, MAXPATHLEN) == NULL)
199 /* relative path requested,
202 for (; cwd[i]; i++) {
203 if (buf[i] != cwd[i])
214 for (i = 0; test[i]; i++) {
215 if (test[i] != dir[i]) {
234 * Store n random bytes an buf
236 void randombytes(void *buf, int n)
238 char *p = (char *)buf;
242 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
243 /* generate from /dev/urandom */
244 if (read(fd, buf, n) != n) {
249 /* fd now != -1, so srandom wont be called below */
254 gettimeofday(&tv, NULL);
255 srandom((unsigned int)tv.tv_usec);
256 for (i=0 ; i < n ; i++)
257 p[i] = random() & 0xFF;