summaryrefslogtreecommitdiffstats
path: root/customdhcpcd/src/common.c
blob: 99471bce22423607ff021c2b08e8be68347206f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* 
 * dhcpcd - DHCP client daemon
 * Copyright 2006-2008 Roy Marples <roy@marples.name>
 * All rights reserved

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "common.h"
#include "logger.h"

/* Handy routine to read very long lines in text files.
 * This means we read the whole line and avoid any nasty buffer overflows. */
char *get_line (FILE *fp)
{
	char *line = NULL;
	char *p;
	size_t len = 0;
	size_t last = 0;

	if (feof (fp))
		return (NULL);

	do {
		len += BUFSIZ;
		line = xrealloc (line, sizeof (char) * len);
		p = line + last;
		memset (p, 0, BUFSIZ);
		fgets (p, BUFSIZ, fp);
		last += strlen (p);
	} while (! feof (fp) && line[last - 1] != '\n');

	/* Trim the trailing newline */
	if (*line && line[--last] == '\n')
		line[last] = '\0';

	return (line);
}

/* OK, this should be in dhcpcd.c
 * It's here to make dhcpcd more readable */
#ifndef HAVE_SRANDOMDEV
void srandomdev (void)
{
	int fd;
	unsigned long seed;

	fd = open ("/dev/urandom", 0);
	if (fd == -1 || read (fd,  &seed, sizeof (seed)) == -1) {
		logger (LOG_WARNING, "Could not read from /dev/urandom: %s",
			strerror (errno));
		seed = time (0);
	}
	if (fd >= 0)
		close(fd);

	srandom (seed);
}
#endif

/* strlcpy is nice, shame glibc does not define it */
#ifndef HAVE_STRLCPY
size_t strlcpy (char *dst, const char *src, size_t size)
{
	const char *s = src;
	size_t n = size;

	if (n && --n)
		do {
			if (! (*dst++ = *src++))
				break;
		} while (--n);

	if (! n) {
		if (size)
			*dst = '\0';
		while (*src++);
	}

	return (src - s - 1);
}
#endif

/* Close our fd's */
int close_fds (void)
{
	int fd;

	if ((fd = open ("/dev/null", O_RDWR)) == -1) {
		logger (LOG_ERR, "open `/dev/null': %s", strerror (errno));
		return (-1);
	}

	dup2 (fd, fileno (stdin));
	dup2 (fd, fileno (stdout));
	dup2 (fd, fileno (stderr));
	if (fd > 2)
		close (fd);
	return (0);
}

int close_on_exec (int fd)
{
	int flags;

	if ((flags = fcntl (fd, F_GETFD, 0)) == -1
	    || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
	{
		logger (LOG_ERR, "fcntl: %s", strerror (errno));
		return (-1);
	}
	return (0);
}

/* Handy function to get the time.
 * We only care about time advancements, not the actual time itself
 * Which is why we use CLOCK_MONOTONIC, but it is not available on all
 * platforms.
 */
int get_time (struct timeval *tp)
{
#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)
	struct timespec ts;
	static clockid_t posix_clock;
	static int posix_clock_set = 0;

	if (! posix_clock_set) {
		if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
			posix_clock = CLOCK_MONOTONIC;
		else
			posix_clock = CLOCK_REALTIME;
		posix_clock_set = 1;
	}

	if (clock_gettime (posix_clock, &ts) == -1) {
		logger (LOG_ERR, "clock_gettime: %s", strerror (errno));
		return (-1);
	}

	tp->tv_sec = ts.tv_sec;
	tp->tv_usec = ts.tv_nsec / 1000;
	return (0);
#else
	if (gettimeofday (tp, NULL) == -1) {
		logger (LOG_ERR, "gettimeofday: %s", strerror (errno));
		return (-1);
	}
	return (0);
#endif
}

time_t uptime (void)
{
	struct timeval tp;

	if (get_time (&tp) == -1)
		return (-1);

	return (tp.tv_sec);
}

void writepid (int fd, pid_t pid)
{
	char spid[16];
	if (ftruncate (fd, (off_t) 0) == -1) {
		logger (LOG_ERR, "ftruncate: %s", strerror (errno));
	} else {
		ssize_t len;
		snprintf (spid, sizeof (spid), "%u", pid);
		len = pwrite (fd, spid, strlen (spid), (off_t) 0);
		if (len != (ssize_t) strlen (spid))
			logger (LOG_ERR, "pwrite: %s", strerror (errno));
	}
}

void *xmalloc (size_t s)
{
	void *value = malloc (s);

	if (value)
		return (value);

	logger (LOG_ERR, "memory exhausted");

	exit (EXIT_FAILURE);
	/* NOTREACHED */
}

void *xzalloc (size_t s)
{
	void *value = xmalloc (s);
	memset (value, 0, s);
	return (value);
}

void *xrealloc (void *ptr, size_t s)
{
	void *value = realloc (ptr, s);

	if (value)
		return (value);

	logger (LOG_ERR, "memory exhausted");
	exit (EXIT_FAILURE);
	/* NOTREACHED */
}

char *xstrdup (const char *str)
{
	char *value;

	if (! str)
		return (NULL);

	if ((value = strdup (str)))
		return (value);

	logger (LOG_ERR, "memory exhausted");
	exit (EXIT_FAILURE);
	/* NOTREACHED */
}