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