]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/unix.c
Merge master
[netatalk.git] / libatalk / util / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 /*!
16  * @file
17  * Netatalk utility functions
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <sys/time.h>
34 #include <time.h>
35
36 #include <atalk/adouble.h>
37 #include <atalk/ea.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>
44
45 /* close all FDs >= a specified value */
46 static void closeall(int fd)
47 {
48     int fdlimit = sysconf(_SC_OPEN_MAX);
49
50     while (fd < fdlimit)
51         close(fd++);
52 }
53
54 /*!
55  * Daemonize
56  *
57  * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
58  *
59  * returns -1 on failure, but you can't do much except exit in that case
60  * since we may already have forked
61  */
62 int daemonize(int nochdir, int noclose)
63 {
64     switch (fork()) {
65     case 0:
66         break;
67     case -1:
68         return -1;
69     default:
70         _exit(0);
71     }
72
73     if (setsid() < 0)
74         return -1;
75
76     switch (fork()) {
77     case 0: 
78         break;
79     case -1:
80         return -1;
81     default:
82         _exit(0);
83     }
84
85     if (!nochdir)
86         chdir("/");
87
88     if (!noclose) {
89         closeall(0);
90         open("/dev/null",O_RDWR);
91         dup(0);
92         dup(0);
93     }
94
95     return 0;
96 }
97
98 /*!
99  * @brief get cwd in static buffer
100  *
101  * @returns pointer to path or pointer to error messages on error
102  */
103 const char *getcwdpath(void)
104 {
105     static char cwd[MAXPATHLEN + 1];
106     char *p;
107
108     if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
109         return p;
110     else
111         return strerror(errno);
112 }
113
114 /*!
115  * @brief Request absolute path
116  *
117  * @returns Absolute filesystem path to object
118  */
119 const char *fullpathname(const char *name)
120 {
121     static char wd[MAXPATHLEN + 1];
122
123     if (name[0] == '/')
124         return name;
125
126     if (getcwd(wd , MAXPATHLEN)) {
127         strlcat(wd, "/", MAXPATHLEN);
128         strlcat(wd, name, MAXPATHLEN);
129     } else {
130         strlcpy(wd, name, MAXPATHLEN);
131     }
132
133     return wd;
134 }
135
136 /*!
137  * Takes a buffer with a path, strips slashs, returns basename
138  *
139  * @param p (rw) path
140  *        path may be
141  *          "[/][dir/[...]]file"
142  *        or
143  *          "[/][dir/[...]]dir/[/]"
144  *        Result is "file" or "dir" 
145  *
146  * @returns pointer to basename in path buffer, buffer is possibly modified
147  */
148 char *stripped_slashes_basename(char *p)
149 {
150     int i = strlen(p) - 1;
151     while (i > 0 && p[i] == '/')
152         p[i--] = 0;
153     return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
154 }
155
156 /*!
157  * @brief symlink safe chdir replacement
158  *
159  * Only chdirs to dir if it doesn't contain symlinks.
160  *
161  * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
162  */
163 int lchdir(const char *dir)
164 {
165     char buf[MAXPATHLEN+1];
166     char cwd[MAXPATHLEN+1];
167     char *test;
168     int  i;
169
170     /*
171      dir is a canonical path (without "../" "./" "//" )
172      but may end with a / 
173     */
174     *cwd = 0;
175     if (*dir != '/') {
176         if (getcwd(cwd, MAXPATHLEN) == NULL)
177             return -1;
178     }
179     if (chdir(dir) != 0)
180         return -1;
181
182     /* 
183      * Cases:
184      * chdir request   | realpath result | ret
185      * (after getwcwd) |                 |
186      * =======================================
187      * /a/b/.          | /a/b            | 0
188      * /a/b/.          | /c              | 1
189      * /a/b/.          | /c/d/e/f        | 1
190      */
191     if (getcwd(buf, MAXPATHLEN) == NULL)
192         return 1;
193
194     i = 0;
195     if (*cwd) {
196         /* relative path requested, 
197          * Same directory?
198         */
199         for (; cwd[i]; i++) {
200             if (buf[i] != cwd[i])
201                 return 1;
202         }
203         if (buf[i]) {
204             if (buf[i] != '/')
205                 return 1;
206             i++;
207         }                    
208     }
209
210     test = &buf[i];    
211     for (i = 0; test[i]; i++) {
212         if (test[i] != dir[i]) {
213             return 1;
214         }
215     }
216     /* trailing '/' ? */
217     if (!dir[i])
218         return 0;
219
220     if (dir[i] != '/')
221         return 1;
222
223     i++;
224     if (dir[i])
225         return 1;
226
227     return 0;
228 }
229
230 /*!
231  * Store n random bytes an buf
232  */
233 void randombytes(void *buf, int n)
234 {
235     char *p = (char *)buf;
236     int fd, i;
237     struct timeval tv;
238
239     if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
240         /* generate from /dev/urandom */
241         if (read(fd, buf, n) != n) {
242             close(fd);
243             fd = -1;
244         } else {
245             close(fd);
246             /* fd now != -1, so srandom wont be called below */
247         }
248     }
249
250     if (fd == -1) {
251         gettimeofday(&tv, NULL);
252         srandom((unsigned int)tv.tv_usec);
253         for (i=0 ; i < n ; i++)
254             p[i] = random() & 0xFF;
255     }
256
257     return;
258 }