Split fileops.c into parseln.c and locking.c
This commit is contained in:
127
lib/util/locking.c
Normal file
127
lib/util/locking.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (c) 1999-2005, 2007, 2009-2014
|
||||
* Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* Sponsored in part by the Defense Advanced Research Projects
|
||||
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
||||
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#ifdef HAVE_FLOCK
|
||||
# include <sys/file.h>
|
||||
#endif /* HAVE_FLOCK */
|
||||
#ifdef STDC_HEADERS
|
||||
# include <stdlib.h>
|
||||
# include <stddef.h>
|
||||
#else
|
||||
# ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
# endif
|
||||
#endif /* STDC_HEADERS */
|
||||
#ifdef HAVE_STRING_H
|
||||
# if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
|
||||
# include <memory.h>
|
||||
# endif
|
||||
# include <string.h>
|
||||
#endif /* HAVE_STRING_H */
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif /* HAVE_STRING_H */
|
||||
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
|
||||
# include <malloc.h>
|
||||
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
|
||||
#include <ctype.h>
|
||||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif /* HAVE_UNISTD_H */
|
||||
#include <fcntl.h>
|
||||
#ifdef HAVE_STDBOOL_H
|
||||
# include <stdbool.h>
|
||||
#else
|
||||
# include "compat/stdbool.h"
|
||||
#endif
|
||||
|
||||
#include "missing.h"
|
||||
#include "sudo_util.h"
|
||||
#include "sudo_debug.h"
|
||||
|
||||
/*
|
||||
* Lock/unlock a file.
|
||||
*/
|
||||
#ifdef HAVE_LOCKF
|
||||
bool
|
||||
sudo_lock_file(int fd, int lockit)
|
||||
{
|
||||
int op = 0;
|
||||
debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
|
||||
|
||||
switch (lockit) {
|
||||
case SUDO_LOCK:
|
||||
op = F_LOCK;
|
||||
break;
|
||||
case SUDO_TLOCK:
|
||||
op = F_TLOCK;
|
||||
break;
|
||||
case SUDO_UNLOCK:
|
||||
op = F_ULOCK;
|
||||
break;
|
||||
}
|
||||
debug_return_bool(lockf(fd, op, 0) == 0);
|
||||
}
|
||||
#elif defined(HAVE_FLOCK)
|
||||
bool
|
||||
sudo_lock_file(int fd, int lockit)
|
||||
{
|
||||
int op = 0;
|
||||
debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
|
||||
|
||||
switch (lockit) {
|
||||
case SUDO_LOCK:
|
||||
op = LOCK_EX;
|
||||
break;
|
||||
case SUDO_TLOCK:
|
||||
op = LOCK_EX | LOCK_NB;
|
||||
break;
|
||||
case SUDO_UNLOCK:
|
||||
op = LOCK_UN;
|
||||
break;
|
||||
}
|
||||
debug_return_bool(flock(fd, op) == 0);
|
||||
}
|
||||
#else
|
||||
bool
|
||||
sudo_lock_file(int fd, int lockit)
|
||||
{
|
||||
#ifdef F_SETLK
|
||||
int func;
|
||||
struct flock lock;
|
||||
debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
|
||||
|
||||
lock.l_start = 0;
|
||||
lock.l_len = 0;
|
||||
lock.l_pid = getpid();
|
||||
lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
|
||||
lock.l_whence = SEEK_SET;
|
||||
func = (lockit == SUDO_LOCK) ? F_SETLKW : F_SETLK;
|
||||
|
||||
debug_return_bool(fcntl(fd, func, &lock) == 0);
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user