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 * @brief Request absolute path
117 * @returns Absolute filesystem path to object
119 const char *fullpathname(const char *name)
121 static char wd[MAXPATHLEN + 1];
126 if (getcwd(wd , MAXPATHLEN)) {
127 strlcat(wd, "/", MAXPATHLEN);
128 strlcat(wd, name, MAXPATHLEN);
130 strlcpy(wd, name, MAXPATHLEN);
137 * Takes a buffer with a path, strips slashs, returns basename
141 * "[/][dir/[...]]file"
143 * "[/][dir/[...]]dir/[/]"
144 * Result is "file" or "dir"
146 * @returns pointer to basename in path buffer, buffer is possibly modified
148 char *stripped_slashes_basename(char *p)
150 int i = strlen(p) - 1;
151 while (i > 0 && p[i] == '/')
153 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
157 * @brief symlink safe chdir replacement
159 * Only chdirs to dir if it doesn't contain symlinks.
161 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
163 int lchdir(const char *dir)
165 char buf[MAXPATHLEN+1];
166 char cwd[MAXPATHLEN+1];
171 dir is a canonical path (without "../" "./" "//" )
176 if (getcwd(cwd, MAXPATHLEN) == NULL)
184 * chdir request | realpath result | ret
185 * (after getwcwd) | |
186 * =======================================
189 * /a/b/. | /c/d/e/f | 1
191 if (getcwd(buf, MAXPATHLEN) == NULL)
196 /* relative path requested,
199 for (; cwd[i]; i++) {
200 if (buf[i] != cwd[i])
211 for (i = 0; test[i]; i++) {
212 if (test[i] != dir[i]) {
231 * Store n random bytes an buf
233 void randombytes(void *buf, int n)
235 char *p = (char *)buf;
239 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
240 /* generate from /dev/urandom */
241 if (read(fd, buf, n) != n) {
246 /* fd now != -1, so srandom wont be called below */
251 gettimeofday(&tv, NULL);
252 srandom((unsigned int)tv.tv_usec);
253 for (i=0 ; i < n ; i++)
254 p[i] = random() & 0xFF;