Imported Debian patch 1.6.8p5-1
[debian/sudo] / fileops.c
1 /*
2  * Copyright (c) 1999, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * Sponsored in part by the Defense Advanced Research Projects
17  * Agency (DARPA) and Air Force Research Laboratory, Air Force
18  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19  */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/time.h>
26 #ifdef HAVE_FLOCK
27 # include <sys/file.h>
28 #endif /* HAVE_FLOCK */
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif /* HAVE_UNISTD_H */
33 #include <fcntl.h>
34 #include <time.h>
35
36 #include "sudo.h"
37
38 #ifndef lint
39 static const char rcsid[] = "$Sudo: fileops.c,v 1.9 2004/09/08 15:48:23 millert Exp $";
40 #endif /* lint */
41
42 /*
43  * Update the access and modify times on an fd or file.
44  */
45 int
46 touch(fd, path, tsp)
47     int fd;
48     char *path;
49     struct timespec *tsp;
50 {
51     struct timeval times[2];
52
53     if (tsp != NULL) {
54         times[0].tv_sec = times[1].tv_sec = tsp->tv_sec;
55         times[0].tv_usec = times[1].tv_usec = tsp->tv_nsec / 1000;
56     }
57
58 #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
59     if (fd != -1)
60         return(futimes(fd, tsp ? times : NULL));
61     else
62 #endif
63     if (path != NULL)
64         return(utimes(path, tsp ? times : NULL));
65     else
66         return(-1);
67 }
68
69 /*
70  * Lock/unlock a file.
71  */
72 #ifdef HAVE_LOCKF
73 int
74 lock_file(fd, lockit)
75     int fd;
76     int lockit;
77 {
78     int op = 0;
79
80     switch (lockit) {
81         case SUDO_LOCK:
82             op = F_LOCK;
83             break;
84         case SUDO_TLOCK:
85             op = F_TLOCK;
86             break;
87         case SUDO_UNLOCK:
88             op = F_ULOCK;
89             break;
90     }
91     return(lockf(fd, op, 0) == 0);
92 }
93 #elif HAVE_FLOCK
94 int
95 lock_file(fd, lockit)
96     int fd;
97     int lockit;
98 {
99     int op = 0;
100
101     switch (lockit) {
102         case SUDO_LOCK:
103             op = LOCK_EX;
104             break;
105         case SUDO_TLOCK:
106             op = LOCK_EX | LOCK_NB;
107             break;
108         case SUDO_UNLOCK:
109             op = LOCK_UN;
110             break;
111     }
112     return(flock(fd, op) == 0);
113 }
114 #else
115 int
116 lock_file(fd, lockit)
117     int fd;
118     int lockit;
119 {
120 #ifdef F_SETLK
121     int func;
122     struct flock lock;
123
124     lock.l_start = 0;
125     lock.l_len = 0;
126     lock.l_pid = getpid();
127     lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
128     lock.l_whence = SEEK_SET;
129     func = (lockit == SUDO_TLOCK) ? F_SETLK : F_SETLKW;
130
131     return(fcntl(fd, func, &lock) == 0);
132 #else
133     return(TRUE);
134 #endif
135 }
136 #endif