main: create parent directories of NMSTATEDIR with permissions 755

In case, "$PREFIX/var/lib" does not exist, we want to create those
directories with permissions 755, not 700.
This commit is contained in:
Thomas Haller
2016-03-21 12:01:26 +01:00
parent 8297683605
commit acf9496ccf
3 changed files with 28 additions and 6 deletions

View File

@@ -29,6 +29,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <locale.h> #include <locale.h>
#include <glib/gstdio.h>
#include <glib-unix.h> #include <glib-unix.h>
#include <gmodule.h> #include <gmodule.h>
@@ -111,6 +112,31 @@ nm_main_utils_write_pidfile (const char *pidfile)
return success; return success;
} }
void
nm_main_utils_ensure_statedir ()
{
gs_free char *parent = NULL;
int errsv;
parent = g_path_get_dirname (NMSTATEDIR);
/* Ensure parent state directories exists */
if ( parent
&& parent[0] == '/'
&& parent[1] != '\0'
&& g_mkdir_with_parents (parent, 0755) != 0) {
errsv = errno;
fprintf (stderr, "Cannot create parents for '%s': %s", NMSTATEDIR, g_strerror (errsv));
exit (1);
}
/* Ensure state directory exists */
if (g_mkdir_with_parents (NMSTATEDIR, 0700) != 0) {
errsv = errno;
fprintf (stderr, "Cannot create '%s': %s", NMSTATEDIR, g_strerror (errsv));
exit (1);
}
}
void void
nm_main_utils_ensure_rundir () nm_main_utils_ensure_rundir ()
{ {

View File

@@ -27,6 +27,7 @@ void nm_main_utils_ensure_root (void);
void nm_main_utils_setup_signals (GMainLoop *main_loop); void nm_main_utils_setup_signals (GMainLoop *main_loop);
void nm_main_utils_ensure_statedir (void);
void nm_main_utils_ensure_rundir (void); void nm_main_utils_ensure_rundir (void);
gboolean nm_main_utils_write_pidfile (const char *pidfile); gboolean nm_main_utils_write_pidfile (const char *pidfile);

View File

@@ -309,12 +309,7 @@ main (int argc, char *argv[])
nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile);
/* Ensure state directory exists */ nm_main_utils_ensure_statedir ();
if (g_mkdir_with_parents (NMSTATEDIR, 0700) != 0) {
fprintf (stderr, "Cannot create '%s': %s", NMSTATEDIR, strerror (errno));
exit (1);
}
nm_main_utils_ensure_rundir (); nm_main_utils_ensure_rundir ();
/* When running from the build directory, determine our build directory /* When running from the build directory, determine our build directory