2 * Copyright (c) 1999-2005, 2007, 2009-2013
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
27 # include <sys/file.h>
28 #endif /* HAVE_FLOCK */
36 #endif /* STDC_HEADERS */
38 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
42 #endif /* HAVE_STRING_H */
45 #endif /* HAVE_STRING_H */
46 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
48 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
52 #endif /* HAVE_UNISTD_H */
57 # include "compat/stdbool.h"
59 #if TIME_WITH_SYS_TIME
62 #ifndef HAVE_STRUCT_TIMESPEC
63 # include "compat/timespec.h"
68 #include "sudo_debug.h"
71 * Update the access and modify times on an fd or file.
74 touch(int fd, char *path, struct timeval *tvp)
76 struct timeval times[2];
78 debug_decl(touch, SUDO_DEBUG_UTIL)
81 times[0].tv_sec = times[1].tv_sec = tvp->tv_sec;
82 times[0].tv_usec = times[1].tv_usec = tvp->tv_usec;
85 #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
87 rval = futimes(fd, tvp ? times : NULL);
91 rval = utimes(path, tvp ? times : NULL);
92 debug_return_int(rval);
100 lock_file(int fd, int lockit)
103 debug_decl(lock_file, SUDO_DEBUG_UTIL)
116 debug_return_bool(lockf(fd, op, 0) == 0);
120 lock_file(int fd, int lockit)
123 debug_decl(lock_file, SUDO_DEBUG_UTIL)
130 op = LOCK_EX | LOCK_NB;
136 debug_return_bool(flock(fd, op) == 0);
140 lock_file(int fd, int lockit)
145 debug_decl(lock_file, SUDO_DEBUG_UTIL)
149 lock.l_pid = getpid();
150 lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
151 lock.l_whence = SEEK_SET;
152 func = (lockit == SUDO_LOCK) ? F_SETLKW : F_SETLK;
154 debug_return_bool(fcntl(fd, func, &lock) == 0);
162 * Read a line of input, honoring line continuation chars.
163 * Remove comments and strips off leading and trailing spaces.
164 * Returns the line length and updates the buf and bufsize pointers.
165 * XXX - just use a struct w/ state, including getline buffer?
166 * could also make comment char and line continuation configurable
169 sudo_parseln(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp)
171 size_t len, linesize = 0, total = 0;
172 char *cp, *line = NULL;
174 debug_decl(sudo_parseln, SUDO_DEBUG_UTIL)
178 len = getline(&line, &linesize, fp);
184 /* Remove trailing newline(s) if present. */
185 while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
188 /* Remove comments or check for line continuation (but not both) */
189 if ((cp = strchr(line, '#')) != NULL) {
191 len = (size_t)(cp - line);
192 } else if (len > 0 && line[len - 1] == '\\' && (len == 1 || line[len - 2] != '\\')) {
197 /* Trim leading and trailing whitespace */
199 while (len > 0 && isblank((unsigned char)line[len - 1]))
202 for (cp = line; isblank((unsigned char)*cp); cp++)
205 if (*bufp == NULL || total + len >= *bufsizep) {
207 size_t size = total + len + 1;
211 } else if (size <= 0x80000000) {
212 /* Round up to next highest power of two. */
221 if ((tmp = realloc(*bufp, size)) == NULL)
226 memcpy(*bufp + total, cp, len + 1);
230 if (len == -1 && total == 0)
231 debug_return_size_t((size_t)-1);
232 debug_return_size_t(total);