blob: 06420558035a8acdcfa5452650f77085b0841359 [file] [log] [blame]
The Android Open Source Projectf7c54212009-03-03 19:29:22 -08001/*
2 * dhcpcd - DHCP client daemon
Dmitry Shmidte86eee12011-01-24 16:27:51 -08003 * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -08004 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
Dmitry Shmidte86eee12011-01-24 16:27:51 -080028/* Needed define to get at getline for glibc and FreeBSD */
29#ifndef _GNU_SOURCE
30# define _GNU_SOURCE
31#endif
32
33#include <sys/cdefs.h>
34
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080035#ifdef __APPLE__
36# include <mach/mach_time.h>
37# include <mach/kern_return.h>
38#endif
39
40#include <sys/param.h>
41#include <sys/time.h>
42
43#include <errno.h>
44#include <fcntl.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080045#include <limits.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080046#ifdef BSD
47# include <paths.h>
48#endif
49#include <stdint.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080053#include <syslog.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080054#include <time.h>
55#include <unistd.h>
56
57#include "common.h"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080058
59#ifndef _PATH_DEVNULL
60# define _PATH_DEVNULL "/dev/null"
61#endif
62
Dmitry Shmidte86eee12011-01-24 16:27:51 -080063int clock_monotonic;
64static char *lbuf;
65static size_t lbuf_len;
66#ifdef DEBUG_MEMORY
67static char lbuf_set;
68#endif
69
70#ifdef DEBUG_MEMORY
71static void
72free_lbuf(void)
73{
74 free(lbuf);
75 lbuf = NULL;
76}
77#endif
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080078
79/* Handy routine to read very long lines in text files.
Dmitry Shmidte86eee12011-01-24 16:27:51 -080080 * This means we read the whole line and avoid any nasty buffer overflows.
81 * We strip leading space and avoid comment lines, making the code that calls
82 * us smaller.
83 * As we don't use threads, this API is clean too. */
84char *
85get_line(FILE * __restrict fp)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080086{
87 char *p;
Dmitry Shmidte86eee12011-01-24 16:27:51 -080088 ssize_t bytes;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080089
Dmitry Shmidte86eee12011-01-24 16:27:51 -080090#ifdef DEBUG_MEMORY
91 if (lbuf_set == 0) {
92 atexit(free_lbuf);
93 lbuf_set = 1;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080094 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080095#endif
96
Dmitry Shmidte86eee12011-01-24 16:27:51 -080097 do {
98 bytes = getline(&lbuf, &lbuf_len, fp);
99 if (bytes == -1)
100 return NULL;
101 for (p = lbuf; *p == ' ' || *p == '\t'; p++)
102 ;
103 } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
104 if (lbuf[--bytes] == '\n')
105 lbuf[bytes] = '\0';
106 return p;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800107}
108
109int
110set_cloexec(int fd)
111{
112 int flags;
113
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800114 if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
115 fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800116 {
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800117 syslog(LOG_ERR, "fcntl: %m");
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800118 return -1;
119 }
120 return 0;
121}
122
123int
124set_nonblock(int fd)
125{
126 int flags;
127
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800128 if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
129 fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800130 {
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800131 syslog(LOG_ERR, "fcntl: %m");
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800132 return -1;
133 }
134 return 0;
135}
136
137/* Handy function to get the time.
138 * We only care about time advancements, not the actual time itself
139 * Which is why we use CLOCK_MONOTONIC, but it is not available on all
140 * platforms.
141 */
142#define NO_MONOTONIC "host does not support a monotonic clock - timing can skew"
143int
144get_monotonic(struct timeval *tp)
145{
146 static int posix_clock_set = 0;
147#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)
148 struct timespec ts;
149 static clockid_t posix_clock;
150
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800151 if (!posix_clock_set) {
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800152 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
153 posix_clock = CLOCK_MONOTONIC;
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800154 clock_monotonic = posix_clock_set = 1;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800155 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800156 }
157
158 if (clock_monotonic) {
159 if (clock_gettime(posix_clock, &ts) == 0) {
160 tp->tv_sec = ts.tv_sec;
161 tp->tv_usec = ts.tv_nsec / 1000;
162 return 0;
163 }
164 }
165#elif defined(__APPLE__)
166#define NSEC_PER_SEC 1000000000
167 /* We can use mach kernel functions here.
168 * This is crap though - why can't they implement clock_gettime?*/
169 static struct mach_timebase_info info = { 0, 0 };
170 static double factor = 0.0;
171 uint64_t nano;
172 long rem;
173
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800174 if (!posix_clock_set) {
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800175 if (mach_timebase_info(&info) == KERN_SUCCESS) {
176 factor = (double)info.numer / (double)info.denom;
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800177 clock_monotonic = posix_clock_set = 1;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800178 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800179 }
180 if (clock_monotonic) {
181 nano = mach_absolute_time();
182 if ((info.denom != 1 || info.numer != 1) && factor != 0.0)
183 nano *= factor;
184 tp->tv_sec = nano / NSEC_PER_SEC;
185 rem = nano % NSEC_PER_SEC;
186 if (rem < 0) {
187 tp->tv_sec--;
188 rem += NSEC_PER_SEC;
189 }
190 tp->tv_usec = rem / 1000;
191 return 0;
192 }
193#endif
194
195 /* Something above failed, so fall back to gettimeofday */
196 if (!posix_clock_set) {
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800197 syslog(LOG_WARNING, NO_MONOTONIC);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800198 posix_clock_set = 1;
199 }
200 return gettimeofday(tp, NULL);
201}
202
203time_t
204uptime(void)
205{
206 struct timeval tv;
207
208 if (get_monotonic(&tv) == -1)
209 return -1;
210 return tv.tv_sec;
211}
212
213int
214writepid(int fd, pid_t pid)
215{
216 char spid[16];
217 ssize_t len;
218
219 if (ftruncate(fd, (off_t)0) == -1)
220 return -1;
221 snprintf(spid, sizeof(spid), "%u\n", pid);
222 len = pwrite(fd, spid, strlen(spid), (off_t)0);
223 if (len != (ssize_t)strlen(spid))
224 return -1;
225 return 0;
226}
227
228void *
229xmalloc(size_t s)
230{
231 void *value = malloc(s);
232
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800233 if (value != NULL)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800234 return value;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800235 syslog(LOG_ERR, "memory exhausted (xalloc %zu bytes)", s);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800236 exit (EXIT_FAILURE);
237 /* NOTREACHED */
238}
239
240void *
241xzalloc(size_t s)
242{
243 void *value = xmalloc(s);
244
245 memset(value, 0, s);
246 return value;
247}
248
249void *
250xrealloc(void *ptr, size_t s)
251{
252 void *value = realloc(ptr, s);
253
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800254 if (value != NULL)
255 return value;
256 syslog(LOG_ERR, "memory exhausted (xrealloc %zu bytes)", s);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800257 exit(EXIT_FAILURE);
258 /* NOTREACHED */
259}
260
261char *
262xstrdup(const char *str)
263{
264 char *value;
265
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800266 if (str == NULL)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800267 return NULL;
268
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800269 if ((value = strdup(str)) != NULL)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800270 return value;
271
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800272 syslog(LOG_ERR, "memory exhausted (xstrdup)");
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800273 exit(EXIT_FAILURE);
274 /* NOTREACHED */
275}