]> git.meshlink.io Git - meshlink/blob - src/dropin.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[meshlink] / src / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "xalloc.h"
23
24 #ifndef HAVE_DAEMON
25 /*
26   Replacement for the daemon() function.
27
28   The daemon() function is for programs wishing to detach themselves
29   from the controlling terminal and run in the background as system
30   daemons.
31
32   Unless the argument nochdir is non-zero, daemon() changes the
33   current working directory to the root (``/'').
34
35   Unless the argument noclose is non-zero, daemon() will redirect
36   standard input, standard output and standard error to /dev/null.
37 */
38 int daemon(int nochdir, int noclose) {
39 #ifdef HAVE_FORK
40         pid_t pid;
41         int fd;
42
43         pid = fork();
44
45         /* Check if forking failed */
46         if(pid < 0) {
47                 perror("fork");
48                 exit(-1);
49         }
50
51         /* If we are the parent, terminate */
52         if(pid)
53                 exit(0);
54
55         /* Detach by becoming the new process group leader */
56         if(setsid() < 0) {
57                 perror("setsid");
58                 return -1;
59         }
60
61         /* Change working directory to the root (to avoid keeping mount
62            points busy) */
63         if(!nochdir) {
64                 chdir("/");
65         }
66
67         /* Redirect stdin/out/err to /dev/null */
68         if(!noclose) {
69                 fd = open("/dev/null", O_RDWR);
70
71                 if(fd < 0) {
72                         perror("opening /dev/null");
73                         return -1;
74                 } else {
75                         dup2(fd, 0);
76                         dup2(fd, 1);
77                         dup2(fd, 2);
78                 }
79         }
80
81         return 0;
82 #else
83         return -1;
84 #endif
85 }
86 #endif
87
88 #ifndef HAVE_GET_CURRENT_DIR_NAME
89 /*
90   Replacement for the GNU get_current_dir_name function:
91
92   get_current_dir_name will malloc(3) an array big enough to hold the
93   current directory name.  If the environment variable PWD is set, and
94   its value is correct, then that value will be returned.
95 */
96 char *get_current_dir_name(void) {
97         size_t size;
98         char *buf;
99         char *r;
100
101         /* Start with 100 bytes.  If this turns out to be insufficient to
102            contain the working directory, double the size.  */
103         size = 100;
104         buf = xmalloc(size);
105
106         errno = 0;                                      /* Success */
107         r = getcwd(buf, size);
108
109         /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
110            is insufficient to contain the entire working directory.  */
111         while(r == NULL && errno == ERANGE) {
112                 free(buf);
113                 size <<= 1;                             /* double the size */
114                 buf = xmalloc(size);
115                 r = getcwd(buf, size);
116         }
117
118         return buf;
119 }
120 #endif
121
122 #ifndef HAVE_ASPRINTF
123 int asprintf(char **buf, const char *fmt, ...) {
124         int result;
125         va_list ap;
126         va_start(ap, fmt);
127         result = vasprintf(buf, fmt, ap);
128         va_end(ap);
129         return result;
130 }
131
132 int vasprintf(char **buf, const char *fmt, va_list ap) {
133         int status;
134         va_list aq;
135         int len;
136
137         len = 4096;
138         *buf = xmalloc(len);
139
140         va_copy(aq, ap);
141         status = vsnprintf(*buf, len, fmt, aq);
142         va_end(aq);
143
144         if(status >= 0)
145                 *buf = xrealloc(*buf, status + 1);
146
147         if(status > len - 1) {
148                 len = status;
149                 va_copy(aq, ap);
150                 status = vsnprintf(*buf, len, fmt, aq);
151                 va_end(aq);
152         }
153
154         return status;
155 }
156 #endif
157
158 #ifndef HAVE_GETTIMEOFDAY
159 int gettimeofday(struct timeval *tv, void *tz) {
160 #ifdef HAVE_MINGW
161         FILETIME ft;
162         GetSystemTimeAsFileTime(&ft);
163         uint64_t lt = (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32);
164         lt -= 116444736000000000ULL;
165         tv->tv_sec = lt / 10000000;
166         tv->tv_usec = (lt / 10) % 1000000;
167 #else
168 #warning No high resolution time source!
169         tv->tv_sec = time(NULL);
170         tv->tv_usec = 0;
171 #endif
172         return 0;
173 }
174 #endif
175
176 #ifndef HAVE_USLEEP
177 int usleep(long long usec) {
178         struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
179         select(0, NULL, NULL, NULL, &tv);
180         return 0;
181 }
182 #endif