Annotation of src/usr.bin/sudo/fileops.c, Revision 1.1
1.1 ! millert 1: /*
! 2: * Copyright (c) 1999 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 <stdio.h>
! 38: #ifdef HAVE_UNISTD_H
! 39: #include <unistd.h>
! 40: #endif /* HAVE_UNISTD_H */
! 41: #include <fcntl.h>
! 42: #include <time.h>
! 43: #include <sys/types.h>
! 44: #include <sys/param.h>
! 45: #ifdef HAVE_UTIME
! 46: # ifdef HAVE_UTIME_H
! 47: # include <utime.h>
! 48: # endif /* HAVE_UTIME_H */
! 49: #else
! 50: # include "emul/utime.h"
! 51: #endif /* HAVE_UTIME */
! 52:
! 53: #include "sudo.h"
! 54:
! 55: #ifndef lint
! 56: static const char rcsid[] = "$Sudo: fileops.c,v 1.1 1999/08/07 09:59:43 millert Exp $";
! 57: #endif /* lint */
! 58:
! 59: /*
! 60: * Update the access and modify times on a file.
! 61: */
! 62: int
! 63: touch(path, when)
! 64: char *path;
! 65: time_t when;
! 66: {
! 67: #ifdef HAVE_UTIME_POSIX
! 68: struct utimbuf ut, *utp;
! 69:
! 70: ut.actime = ut.modtime = when;
! 71: utp = &ut;
! 72: #else
! 73: /* BSD <= 4.3 has no struct utimbuf */
! 74: time_t utp[2];
! 75:
! 76: utp[0] = utp[1] = when;
! 77: #endif /* HAVE_UTIME_POSIX */
! 78:
! 79: return(utime(path, utp));
! 80: }
! 81:
! 82: /*
! 83: * Lock/unlock a file.
! 84: */
! 85: #ifdef HAVE_LOCKF
! 86: int
! 87: lock_file(fd, lockit)
! 88: int fd;
! 89: int lockit;
! 90: {
! 91: int op = 0;
! 92:
! 93: switch (lockit) {
! 94: case SUDO_LOCK:
! 95: op = F_LOCK;
! 96: break;
! 97: case SUDO_TLOCK:
! 98: op = F_TLOCK;
! 99: break;
! 100: case SUDO_UNLOCK:
! 101: op = F_ULOCK;
! 102: break;
! 103: }
! 104: return(lockf(fd, op, 0) == 0);
! 105: }
! 106: #elif HAVE_FLOCK
! 107: int
! 108: lock_file(fd, lockit)
! 109: int fd;
! 110: int lockit;
! 111: {
! 112: int op = 0;
! 113:
! 114: switch (lockit) {
! 115: case SUDO_LOCK:
! 116: op = LOCK_EX;
! 117: break;
! 118: case SUDO_TLOCK:
! 119: op = LOCK_EX | LOCK_NB;
! 120: break;
! 121: case SUDO_UNLOCK:
! 122: op = LOCK_EX;
! 123: break;
! 124: }
! 125: return(flock(fd, op) == 0);
! 126: }
! 127: #else
! 128: int
! 129: lock_file(fd, lockit)
! 130: int fd;
! 131: int lockit;
! 132: {
! 133: #ifdef F_SETLK
! 134: int func;
! 135: struct flock lock;
! 136:
! 137: lock.l_start = 0;
! 138: lock.l_len = 0;
! 139: lock.l_pid = getpid();
! 140: lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
! 141: lock.l_whence = SEEK_SET;
! 142: func = (lockit == SUDO_TLOCK) ? F_SETLK : F_SETLKW;
! 143:
! 144: return(fcntl(fd, func, &lock) == 0);
! 145: #else
! 146: return(TRUE);
! 147: #endif
! 148: }
! 149: #endif