/*
- * Copyright (c) 2010, 2011, 2013 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2010, 2011, 2013, 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
struct timeval;
-__dso_public bool lock_file(int, int);
-__dso_public int touch(int, char *, struct timeval *);
+__dso_public bool sudo_lock_file(int, int);
__dso_public ssize_t sudo_parseln(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp);
#endif /* _SUDO_FILEOPS_H */
/*
- * Copyright (c) 1999-2005, 2007, 2009-2013
+ * 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
#else
# include "compat/stdbool.h"
#endif
-#ifdef TIME_WITH_SYS_TIME
-# include <time.h>
-#endif
-#ifndef HAVE_STRUCT_TIMESPEC
-# include "compat/timespec.h"
-#endif
#include "missing.h"
#include "fileops.h"
#include "sudo_debug.h"
-/*
- * Update the access and modify times on an fd or file.
- */
-int
-touch(int fd, char *path, struct timeval *tvp)
-{
- struct timeval times[2];
- int rval = -1;
- debug_decl(touch, SUDO_DEBUG_UTIL)
-
- if (tvp != NULL) {
- times[0].tv_sec = times[1].tv_sec = tvp->tv_sec;
- times[0].tv_usec = times[1].tv_usec = tvp->tv_usec;
- }
-
-#if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
- if (fd != -1)
- rval = futimes(fd, tvp ? times : NULL);
- else
-#endif
- if (path != NULL)
- rval = utimes(path, tvp ? times : NULL);
- debug_return_int(rval);
-}
-
/*
* Lock/unlock a file.
*/
#ifdef HAVE_LOCKF
bool
-lock_file(int fd, int lockit)
+sudo_lock_file(int fd, int lockit)
{
int op = 0;
- debug_decl(lock_file, SUDO_DEBUG_UTIL)
+ debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
switch (lockit) {
case SUDO_LOCK:
}
#elif defined(HAVE_FLOCK)
bool
-lock_file(int fd, int lockit)
+sudo_lock_file(int fd, int lockit)
{
int op = 0;
- debug_decl(lock_file, SUDO_DEBUG_UTIL)
+ debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
switch (lockit) {
case SUDO_LOCK:
}
#else
bool
-lock_file(int fd, int lockit)
+sudo_lock_file(int fd, int lockit)
{
#ifdef F_SETLK
int func;
struct flock lock;
- debug_decl(lock_file, SUDO_DEBUG_UTIL)
+ debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
lock.l_start = 0;
lock.l_len = 0;
lbuf_destroy
lbuf_init
lbuf_print
-lock_file
parse_gid_list
sudo_asprintf
sudo_clock_gettime
sudo_glob
sudo_globfree
sudo_inet_pton
+sudo_lock_file
sudo_memrchr
sudo_memset_s
sudo_mkdtemp
term_noecho
term_raw
term_restore
-touch
vfatal_nodebug
vfatalx_nodebug
vwarning_nodebug
log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), pathbuf);
debug_return_bool(false);
}
- lock_file(fd, SUDO_LOCK);
+ sudo_lock_file(fd, SUDO_LOCK);
/*
* If there is no seq file in iolog_dir and a fallback dir was
if (ofd != -1) {
(void) fcntl(ofd, F_SETFL, 0);
- if (lock_file(ofd, SUDO_LOCK)) {
+ if (sudo_lock_file(ofd, SUDO_LOCK)) {
snprintf(new_ccname, sizeof(new_ccname), "%s%s",
_PATH_TMP, "sudocc_XXXXXXXX");
nfd = mkstemp(new_ccname);
if (fp == NULL) {
send_mail(_("unable to open log file: %s: %s"),
def_logfile, strerror(errno));
- } else if (!lock_file(fileno(fp), SUDO_LOCK)) {
+ } else if (!sudo_lock_file(fileno(fp), SUDO_LOCK)) {
send_mail(_("unable to lock log file: %s: %s"),
def_logfile, strerror(errno));
} else {
efree(full_line);
}
(void) fflush(fp);
- (void) lock_file(fileno(fp), SUDO_UNLOCK);
+ (void) sudo_lock_file(fileno(fp), SUDO_UNLOCK);
(void) fclose(fp);
}
sudoers_setlocale(oldlocale, NULL);
}
/* Update record or append a new one. */
- lock_file(fd, SUDO_LOCK);
+ sudo_lock_file(fd, SUDO_LOCK);
ts_update_record(fd, &entry, timestamp_hint);
close(fd);
status = TS_MISSING;
goto done;
}
- lock_file(fd, SUDO_LOCK);
+ sudo_lock_file(fd, SUDO_LOCK);
/* Ignore and clear time stamp file if mtime predates boot time. */
if (fstat(fd, &sb) == 0) {
(void) restore_perms();
if (fd == -1)
goto done;
- lock_file(fd, SUDO_LOCK);
+ sudo_lock_file(fd, SUDO_LOCK);
/*
* Find matching entries and invalidate them.
char *cp; /* scratch char pointer */
char buf[PATH_MAX*2]; /* buffer used for copying files */
char linestr[64]; /* string version of lineno */
- struct timeval tv, tv1, tv2; /* time before and after edit */
+ struct timeval tv, times[2]; /* time before and after edit */
struct timeval orig_mtim; /* starting mtime of sudoers file */
off_t orig_size; /* starting size of sudoers file */
ssize_t nread; /* number of bytes read */
}
(void) close(tfd);
}
- (void) touch(-1, sp->tpath, &orig_mtim);
+ times[0].tv_sec = times[1].tv_sec = orig_mtim.tv_sec;
+ times[0].tv_usec = times[1].tv_usec = orig_mtim.tv_usec;
+ (void) utimes(sp->tpath, times);
/* Does the editor support +lineno? */
if (lineno > 0)
* XPG4 specifies that vi's exit value is a function of the
* number of errors during editing (?!?!).
*/
- gettimeofday(&tv1, NULL);
+ gettimeofday(×[0], NULL);
if (run_command(editor, av) != -1) {
- gettimeofday(&tv2, NULL);
+ gettimeofday(×[1], NULL);
/*
* Sanity checks.
*/
* If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed.
*/
- if (sudo_timevalcmp(&tv1, &tv2, !=))
+ if (sudo_timevalcmp(×[0], ×[1], !=))
modified = false;
}
efree(entry);
debug_return_ptr(NULL);
}
- if (!checkonly && !lock_file(entry->fd, SUDO_TLOCK))
+ if (!checkonly && !sudo_lock_file(entry->fd, SUDO_TLOCK))
fatalx(U_("%s busy, try again later"), entry->path);
if ((fp = fdopen(entry->fd, "r")) == NULL)
fatal("%s", entry->path);
int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen;
int editor_argc = 0, nfiles = 0;
struct stat sb;
- struct timeval tv, tv1, tv2;
+ struct timeval tv, times[2];
struct tempfile {
char *tfile;
char *ofile;
* We always update the stashed mtime because the time
* resolution of the filesystem the temporary file is on may
* not match that of the filesystem where the file to be edited
- * resides. It is OK if touch() fails since we only use the info
- * to determine whether or not a file has been modified.
+ * resides. It is OK if futimes() fails since we only use the
+ * info to determine whether or not a file has been modified.
*/
- (void) touch(tfd, NULL, &tf[j].omtim);
+ times[0].tv_sec = times[1].tv_sec = tf[j].omtim.tv_sec;
+ times[0].tv_usec = times[1].tv_usec = tf[j].omtim.tv_usec;
+ (void) futimes(tfd, times);
rc = fstat(tfd, &sb);
if (!rc)
mtim_get(&sb, &tf[j].omtim);
* Run the editor with the invoking user's creds,
* keeping track of the time spent in the editor.
*/
- gettimeofday(&tv1, NULL);
+ gettimeofday(×[0], NULL);
memcpy(&editor_details, command_details, sizeof(editor_details));
editor_details.uid = user_details.uid;
editor_details.euid = user_details.uid;
editor_details.groups = user_details.groups;
editor_details.argv = nargv;
rval = run_command(&editor_details);
- gettimeofday(&tv2, NULL);
+ gettimeofday(×[1], NULL);
/* Copy contents of temp files to real ones */
for (i = 0; i < nfiles; i++) {
* If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed.
*/
- if (sudo_timevalcmp(&tv1, &tv2, !=)) {
+ if (sudo_timevalcmp(×[0], ×[1], !=)) {
warningx(U_("%s unchanged"), tf[i].ofile);
unlink(tf[i].tfile);
close(tfd);