[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.5

1.1       millert     1: /*
1.5     ! millert     2:  * Copyright (c) 1999-2005 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     3:  *
1.4       millert     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.
1.3       millert    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.
1.1       millert    19:  */
                     20:
1.5     ! millert    21: #include <config.h>
1.1       millert    22:
1.2       millert    23: #include <sys/types.h>
                     24: #include <sys/param.h>
1.4       millert    25: #include <sys/time.h>
1.2       millert    26: #ifdef HAVE_FLOCK
                     27: # include <sys/file.h>
                     28: #endif /* HAVE_FLOCK */
1.1       millert    29: #include <stdio.h>
                     30: #ifdef HAVE_UNISTD_H
1.2       millert    31: # include <unistd.h>
1.1       millert    32: #endif /* HAVE_UNISTD_H */
                     33: #include <fcntl.h>
1.5     ! millert    34: #if TIME_WITH_SYS_TIME
        !            35: # include <time.h>
        !            36: #endif
        !            37: #ifndef HAVE_TIMESPEC
        !            38: # include <emul/timespec.h>
        !            39: #endif
1.1       millert    40:
                     41: #include "sudo.h"
                     42:
                     43: #ifndef lint
1.5     ! millert    44: __unused static const char rcsid[] = "$Sudo: fileops.c,v 1.5.2.5 2007/06/12 01:28:41 millert Exp $";
1.1       millert    45: #endif /* lint */
                     46:
                     47: /*
1.4       millert    48:  * Update the access and modify times on an fd or file.
1.1       millert    49:  */
                     50: int
1.4       millert    51: touch(fd, path, tsp)
                     52:     int fd;
1.1       millert    53:     char *path;
1.4       millert    54:     struct timespec *tsp;
1.1       millert    55: {
1.4       millert    56:     struct timeval times[2];
1.1       millert    57:
1.4       millert    58:     if (tsp != NULL) {
                     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;
                     61:     }
                     62:
                     63: #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
                     64:     if (fd != -1)
                     65:        return(futimes(fd, tsp ? times : NULL));
                     66:     else
                     67: #endif
                     68:     if (path != NULL)
                     69:        return(utimes(path, tsp ? times : NULL));
                     70:     else
                     71:        return(-1);
1.1       millert    72: }
                     73:
                     74: /*
                     75:  * Lock/unlock a file.
                     76:  */
                     77: #ifdef HAVE_LOCKF
                     78: int
                     79: lock_file(fd, lockit)
                     80:     int fd;
                     81:     int lockit;
                     82: {
                     83:     int op = 0;
                     84:
                     85:     switch (lockit) {
                     86:        case SUDO_LOCK:
                     87:            op = F_LOCK;
                     88:            break;
                     89:        case SUDO_TLOCK:
                     90:            op = F_TLOCK;
                     91:            break;
                     92:        case SUDO_UNLOCK:
                     93:            op = F_ULOCK;
                     94:            break;
                     95:     }
                     96:     return(lockf(fd, op, 0) == 0);
                     97: }
                     98: #elif HAVE_FLOCK
                     99: int
                    100: lock_file(fd, lockit)
                    101:     int fd;
                    102:     int lockit;
                    103: {
                    104:     int op = 0;
                    105:
                    106:     switch (lockit) {
                    107:        case SUDO_LOCK:
                    108:            op = LOCK_EX;
                    109:            break;
                    110:        case SUDO_TLOCK:
                    111:            op = LOCK_EX | LOCK_NB;
                    112:            break;
                    113:        case SUDO_UNLOCK:
1.2       millert   114:            op = LOCK_UN;
1.1       millert   115:            break;
                    116:     }
                    117:     return(flock(fd, op) == 0);
                    118: }
                    119: #else
                    120: int
                    121: lock_file(fd, lockit)
                    122:     int fd;
                    123:     int lockit;
                    124: {
                    125: #ifdef F_SETLK
                    126:     int func;
                    127:     struct flock lock;
                    128:
                    129:     lock.l_start = 0;
                    130:     lock.l_len = 0;
                    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;
                    135:
                    136:     return(fcntl(fd, func, &lock) == 0);
                    137: #else
                    138:     return(TRUE);
                    139: #endif
                    140: }
                    141: #endif