2 * Copyright (c) 1999-2005 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 #include <sys/types.h>
24 #include <sys/param.h>
27 # include <sys/file.h>
28 #endif /* HAVE_FLOCK */
32 #endif /* HAVE_UNISTD_H */
34 #if TIME_WITH_SYS_TIME
38 # include <emul/timespec.h>
44 __unused static const char rcsid[] = "$Sudo: fileops.c,v 1.5.2.5 2007/06/12 01:28:41 millert Exp $";
48 * Update the access and modify times on an fd or file.
56 struct timeval times[2];
59 times[0].tv_sec = times[1].tv_sec = tsp->tv_sec;
60 times[0].tv_usec = times[1].tv_usec = tsp->tv_nsec / 1000;
63 #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
65 return(futimes(fd, tsp ? times : NULL));
69 return(utimes(path, tsp ? times : NULL));
96 return(lockf(fd, op, 0) == 0);
100 lock_file(fd, lockit)
111 op = LOCK_EX | LOCK_NB;
117 return(flock(fd, op) == 0);
121 lock_file(fd, lockit)
131 lock.l_pid = getpid();
132 lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
133 lock.l_whence = SEEK_SET;
134 func = (lockit == SUDO_TLOCK) ? F_SETLK : F_SETLKW;
136 return(fcntl(fd, func, &lock) == 0);