Add help to rc-update #188170 and tidy up the code a fair bit.
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
			
		||||
 | 
			
		||||
  08 Aug 2007; Roy Marples <uberlord@gentoo.org>:
 | 
			
		||||
 | 
			
		||||
    Add help to rc-update #188170.
 | 
			
		||||
    If given a pidfile, just match on that for seeing if we have crashed
 | 
			
		||||
	or not, #186159.
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -257,9 +257,6 @@ bool rc_service_in_runlevel (const char *service, const char *runlevel)
 | 
			
		||||
	if (! runlevel || ! service)
 | 
			
		||||
		return (false);
 | 
			
		||||
 | 
			
		||||
	if (! rc_service_exists (service))
 | 
			
		||||
		return (false);
 | 
			
		||||
 | 
			
		||||
	svc = rc_xstrdup (service);
 | 
			
		||||
	file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
 | 
			
		||||
						   (char *) NULL);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										138
									
								
								src/rc-update.c
									
									
									
									
									
								
							
							
						
						
									
										138
									
								
								src/rc-update.c
									
									
									
									
									
								
							@@ -5,6 +5,8 @@
 | 
			
		||||
   Released under the GPLv2
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
#define APPLET "rc-update"
 | 
			
		||||
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <limits.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
@@ -21,31 +23,76 @@
 | 
			
		||||
 | 
			
		||||
static char *applet = NULL;
 | 
			
		||||
 | 
			
		||||
static void usage (void )
 | 
			
		||||
{
 | 
			
		||||
	printf (
 | 
			
		||||
			"usage: %s -a|add script runlevel1 [runlevel2 ...]\n"
 | 
			
		||||
			"       %s -d|del script [runlevel1 ...]\n"
 | 
			
		||||
			"       %s -s|show [-v|--verbose] [runlevel1 ...]\n"
 | 
			
		||||
			"       %s -h|help\n"
 | 
			
		||||
			"\n"
 | 
			
		||||
			"examples:\n"
 | 
			
		||||
			"       # %s add net.eth0 default\n"
 | 
			
		||||
			"       Adds the net.eth0 script (in /etc/init.d) to the `default' runlevel.\n"
 | 
			
		||||
			"\n"
 | 
			
		||||
			"       # %s del sysklogd\n"
 | 
			
		||||
			"       Deletes the sysklogd script from all runlevels.  The original script\n"
 | 
			
		||||
			"       is not deleted, just any symlinks to the script in /etc/runlevels/*.\n"
 | 
			
		||||
			"\n"
 | 
			
		||||
			"       # %s del net.eth2 default wumpus\n"
 | 
			
		||||
			"       Delete the net.eth2 script from the default and wumpus runlevels.\n"
 | 
			
		||||
			"       All other runlevels are unaffected.  Again, the net.eth2 script\n"
 | 
			
		||||
			"       residing in /etc/init.d is not deleted, just any symlinks in\n"
 | 
			
		||||
			"       /etc/runlevels/default and /etc/runlevels/wumpus.\n"
 | 
			
		||||
			"\n"
 | 
			
		||||
			"       # %s show\n"
 | 
			
		||||
			"       Show all enabled scripts and list at which runlevels they will\n"
 | 
			
		||||
			"       execute.  Run with --verbose to see all available scripts.\n",
 | 
			
		||||
		applet, applet, applet, applet, applet, applet, applet, applet);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool add (const char *runlevel, const char *service)
 | 
			
		||||
{
 | 
			
		||||
	bool retval = true;
 | 
			
		||||
	bool retval = false;
 | 
			
		||||
 | 
			
		||||
	if (! rc_runlevel_exists (runlevel)) {
 | 
			
		||||
		ewarn ("runlevel `%s' does not exist", runlevel);
 | 
			
		||||
		return (false);
 | 
			
		||||
	}
 | 
			
		||||
	if (rc_service_in_runlevel (service, runlevel)) {
 | 
			
		||||
	if (! rc_service_exists (service))
 | 
			
		||||
		eerror ("service `%s' does not exist", service);
 | 
			
		||||
	else if (! rc_runlevel_exists (runlevel))
 | 
			
		||||
		eerror ("runlevel `%s' does not exist", runlevel);
 | 
			
		||||
	else if (rc_service_in_runlevel (service, runlevel)) {
 | 
			
		||||
		ewarn ("%s already installed in runlevel `%s'; skipping",
 | 
			
		||||
			   service, runlevel);
 | 
			
		||||
		return (false);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (rc_service_add (runlevel, service))
 | 
			
		||||
		retval = true;
 | 
			
		||||
	} else if (rc_service_add (runlevel, service)) {
 | 
			
		||||
		einfo ("%s added to runlevel %s", service, runlevel);
 | 
			
		||||
	else {
 | 
			
		||||
		retval = true;
 | 
			
		||||
	} else
 | 
			
		||||
		eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
 | 
			
		||||
				applet, service, runlevel, strerror (errno));
 | 
			
		||||
		retval = false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return (retval);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool delete (const char *runlevel, const char *service)
 | 
			
		||||
{
 | 
			
		||||
	bool retval = false;
 | 
			
		||||
 | 
			
		||||
	if (rc_service_in_runlevel (service, runlevel))	{
 | 
			
		||||
		if (rc_service_delete (runlevel, service)) {
 | 
			
		||||
			einfo ("%s removed from runlevel %s", service, runlevel);
 | 
			
		||||
			retval = true;
 | 
			
		||||
		} else
 | 
			
		||||
			eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
 | 
			
		||||
					applet, service, runlevel, strerror (errno));
 | 
			
		||||
	} else if (! rc_service_exists (service))
 | 
			
		||||
		eerror ("service `%s' does not exist", service);
 | 
			
		||||
	else if (! rc_runlevel_exists (runlevel))
 | 
			
		||||
		eerror ("runlevel `%s' does not exist", runlevel);
 | 
			
		||||
	else
 | 
			
		||||
		retval = true;
 | 
			
		||||
	return (retval);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int rc_update (int argc, char **argv)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
@@ -53,6 +100,8 @@ int rc_update (int argc, char **argv)
 | 
			
		||||
	char *service;
 | 
			
		||||
	char **runlevels = NULL;
 | 
			
		||||
	char *runlevel;
 | 
			
		||||
	bool doadd;
 | 
			
		||||
	int retval;
 | 
			
		||||
 | 
			
		||||
	applet = argv[0];
 | 
			
		||||
	if (argc < 2 ||
 | 
			
		||||
@@ -96,8 +145,18 @@ int rc_update (int argc, char **argv)
 | 
			
		||||
			STRLIST_FOREACH (in, runlevel, j)
 | 
			
		||||
				printf (" %s", runlevel);
 | 
			
		||||
			printf ("\n");
 | 
			
		||||
			rc_strlist_free (in);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		rc_strlist_free (runlevels);
 | 
			
		||||
		rc_strlist_free (services);
 | 
			
		||||
		return (EXIT_SUCCESS);
 | 
			
		||||
	} else if (argc > 1 &&
 | 
			
		||||
			   (strcmp (argv[1], "help") == 0 ||
 | 
			
		||||
				strcmp (argv[1], "--help") == 0 ||
 | 
			
		||||
				strcmp (argv[1], "-h") == 0))
 | 
			
		||||
	{
 | 
			
		||||
		usage ();
 | 
			
		||||
		return (EXIT_SUCCESS);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -110,43 +169,32 @@ int rc_update (int argc, char **argv)
 | 
			
		||||
 | 
			
		||||
	if (strcmp (argv[1], "add") == 0 ||
 | 
			
		||||
		strcmp (argv[1], "-a") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (! service) 
 | 
			
		||||
			eerrorx ("%s: no service specified", applet);
 | 
			
		||||
		if (! rc_service_exists (service))
 | 
			
		||||
			eerrorx ("%s: service `%s' does not exist", applet, service);
 | 
			
		||||
 | 
			
		||||
		if (argc < 4)
 | 
			
		||||
			add (rc_get_runlevel (), service);
 | 
			
		||||
 | 
			
		||||
		for (i = 3; i < argc; i++)
 | 
			
		||||
			add (argv[i], service);
 | 
			
		||||
 | 
			
		||||
		return (EXIT_SUCCESS);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (strcmp (argv[1], "delete") == 0 ||
 | 
			
		||||
		doadd = true;
 | 
			
		||||
	else if (strcmp (argv[1], "delete") == 0 ||
 | 
			
		||||
		strcmp (argv[1], "del") == 0 ||
 | 
			
		||||
		strcmp (argv[1], "-d") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		for (i = 3; i < argc; i++)
 | 
			
		||||
			runlevels = rc_strlist_add (runlevels, argv[i]);
 | 
			
		||||
		doadd = false;
 | 
			
		||||
	else
 | 
			
		||||
		eerrorx ("%s: unknown command `%s'", applet, argv[1]);
 | 
			
		||||
 | 
			
		||||
		if (! runlevels)
 | 
			
		||||
			runlevels = rc_strlist_add (runlevels, rc_get_runlevel ()); 
 | 
			
		||||
	for (i = 3; i < argc; i++)
 | 
			
		||||
		runlevels = rc_strlist_add (runlevels, argv[i]);
 | 
			
		||||
 | 
			
		||||
		STRLIST_FOREACH (runlevels, runlevel, i) {
 | 
			
		||||
			if (rc_service_in_runlevel (service, runlevel))	{
 | 
			
		||||
				if (rc_service_delete (runlevel, service))
 | 
			
		||||
					einfo ("%s removed from runlevel %s", service, runlevel);
 | 
			
		||||
				else
 | 
			
		||||
					eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
 | 
			
		||||
							applet, service, runlevel, strerror (errno));
 | 
			
		||||
			}
 | 
			
		||||
	if (! runlevels)
 | 
			
		||||
		runlevels = rc_strlist_add (runlevels, rc_get_runlevel ()); 
 | 
			
		||||
 | 
			
		||||
	retval = EXIT_SUCCESS;
 | 
			
		||||
	STRLIST_FOREACH (runlevels, runlevel, i) {
 | 
			
		||||
		if (doadd) {
 | 
			
		||||
			if (! add (runlevel, service))
 | 
			
		||||
				retval = EXIT_FAILURE;
 | 
			
		||||
		} else {
 | 
			
		||||
			if (! delete (runlevel, service))
 | 
			
		||||
				retval = EXIT_FAILURE;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return (EXIT_SUCCESS);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	eerrorx ("%s: unknown command `%s'", applet, argv[1]);
 | 
			
		||||
	rc_strlist_free (runlevels);
 | 
			
		||||
 | 
			
		||||
	return (retval);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user