Annotation of embedaddon/sudo/common/fileops.c, revision 1.1.1.2
1.1 misho 1: /*
2: * Copyright (c) 1999-2005, 2007, 2009-2011
3: * Todd C. Miller <Todd.Miller@courtesan.com>
4: *
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.
8: *
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.
16: *
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.
20: */
21:
22: #include <config.h>
23:
24: #include <sys/types.h>
25: #include <sys/param.h>
26: #include <sys/time.h>
27: #ifdef HAVE_FLOCK
28: # include <sys/file.h>
29: #endif /* HAVE_FLOCK */
30: #include <stdio.h>
1.1.1.2 ! misho 31: #ifdef HAVE_STDBOOL_H
! 32: # include <stdbool.h>
! 33: #else
! 34: # include "compat/stdbool.h"
! 35: #endif
1.1 misho 36: #ifdef HAVE_STRING_H
37: # include <string.h>
38: #endif /* HAVE_STRING_H */
39: #ifdef HAVE_STRINGS_H
40: # include <strings.h>
41: #endif /* HAVE_STRINGS_H */
42: #include <ctype.h>
43: #include <limits.h>
44: #ifdef HAVE_UNISTD_H
45: # include <unistd.h>
46: #endif /* HAVE_UNISTD_H */
47: #include <fcntl.h>
48: #if TIME_WITH_SYS_TIME
49: # include <time.h>
50: #endif
1.1.1.2 ! misho 51: #ifndef HAVE_STRUCT_TIMESPEC
1.1 misho 52: # include "compat/timespec.h"
53: #endif
54:
55: #include "missing.h"
56: #include "fileops.h"
1.1.1.2 ! misho 57: #include "sudo_debug.h"
1.1 misho 58:
59: #ifndef LINE_MAX
60: # define LINE_MAX 2048
61: #endif
62:
63: /*
64: * Update the access and modify times on an fd or file.
65: */
66: int
67: touch(int fd, char *path, struct timeval *tvp)
68: {
69: struct timeval times[2];
1.1.1.2 ! misho 70: int rval = -1;
! 71: debug_decl(touch, SUDO_DEBUG_UTIL)
1.1 misho 72:
73: if (tvp != NULL) {
74: times[0].tv_sec = times[1].tv_sec = tvp->tv_sec;
75: times[0].tv_usec = times[1].tv_usec = tvp->tv_usec;
76: }
77:
78: #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
79: if (fd != -1)
1.1.1.2 ! misho 80: rval = futimes(fd, tvp ? times : NULL);
1.1 misho 81: else
82: #endif
83: if (path != NULL)
1.1.1.2 ! misho 84: rval = utimes(path, tvp ? times : NULL);
! 85: debug_return_int(rval);
1.1 misho 86: }
87:
88: /*
89: * Lock/unlock a file.
90: */
91: #ifdef HAVE_LOCKF
1.1.1.2 ! misho 92: bool
1.1 misho 93: lock_file(int fd, int lockit)
94: {
95: int op = 0;
1.1.1.2 ! misho 96: debug_decl(lock_file, SUDO_DEBUG_UTIL)
1.1 misho 97:
98: switch (lockit) {
99: case SUDO_LOCK:
100: op = F_LOCK;
101: break;
102: case SUDO_TLOCK:
103: op = F_TLOCK;
104: break;
105: case SUDO_UNLOCK:
106: op = F_ULOCK;
107: break;
108: }
1.1.1.2 ! misho 109: debug_return_bool(lockf(fd, op, 0) == 0);
1.1 misho 110: }
111: #elif HAVE_FLOCK
1.1.1.2 ! misho 112: bool
1.1 misho 113: lock_file(int fd, int lockit)
114: {
115: int op = 0;
1.1.1.2 ! misho 116: debug_decl(lock_file, SUDO_DEBUG_UTIL)
1.1 misho 117:
118: switch (lockit) {
119: case SUDO_LOCK:
120: op = LOCK_EX;
121: break;
122: case SUDO_TLOCK:
123: op = LOCK_EX | LOCK_NB;
124: break;
125: case SUDO_UNLOCK:
126: op = LOCK_UN;
127: break;
128: }
1.1.1.2 ! misho 129: debug_return_bool(flock(fd, op) == 0);
1.1 misho 130: }
131: #else
1.1.1.2 ! misho 132: bool
1.1 misho 133: lock_file(int fd, int lockit)
134: {
135: #ifdef F_SETLK
136: int func;
137: struct flock lock;
1.1.1.2 ! misho 138: debug_decl(lock_file, SUDO_DEBUG_UTIL)
1.1 misho 139:
140: lock.l_start = 0;
141: lock.l_len = 0;
142: lock.l_pid = getpid();
143: lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
144: lock.l_whence = SEEK_SET;
145: func = (lockit == SUDO_LOCK) ? F_SETLKW : F_SETLK;
146:
1.1.1.2 ! misho 147: debug_return_bool(fcntl(fd, func, &lock) == 0);
1.1 misho 148: #else
1.1.1.2 ! misho 149: return true;
1.1 misho 150: #endif
151: }
152: #endif
153:
154: /*
155: * Read a line of input, remove comments and strip off leading
156: * and trailing spaces. Returns static storage that is reused.
157: */
158: char *
159: sudo_parseln(FILE *fp)
160: {
161: size_t len;
162: char *cp = NULL;
163: static char buf[LINE_MAX];
1.1.1.2 ! misho 164: debug_decl(sudo_parseln, SUDO_DEBUG_UTIL)
1.1 misho 165:
166: if (fgets(buf, sizeof(buf), fp) != NULL) {
167: /* Remove comments */
168: if ((cp = strchr(buf, '#')) != NULL)
169: *cp = '\0';
170:
171: /* Trim leading and trailing whitespace/newline */
172: len = strlen(buf);
173: while (len > 0 && isspace((unsigned char)buf[len - 1]))
174: buf[--len] = '\0';
175: for (cp = buf; isblank((unsigned char)*cp); cp++)
176: continue;
177: }
1.1.1.2 ! misho 178: debug_return_str(cp);
1.1 misho 179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>