[BACK]Return to fileops.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/fileops.c, Revision 1.3

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