summary refs log tree commit diff
path: root/src/main-utils.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2015-05-05 17:48:57 +0200
committerMichael Biebl <biebl@debian.org>2015-05-05 17:48:57 +0200
commitf408e27bccfacf347605a8d98649975a68f38a17 (patch)
tree654fd6695c31511baf919b1c0870d119a352ed75 /src/main-utils.c
parent2c032d8f1c6292c1338a615e6ec40252889ba85c (diff)
Imported Upstream version 1.0.2 upstream/1.0.2
Diffstat (limited to 'src/main-utils.c')
-rw-r--r--src/main-utils.c93
1 files changed, 58 insertions, 35 deletions
diff --git a/src/main-utils.c b/src/main-utils.c
index 0fa3644f..c4d0eeb9 100644
--- a/src/main-utils.c
+++ b/src/main-utils.c
@@ -33,8 +33,10 @@
 #include <glib/gi18n.h>
 #include <gmodule.h>
 
+#include "gsystem-local-alloc.h"
 #include "main-utils.h"
 #include "nm-posix-signals.h"
+#include "NetworkManagerUtils.h"
 #include "nm-logging.h"
 
 static sigset_t signal_set;
@@ -152,72 +154,82 @@ nm_main_utils_write_pidfile (const char *pidfile)
 	return success;
 }
 
+void
+nm_main_utils_ensure_rundir ()
+{
+	/* Setup runtime directory */
+	if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) {
+		fprintf (stderr, _("Cannot create '%s': %s"), NMRUNDIR, strerror (errno));
+		exit (1);
+	}
+}
+
 /**
- * nm_main_utils_check_pidfile:
+ * nm_main_utils_ensure_not_running_pidfile:
  * @pidfile: the pid file
- * @name: the process name
  *
  * Checks whether the pidfile already exists and contains PID of a running
  * process.
  *
- * Returns: %TRUE if the specified pidfile already exists and contains the PID
- *  of a running process named @name, or %FALSE if not
+ * Exits with code 1 if a conflicting process is running.
  */
-gboolean
-nm_main_utils_check_pidfile (const char *pidfile, const char *name)
+void
+nm_main_utils_ensure_not_running_pidfile (const char *pidfile)
 {
-	char *contents = NULL;
+	gs_free char *contents = NULL;
+	gs_free char *proc_cmdline = NULL;
 	gsize len = 0;
 	glong pid;
-	char *proc_cmdline = NULL;
-	gboolean nm_running = FALSE;
 	const char *process_name;
+	const char *prgname = g_get_prgname ();
 
-	/* Setup runtime directory */
-	if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) {
-		nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMRUNDIR, strerror (errno));
-		exit (1);
-	}
+	g_return_if_fail (prgname);
 
-	if (!g_file_get_contents (pidfile, &contents, &len, NULL))
-		return FALSE;
+	if (!pidfile || !*pidfile)
+		return;
 
+	if (!g_file_get_contents (pidfile, &contents, &len, NULL))
+		return;
 	if (len <= 0)
-		goto done;
+		return;
 
 	errno = 0;
 	pid = strtol (contents, NULL, 10);
 	if (pid <= 0 || pid > 65536 || errno)
-		goto done;
+		return;
 
-	g_free (contents);
+	g_clear_pointer (&contents, g_free);
 	proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid);
 	if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL))
-		goto done;
+		return;
 
 	process_name = strrchr (contents, '/');
 	if (process_name)
 		process_name++;
 	else
 		process_name = contents;
-	if (strcmp (process_name, name) == 0) {
+	if (strcmp (process_name, prgname) == 0) {
 		/* Check that the process exists */
 		if (kill (pid, 0) == 0) {
-			fprintf (stderr, _("%s is already running (pid %ld)\n"), name, pid);
-			nm_running = TRUE;
+			fprintf (stderr, _("%s is already running (pid %ld)\n"), prgname, pid);
+			exit (1);
 		}
 	}
+}
 
-done:
-	g_free (proc_cmdline);
-	g_free (contents);
-	return nm_running;
+void
+nm_main_utils_ensure_root ()
+{
+	if (getuid () != 0) {
+		fprintf (stderr, _("You must be root to run %s!\n"), str_if_set (g_get_prgname (), ""));
+		exit (1);
+	}
 }
 
 gboolean
 nm_main_utils_early_setup (const char *progname,
-                           char **argv[],
                            int *argc,
+                           char **argv[],
                            GOptionEntry *options,
                            GOptionEntry *more_options,
                            const char *summary)
@@ -226,6 +238,8 @@ nm_main_utils_early_setup (const char *progname,
 	GError *error = NULL;
 	gboolean success = FALSE;
 	int i;
+	const char *opt_fmt_log_level = NULL, *opt_fmt_log_domains = NULL;
+	const char **opt_loc_log_level = NULL, **opt_loc_log_domains = NULL;
 
 	/* Make GIO ignore the remote VFS service; otherwise it tries to use the
 	 * session bus to contact the remote service, and NM shouldn't ever be
@@ -244,16 +258,16 @@ nm_main_utils_early_setup (const char *progname,
 	setlocale (LC_ALL, "");
 	textdomain (GETTEXT_PACKAGE);
 
-	if (getuid () != 0) {
-		fprintf (stderr, _("You must be root to run %s!\n"), progname);
-		exit (1);
-	}
-
 	for (i = 0; options[i].long_name; i++) {
-		if (!strcmp (options[i].long_name, "log-level"))
+		if (!strcmp (options[i].long_name, "log-level")) {
+			opt_fmt_log_level = options[i].description;
+			opt_loc_log_level = &options[i].description;
 			options[i].description = g_strdup_printf (options[i].description, nm_logging_all_levels_to_string ());
-		else if (!strcmp (options[i].long_name, "log-domains"))
+		} else if (!strcmp (options[i].long_name, "log-domains")) {
+			opt_fmt_log_domains = options[i].description;
+			opt_loc_log_domains = &options[i].description;
 			options[i].description = g_strdup_printf (options[i].description, nm_logging_all_domains_to_string ());
+		}
 	}
 
 	/* Parse options */
@@ -274,6 +288,15 @@ nm_main_utils_early_setup (const char *progname,
 	}
 	g_option_context_free (opt_ctx);
 
+	if (opt_loc_log_level) {
+		g_free ((char *) *opt_loc_log_level);
+		*opt_loc_log_level = opt_fmt_log_level;
+	}
+	if (opt_loc_log_domains) {
+		g_free ((char *) *opt_loc_log_domains);
+		*opt_loc_log_domains = opt_fmt_log_domains;
+	}
+
 	return success;
 }