]> 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  * Make argument path absoulte
116  *
117  * @returns pointer to path or pointer to error messages on error
118  */
119 const char *abspath(const char *name)
120 {
121     static char buf[MAXPATHLEN + 1];
122     char *p;
123     int n;
124
125     if (name[0] == '/')
126         return name;
127
128     if ((p = getcwd(buf, MAXPATHLEN)) == NULL)
129         return strerror(errno);
130
131     n = strlen(buf);
132     if (buf[n-1] != '/')
133         buf[n++] = '/';
134         
135     strlcpy(buf + n, name, MAXPATHLEN - n);
136     return buf;
137 }
138
139 /*!
140  * Takes a buffer with a path, strips slashs, returns basename
141  *
142  * @param p (rw) path
143  *        path may be
144  *          "[/][dir/[...]]file"
145  *        or
146  *          "[/][dir/[...]]dir/[/]"
147  *        Result is "file" or "dir" 
148  *
149  * @returns pointer to basename in path buffer, buffer is possibly modified
150  */
151 char *stripped_slashes_basename(char *p)
152 {
153     int i = strlen(p) - 1;
154     while (i > 0 && p[i] == '/')
155         p[i--] = 0;
156     return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
157 }
158
159 /*!
160  * @brief symlink safe chdir replacement
161  *
162  * Only chdirs to dir if it doesn't contain symlinks.
163  *
164  * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
165  */
166 int lchdir(const char *dir)
167 {
168     char buf[MAXPATHLEN+1];
169     char cwd[MAXPATHLEN+1];
170     char *test;
171     int  i;
172
173     /*
174      dir is a canonical path (without "../" "./" "//" )
175      but may end with a / 
176     */
177     *cwd = 0;
178     if (*dir != '/') {
179         if (getcwd(cwd, MAXPATHLEN) == NULL)
180             return -1;
181     }
182     if (chdir(dir) != 0)
183         return -1;
184
185     /* 
186      * Cases:
187      * chdir request   | realpath result | ret
188      * (after getwcwd) |                 |
189      * =======================================
190      * /a/b/.          | /a/b            | 0
191      * /a/b/.          | /c              | 1
192      * /a/b/.          | /c/d/e/f        | 1
193      */
194     if (getcwd(buf, MAXPATHLEN) == NULL)
195         return 1;
196
197     i = 0;
198     if (*cwd) {
199         /* relative path requested, 
200          * Same directory?
201         */
202         for (; cwd[i]; i++) {
203             if (buf[i] != cwd[i])
204                 return 1;
205         }
206         if (buf[i]) {
207             if (buf[i] != '/')
208                 return 1;
209             i++;
210         }                    
211     }
212
213     test = &buf[i];    
214     for (i = 0; test[i]; i++) {
215         if (test[i] != dir[i]) {
216             return 1;
217         }
218     }
219     /* trailing '/' ? */
220     if (!dir[i])
221         return 0;
222
223     if (dir[i] != '/')
224         return 1;
225
226     i++;
227     if (dir[i])
228         return 1;
229
230     return 0;
231 }
232
233 /*!
234  * Store n random bytes an buf
235  */
236 void randombytes(void *buf, int n)
237 {
238     char *p = (char *)buf;
239     int fd, i;
240     struct timeval tv;
241
242     if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
243         /* generate from /dev/urandom */
244         if (read(fd, buf, n) != n) {
245             close(fd);
246             fd = -1;
247         } else {
248             close(fd);
249             /* fd now != -1, so srandom wont be called below */
250         }
251     }
252
253     if (fd == -1) {
254         gettimeofday(&tv, NULL);
255         srandom((unsigned int)tv.tv_usec);
256         for (i=0 ; i < n ; i++)
257             p[i] = random() & 0xFF;
258     }
259
260     return;
261 }