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);
98 * @brief get cwd in static buffer
100 * @returns pointer to path or pointer to error messages on error
102 const char *getcwdpath(void)
104 static char cwd[MAXPATHLEN + 1];
107 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
110 return strerror(errno);
114 * @brief Request absolute path
116 * @returns Absolute filesystem path to object
118 const char *fullpathname(const char *name)
120 static char wd[MAXPATHLEN + 1];
125 if (getcwd(wd , MAXPATHLEN)) {
126 strlcat(wd, "/", MAXPATHLEN);
127 strlcat(wd, name, MAXPATHLEN);
129 strlcpy(wd, name, MAXPATHLEN);
136 * Takes a buffer with a path, strips slashs, returns basename
140 * "[/][dir/[...]]file"
142 * "[/][dir/[...]]dir/[/]"
143 * Result is "file" or "dir"
145 * @returns pointer to basename in path buffer, buffer is possibly modified
147 char *stripped_slashes_basename(char *p)
149 int i = strlen(p) - 1;
150 while (i > 0 && p[i] == '/')
152 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
156 * @brief symlink safe chdir replacement
158 * Only chdirs to dir if it doesn't contain symlinks.
160 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
162 int lchdir(const char *dir)
164 char buf[MAXPATHLEN+1];
165 char cwd[MAXPATHLEN+1];
170 dir is a canonical path (without "../" "./" "//" )
175 if (getcwd(cwd, MAXPATHLEN) == NULL)
183 * chdir request | realpath result | ret
184 * (after getwcwd) | |
185 * =======================================
188 * /a/b/. | /c/d/e/f | 1
190 if (getcwd(buf, MAXPATHLEN) == NULL)
195 /* relative path requested,
198 for (; cwd[i]; i++) {
199 if (buf[i] != cwd[i])
210 for (i = 0; test[i]; i++) {
211 if (test[i] != dir[i]) {
230 * Store n random bytes an buf
232 void randombytes(void *buf, int n)
234 char *p = (char *)buf;
238 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
239 /* generate from /dev/urandom */
240 if (read(fd, buf, n) != n) {
245 /* fd now != -1, so srandom wont be called below */
250 gettimeofday(&tv, NULL);
251 srandom((unsigned int)tv.tv_usec);
252 for (i=0 ; i < n ; i++)
253 p[i] = random() & 0xFF;