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