Remove touch() from fileops.c and just call utimes/futimes directly.

Rename lock_file -> sudo_lock_file to avoid namespace pollution
This commit is contained in:
Todd C. Miller
2014-06-26 15:51:15 -06:00
parent 36e828b59f
commit f029e3e744
9 changed files with 35 additions and 63 deletions

View File

@@ -1,5 +1,6 @@
/* /*
* 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 * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -26,8 +27,7 @@
struct timeval; struct timeval;
__dso_public bool lock_file(int, int); __dso_public bool sudo_lock_file(int, int);
__dso_public int touch(int, char *, struct timeval *);
__dso_public ssize_t sudo_parseln(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp); __dso_public ssize_t sudo_parseln(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp);
#endif /* _SUDO_FILEOPS_H */ #endif /* _SUDO_FILEOPS_H */

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999-2005, 2007, 2009-2013 * Copyright (c) 1999-2005, 2007, 2009-2014
* Todd C. Miller <Todd.Miller@courtesan.com> * Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
@@ -56,51 +56,20 @@
#else #else
# include "compat/stdbool.h" # include "compat/stdbool.h"
#endif #endif
#ifdef TIME_WITH_SYS_TIME
# include <time.h>
#endif
#ifndef HAVE_STRUCT_TIMESPEC
# include "compat/timespec.h"
#endif
#include "missing.h" #include "missing.h"
#include "fileops.h" #include "fileops.h"
#include "sudo_debug.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. * Lock/unlock a file.
*/ */
#ifdef HAVE_LOCKF #ifdef HAVE_LOCKF
bool bool
lock_file(int fd, int lockit) sudo_lock_file(int fd, int lockit)
{ {
int op = 0; int op = 0;
debug_decl(lock_file, SUDO_DEBUG_UTIL) debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
switch (lockit) { switch (lockit) {
case SUDO_LOCK: case SUDO_LOCK:
@@ -117,10 +86,10 @@ lock_file(int fd, int lockit)
} }
#elif defined(HAVE_FLOCK) #elif defined(HAVE_FLOCK)
bool bool
lock_file(int fd, int lockit) sudo_lock_file(int fd, int lockit)
{ {
int op = 0; int op = 0;
debug_decl(lock_file, SUDO_DEBUG_UTIL) debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
switch (lockit) { switch (lockit) {
case SUDO_LOCK: case SUDO_LOCK:
@@ -137,12 +106,12 @@ lock_file(int fd, int lockit)
} }
#else #else
bool bool
lock_file(int fd, int lockit) sudo_lock_file(int fd, int lockit)
{ {
#ifdef F_SETLK #ifdef F_SETLK
int func; int func;
struct flock lock; struct flock lock;
debug_decl(lock_file, SUDO_DEBUG_UTIL) debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL)
lock.l_start = 0; lock.l_start = 0;
lock.l_len = 0; lock.l_len = 0;

View File

@@ -50,7 +50,6 @@ lbuf_append_quoted
lbuf_destroy lbuf_destroy
lbuf_init lbuf_init
lbuf_print lbuf_print
lock_file
parse_gid_list parse_gid_list
sudo_asprintf sudo_asprintf
sudo_clock_gettime sudo_clock_gettime
@@ -116,6 +115,7 @@ sudo_getopt_long_only
sudo_glob sudo_glob
sudo_globfree sudo_globfree
sudo_inet_pton sudo_inet_pton
sudo_lock_file
sudo_memrchr sudo_memrchr
sudo_memset_s sudo_memset_s
sudo_mkdtemp sudo_mkdtemp
@@ -142,7 +142,6 @@ term_kill
term_noecho term_noecho
term_raw term_raw
term_restore term_restore
touch
vfatal_nodebug vfatal_nodebug
vfatalx_nodebug vfatalx_nodebug
vwarning_nodebug vwarning_nodebug

View File

@@ -209,7 +209,7 @@ io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7])
log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), pathbuf); log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), pathbuf);
debug_return_bool(false); 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 there is no seq file in iolog_dir and a fallback dir was

View File

@@ -2118,7 +2118,7 @@ sudo_krb5_copy_cc_file(const char *old_ccname)
if (ofd != -1) { if (ofd != -1) {
(void) fcntl(ofd, F_SETFL, 0); (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", snprintf(new_ccname, sizeof(new_ccname), "%s%s",
_PATH_TMP, "sudocc_XXXXXXXX"); _PATH_TMP, "sudocc_XXXXXXXX");
nfd = mkstemp(new_ccname); nfd = mkstemp(new_ccname);

View File

@@ -186,7 +186,7 @@ do_logfile(char *msg)
if (fp == NULL) { if (fp == NULL) {
send_mail(_("unable to open log file: %s: %s"), send_mail(_("unable to open log file: %s: %s"),
def_logfile, strerror(errno)); 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"), send_mail(_("unable to lock log file: %s: %s"),
def_logfile, strerror(errno)); def_logfile, strerror(errno));
} else { } else {
@@ -217,7 +217,7 @@ do_logfile(char *msg)
efree(full_line); efree(full_line);
} }
(void) fflush(fp); (void) fflush(fp);
(void) lock_file(fileno(fp), SUDO_UNLOCK); (void) sudo_lock_file(fileno(fp), SUDO_UNLOCK);
(void) fclose(fp); (void) fclose(fp);
} }
sudoers_setlocale(oldlocale, NULL); sudoers_setlocale(oldlocale, NULL);

View File

@@ -360,7 +360,7 @@ update_timestamp(struct passwd *pw)
} }
/* Update record or append a new one. */ /* 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); ts_update_record(fd, &entry, timestamp_hint);
close(fd); close(fd);
@@ -444,7 +444,7 @@ timestamp_status(struct passwd *pw)
status = TS_MISSING; status = TS_MISSING;
goto done; goto done;
} }
lock_file(fd, SUDO_LOCK); sudo_lock_file(fd, SUDO_LOCK);
/* Ignore and clear time stamp file if mtime predates boot time. */ /* Ignore and clear time stamp file if mtime predates boot time. */
if (fstat(fd, &sb) == 0) { if (fstat(fd, &sb) == 0) {
@@ -574,7 +574,7 @@ remove_timestamp(bool unlink_it)
(void) restore_perms(); (void) restore_perms();
if (fd == -1) if (fd == -1)
goto done; goto done;
lock_file(fd, SUDO_LOCK); sudo_lock_file(fd, SUDO_LOCK);
/* /*
* Find matching entries and invalidate them. * Find matching entries and invalidate them.

View File

@@ -315,7 +315,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
char *cp; /* scratch char pointer */ char *cp; /* scratch char pointer */
char buf[PATH_MAX*2]; /* buffer used for copying files */ char buf[PATH_MAX*2]; /* buffer used for copying files */
char linestr[64]; /* string version of lineno */ 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 */ struct timeval orig_mtim; /* starting mtime of sudoers file */
off_t orig_size; /* starting size of sudoers file */ off_t orig_size; /* starting size of sudoers file */
ssize_t nread; /* number of bytes read */ ssize_t nread; /* number of bytes read */
@@ -351,7 +351,9 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
} }
(void) close(tfd); (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? */ /* Does the editor support +lineno? */
if (lineno > 0) if (lineno > 0)
@@ -421,9 +423,9 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
* XPG4 specifies that vi's exit value is a function of the * XPG4 specifies that vi's exit value is a function of the
* number of errors during editing (?!?!). * number of errors during editing (?!?!).
*/ */
gettimeofday(&tv1, NULL); gettimeofday(&times[0], NULL);
if (run_command(editor, av) != -1) { if (run_command(editor, av) != -1) {
gettimeofday(&tv2, NULL); gettimeofday(&times[1], NULL);
/* /*
* Sanity checks. * Sanity checks.
*/ */
@@ -451,7 +453,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
* If mtime and size match but the user spent no measurable * If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed. * time in the editor we can't tell if the file was changed.
*/ */
if (sudo_timevalcmp(&tv1, &tv2, !=)) if (sudo_timevalcmp(&times[0], &times[1], !=))
modified = false; modified = false;
} }
@@ -906,7 +908,7 @@ open_sudoers(const char *path, bool doedit, bool *keepopen)
efree(entry); efree(entry);
debug_return_ptr(NULL); 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); fatalx(U_("%s busy, try again later"), entry->path);
if ((fp = fdopen(entry->fd, "r")) == NULL) if ((fp = fdopen(entry->fd, "r")) == NULL)
fatal("%s", entry->path); fatal("%s", entry->path);

View File

@@ -93,7 +93,7 @@ sudo_edit(struct command_details *command_details)
int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen; int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen;
int editor_argc = 0, nfiles = 0; int editor_argc = 0, nfiles = 0;
struct stat sb; struct stat sb;
struct timeval tv, tv1, tv2; struct timeval tv, times[2];
struct tempfile { struct tempfile {
char *tfile; char *tfile;
char *ofile; char *ofile;
@@ -211,10 +211,12 @@ sudo_edit(struct command_details *command_details)
* We always update the stashed mtime because the time * We always update the stashed mtime because the time
* resolution of the filesystem the temporary file is on may * resolution of the filesystem the temporary file is on may
* not match that of the filesystem where the file to be edited * 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 * resides. It is OK if futimes() fails since we only use the
* to determine whether or not a file has been modified. * 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); rc = fstat(tfd, &sb);
if (!rc) if (!rc)
mtim_get(&sb, &tf[j].omtim); mtim_get(&sb, &tf[j].omtim);
@@ -241,7 +243,7 @@ sudo_edit(struct command_details *command_details)
* Run the editor with the invoking user's creds, * Run the editor with the invoking user's creds,
* keeping track of the time spent in the editor. * keeping track of the time spent in the editor.
*/ */
gettimeofday(&tv1, NULL); gettimeofday(&times[0], NULL);
memcpy(&editor_details, command_details, sizeof(editor_details)); memcpy(&editor_details, command_details, sizeof(editor_details));
editor_details.uid = user_details.uid; editor_details.uid = user_details.uid;
editor_details.euid = user_details.uid; editor_details.euid = user_details.uid;
@@ -251,7 +253,7 @@ sudo_edit(struct command_details *command_details)
editor_details.groups = user_details.groups; editor_details.groups = user_details.groups;
editor_details.argv = nargv; editor_details.argv = nargv;
rval = run_command(&editor_details); rval = run_command(&editor_details);
gettimeofday(&tv2, NULL); gettimeofday(&times[1], NULL);
/* Copy contents of temp files to real ones */ /* Copy contents of temp files to real ones */
for (i = 0; i < nfiles; i++) { for (i = 0; i < nfiles; i++) {
@@ -279,7 +281,7 @@ sudo_edit(struct command_details *command_details)
* If mtime and size match but the user spent no measurable * If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed. * time in the editor we can't tell if the file was changed.
*/ */
if (sudo_timevalcmp(&tv1, &tv2, !=)) { if (sudo_timevalcmp(&times[0], &times[1], !=)) {
warningx(U_("%s unchanged"), tf[i].ofile); warningx(U_("%s unchanged"), tf[i].ofile);
unlink(tf[i].tfile); unlink(tf[i].tfile);
close(tfd); close(tfd);