From 2854f067aec69b613f8b964daa4e15bd3311ec25 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Fri, 24 Aug 2001 23:49:49 +0000 Subject: robustification svn path=/trunk/; revision=12468 --- e-util/ChangeLog | 4 ++++ e-util/e-mktemp.c | 46 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/e-util/ChangeLog b/e-util/ChangeLog index eb1c2f02ba..5eca6efaac 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,7 @@ +2001-08-24 Jeffrey Stedfast + + * e-mktemp.c (get_path): Make more robust. + 2001-08-24 Jeffrey Stedfast * Makefile.am: Added e-mktemp.[c,h] to the build. diff --git a/e-util/e-mktemp.c b/e-util/e-mktemp.c index 8164bc145e..e1211da15b 100644 --- a/e-util/e-mktemp.c +++ b/e-util/e-mktemp.c @@ -32,6 +32,7 @@ #include #include #include +#include #ifdef ENABLE_THREADS #include @@ -62,10 +63,37 @@ get_path (gboolean make) GString *path; path = g_string_new ("/tmp/evolution-"); - g_string_sprintfa (path, "%d-%d", getuid (), getpid ()); + g_string_sprintfa (path, "%d-%d", (int) getuid (), (int) getpid ()); - if (make) - mkdir (path->str, S_IRWXU); + if (make) { + int ret; + + /* shoot now, ask questions later */ + ret = mkdir (path->str, S_IRWXU); + if (ret == -1) { + if (errno == EEXIST) { + struct stat st; + + if (stat (path->str, &st) == -1) { + /* reset errno */ + errno = EEXIST; + g_string_free (path, TRUE); + return NULL; + } + + /* make sure this is a directory and belongs to us... */ + if (!S_ISDIR (st.st_dev) || st.st_uid != getuid ()) { + /* eek! this is bad... */ + g_string_free (path, TRUE); + return NULL; + } + } else { + /* some other error...do not pass go, do not collect $200 */ + g_string_free (path, TRUE); + return NULL; + } + } + } return path; } @@ -145,8 +173,10 @@ e_mktemp (const char *template) ret = mktemp (path->str); if (ret) { TEMP_FILES_LOCK (); - if (!initialized) + if (!initialized) { g_atexit (e_mktemp_cleanup); + initialized = TRUE; + } temp_files = g_slist_prepend (temp_files, ret); g_string_free (path, FALSE); TEMP_FILES_UNLOCK (); @@ -177,8 +207,10 @@ e_mkstemp (const char *template) fd = mkstemp (path->str); if (fd != -1) { TEMP_FILES_LOCK (); - if (!initialized) + if (!initialized) { g_atexit (e_mktemp_cleanup); + initialized = TRUE; + } temp_files = g_slist_prepend (temp_files, path->str); g_string_free (path, FALSE); TEMP_FILES_UNLOCK (); @@ -218,8 +250,10 @@ e_mkdtemp (const char *template) if (tmpdir) { TEMP_DIRS_LOCK (); - if (!initialized) + if (!initialized) { g_atexit (e_mktemp_cleanup); + initialized = TRUE; + } temp_dirs = g_slist_prepend (temp_dirs, tmpdir); g_string_free (path, FALSE); TEMP_DIRS_UNLOCK (); -- cgit