Imported Upstream version 1.6.6
[debian/sudo] / fileops.c
1 /*
2  * Copyright (c) 1999, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * 4. Products derived from this software may not be called "Sudo" nor
20  *    may "Sudo" appear in their names without specific prior written
21  *    permission from the author.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "config.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #ifdef HAVE_FLOCK
40 # include <sys/file.h>
41 #endif /* HAVE_FLOCK */
42 #include <stdio.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif /* HAVE_UNISTD_H */
46 #include <fcntl.h>
47 #include <time.h>
48 #ifdef HAVE_UTIME
49 # ifdef HAVE_UTIME_H
50 #  include <utime.h>
51 # endif /* HAVE_UTIME_H */
52 #else
53 # include "emul/utime.h"
54 #endif /* HAVE_UTIME */
55
56 #include "sudo.h"
57
58 #ifndef lint
59 static const char rcsid[] = "$Sudo: fileops.c,v 1.3 2001/12/14 19:52:47 millert Exp $";
60 #endif /* lint */
61
62 /*
63  * Update the access and modify times on a file.
64  */
65 int
66 touch(path, when)
67     char *path;
68     time_t when;
69 {
70 #ifdef HAVE_UTIME_POSIX
71     struct utimbuf ut, *utp;
72
73     ut.actime = ut.modtime = when;
74     utp = &ut;
75 #else
76     /* BSD <= 4.3 has no struct utimbuf */
77     time_t utp[2];
78
79     utp[0] = utp[1] = when;
80 #endif /* HAVE_UTIME_POSIX */
81
82     return(utime(path, utp));
83 }
84
85 /*
86  * Lock/unlock a file.
87  */
88 #ifdef HAVE_LOCKF
89 int
90 lock_file(fd, lockit)
91     int fd;
92     int lockit;
93 {
94     int op = 0;
95
96     switch (lockit) {
97         case SUDO_LOCK:
98             op = F_LOCK;
99             break;
100         case SUDO_TLOCK:
101             op = F_TLOCK;
102             break;
103         case SUDO_UNLOCK:
104             op = F_ULOCK;
105             break;
106     }
107     return(lockf(fd, op, 0) == 0);
108 }
109 #elif HAVE_FLOCK
110 int
111 lock_file(fd, lockit)
112     int fd;
113     int lockit;
114 {
115     int op = 0;
116
117     switch (lockit) {
118         case SUDO_LOCK:
119             op = LOCK_EX;
120             break;
121         case SUDO_TLOCK:
122             op = LOCK_EX | LOCK_NB;
123             break;
124         case SUDO_UNLOCK:
125             op = LOCK_UN;
126             break;
127     }
128     return(flock(fd, op) == 0);
129 }
130 #else
131 int
132 lock_file(fd, lockit)
133     int fd;
134     int lockit;
135 {
136 #ifdef F_SETLK
137     int func;
138     struct flock lock;
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_TLOCK) ? F_SETLK : F_SETLKW;
146
147     return(fcntl(fd, func, &lock) == 0);
148 #else
149     return(TRUE);
150 #endif
151 }
152 #endif