From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1SQtq7-0000Vc-JB for garchives@archives.gentoo.org; Sun, 06 May 2012 05:16:59 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 455A9E0675; Sun, 6 May 2012 05:16:46 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 052E3E0675 for ; Sun, 6 May 2012 05:16:45 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 465F71B404E for ; Sun, 6 May 2012 05:16:45 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 06E75E542E for ; Sun, 6 May 2012 05:16:42 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1336281450.2486eb4989918ff5ef645f2a17fedbec559b9485.vapier@OpenRC> Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/ X-VCS-Repository: proj/openrc X-VCS-Files: src/rc/rc-applets.c X-VCS-Directories: src/rc/ X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: 2486eb4989918ff5ef645f2a17fedbec559b9485 X-VCS-Branch: master Date: Sun, 6 May 2012 05:16:42 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: b11ee12e-1deb-434b-9a01-865dd4174a97 X-Archives-Hash: 81e8580a0fdb958ba7d1ff923826604a commit: 2486eb4989918ff5ef645f2a17fedbec559b9485 Author: Mike Frysinger gentoo org> AuthorDate: Sun May 6 00:28:28 2012 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Sun May 6 05:17:30 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/openrc.git;a=3D= commit;h=3D2486eb49 rc: collapse the applet if statements into a single array walk Signed-off-by: Mike Frysinger gentoo.org> --- src/rc/rc-applets.c | 240 ++++++++++++++++++++++++++++-----------------= ------ 1 files changed, 133 insertions(+), 107 deletions(-) diff --git a/src/rc/rc-applets.c b/src/rc/rc-applets.c index 5d02682..21562f0 100644 --- a/src/rc/rc-applets.c +++ b/src/rc/rc-applets.c @@ -253,13 +253,38 @@ do_e(int argc, char **argv) return retval; } =20 +static const struct { + const char * const name; + RC_SERVICE bit; +} service_bits[] =3D { + { "service_started", RC_SERVICE_STARTED, }, + { "service_stopped", RC_SERVICE_STOPPED, }, + { "service_inactive", RC_SERVICE_INACTIVE, }, + { "service_starting", RC_SERVICE_STARTING, }, + { "service_stopping", RC_SERVICE_STOPPING, }, + { "service_hotplugged", RC_SERVICE_HOTPLUGGED, }, + { "service_wasinactive", RC_SERVICE_WASINACTIVE, }, + { "service_failed", RC_SERVICE_FAILED, }, +}; + +static RC_SERVICE +lookup_service_state(const char *service) +{ + size_t i; + for (i =3D 0; i < ARRAY_SIZE(service_bits); ++i) + if (!strcmp(service, service_bits[i].name)) + return service_bits[i].bit; + return 0; +} + static int do_service(int argc, char **argv) { bool ok =3D false; char *service; char *exec; - int idx =3D 0; + int idx; + RC_SERVICE state, bit; =20 if (argc > 1) service =3D argv[1]; @@ -269,21 +294,11 @@ do_service(int argc, char **argv) if (service =3D=3D NULL || *service =3D=3D '\0') eerrorx("%s: no service specified", applet); =20 - if (strcmp(applet, "service_started") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_STARTED); - else if (strcmp(applet, "service_stopped") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_STOPPED); - else if (strcmp(applet, "service_inactive") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_INACTIVE); - else if (strcmp(applet, "service_starting") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_STARTING); - else if (strcmp(applet, "service_stopping") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_STOPPING); - else if (strcmp(applet, "service_hotplugged") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_HOTPLUGGED); - else if (strcmp(applet, "service_wasinactive") =3D=3D 0) - ok =3D (rc_service_state(service) & RC_SERVICE_WASINACTIVE); - else if (strcmp(applet, "service_started_daemon") =3D=3D 0) { + state =3D rc_service_state(service); + bit =3D lookup_service_state(service); + if (bit) { + ok =3D (state & bit); + } else if (strcmp(applet, "service_started_daemon") =3D=3D 0) { service =3D getenv("RC_SVCNAME"); exec =3D argv[1]; if (argc > 3) { @@ -317,6 +332,7 @@ do_mark_service(int argc, char **argv) char *runscript_pid; /* char *mtime; */ pid_t pid; + RC_SERVICE bit; /* size_t l; */ =20 if (argc > 1) @@ -327,20 +343,9 @@ do_mark_service(int argc, char **argv) if (service =3D=3D NULL || *service =3D=3D '\0') eerrorx("%s: no service specified", applet); =20 - if (strcmp(applet, "mark_service_started") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_STARTED); - else if (strcmp(applet, "mark_service_stopped") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_STOPPED); - else if (strcmp(applet, "mark_service_inactive") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_INACTIVE); - else if (strcmp(applet, "mark_service_starting") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_STARTING); - else if (strcmp(applet, "mark_service_stopping") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_STOPPING); - else if (strcmp(applet, "mark_service_hotplugged") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_HOTPLUGGED); - else if (strcmp(applet, "mark_service_failed") =3D=3D 0) - ok =3D rc_service_mark(service, RC_SERVICE_FAILED); + if (strncmp(applet, "mark_", 5) && + (bit =3D lookup_service_state(applet + 5))) + ok =3D rc_service_mark(service, bit); else eerrorx("%s: unknown applet", applet); =20 @@ -409,7 +414,7 @@ do_value(int argc, char **argv) } =20 static int -do_shell_var(int argc, char **argv) +shell_var(int argc, char **argv) { int i; char *p; @@ -430,106 +435,127 @@ do_shell_var(int argc, char **argv) return EXIT_SUCCESS; } =20 +static int +is_older_than(int argc, char **argv) +{ + int i; + + if (argc < 3) + return EXIT_FAILURE; + + /* This test is perverted - historically the baselayout function + * returns 0 on *failure*, which is plain wrong */ + for (i =3D 2; i < argc; ++i) + if (!rc_newer_than(argv[1], argv[i], NULL, NULL)) + return EXIT_SUCCESS; + + return EXIT_FAILURE; +} + +static int +is_newer_than(int argc, char **argv) +{ + int i; + + if (argc < 3) + return EXIT_FAILURE; + + /* This test is correct as it's not present in baselayout */ + for (i =3D 2; i < argc; ++i) + if (!rc_newer_than(argv[1], argv[i], NULL, NULL)) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + +static int +is_runlevel_start(_unused int argc, _unused char **argv) +{ + return rc_runlevel_starting() ? 0 : 1; +} + +static int +is_runlevel_stop(_unused int argc, _unused char **argv) +{ + return rc_runlevel_stopping() ? 0 : 1; +} + +static int +rc_abort(_unused int argc, _unused char **argv) +{ + const char *p =3D getenv("RC_PID"); + int pid; + + if (p && sscanf(p, "%d", &pid) =3D=3D 1) { + if (kill(pid, SIGUSR1) !=3D 0) + eerrorx("rc-abort: failed to signal parent %d: %s", + pid, strerror(errno)); + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; +} + +static const struct { + const char * const name; + int (* const applet)(int argc, char **argv); +} applets[] =3D { +#define A(a) { #a, a } + A(fstabinfo), + A(mountinfo), + { "rc-depend", rc_depend, }, + { "rc-service", rc_service, }, + { "rc-status", rc_status, }, + { "rc-update", rc_update, }, + { "update-rc", rc_update, }, + A(runscript), + { "start-stop-daemon", start_stop_daemon, }, + A(checkpath), + A(swclock), + A(shell_var), + A(is_older_than), + A(is_newer_than), + A(is_runlevel_start), + A(is_runlevel_stop), + { "rc-abort", rc_abort, }, + /* These are purely for init scripts and do not make sense as + * anything else */ + { "service_get_value", do_value, }, + { "service_set_value", do_value, }, + { "get_options", do_value, }, + { "save_options", do_value, }, +#undef A +}; + void run_applets(int argc, char **argv) { - int i =3D 2; - char *p; - pid_t pid =3D 0; + size_t i; =20 /* Bug 351712: We need an extra way to explicitly select an applet OTHE= R * than trusting argv[0], as argv[0] is not going to be the applet valu= e if * we are doing SELinux context switching. For this, we allow calls suc= h as * 'rc --applet APPLET', and shift ALL of argv down by two array items.= */ if (strcmp(applet, "rc") =3D=3D 0 && argc >=3D 3 && - (strcmp(argv[1],"--applet") =3D=3D 0 || strcmp(argv[1], "-a") =3D=3D= 0)) { + (strcmp(argv[1],"--applet") =3D=3D 0 || strcmp(argv[1], "-a") =3D=3D 0= )) { applet =3D argv[2]; argv +=3D 2; argc -=3D 2; } =20 - /* These are designed to be applications in their own right */ - if (strcmp(applet, "fstabinfo") =3D=3D 0) - exit(fstabinfo(argc, argv)); - else if (strcmp(applet, "mountinfo") =3D=3D 0) - exit(mountinfo(argc, argv)); - else if (strcmp(applet, "rc-depend") =3D=3D 0) - exit(rc_depend(argc, argv)); - else if (strcmp(applet, "rc-service") =3D=3D 0) - exit(rc_service(argc, argv)); - else if (strcmp(applet, "rc-status") =3D=3D 0) - exit(rc_status(argc, argv)); - else if (strcmp(applet, "rc-update") =3D=3D 0 || - strcmp(applet, "update-rc") =3D=3D 0) - exit(rc_update(argc, argv)); - else if (strcmp(applet, "runscript") =3D=3D 0) - exit(runscript(argc, argv)); - else if (strcmp(applet, "start-stop-daemon") =3D=3D 0) - exit(start_stop_daemon(argc, argv)); - else if (strcmp (applet, "checkpath") =3D=3D 0) - exit(checkpath(argc, argv)); - else if (strcmp(applet, "swclock") =3D=3D 0) - exit(swclock(argc, argv)); - - /* These could also be applications in their own right */ - if (strcmp(applet, "shell_var") =3D=3D 0) - exit(do_shell_var(argc, argv)); - - /* This test is perverted - historically the baselayout function - * returns 0 on *failure*, which is plain wrong */ - if (strcmp(applet, "is_older_than") =3D=3D 0) { - if (argc < 3) - exit (EXIT_FAILURE); - while (i < argc) { - if (!rc_newer_than(argv[1], argv[i++], NULL, NULL)) - exit(EXIT_SUCCESS); - } - exit(EXIT_FAILURE); - }; - /* This test is correct as it's not present in baselayout */ - if (strcmp(applet, "is_newer_than") =3D=3D 0) { - if (argc < 3) - exit (EXIT_FAILURE); - while (i < argc) { - if (!rc_newer_than(argv[1], argv[i++], NULL, NULL)) - exit(EXIT_FAILURE); - } - exit(EXIT_SUCCESS); - }; + for (i =3D 0; i < ARRAY_SIZE(applets); ++i) + if (!strcmp(applet, applets[i].name)) + exit(applets[i].applet(argc, argv)); =20 if (applet[0] =3D=3D 'e' || (applet[0] =3D=3D 'v' && applet[1] =3D=3D '= e')) exit(do_e(argc, argv)); =20 - /* These are purely for init scripts and do not make sense as - * anything else */ - if (strcmp(applet, "service_get_value") =3D=3D 0 || - strcmp(applet, "service_set_value") =3D=3D 0 || - strcmp(applet, "get_options") =3D=3D 0 || - strcmp(applet, "save_options") =3D=3D 0) - exit(do_value(argc, argv)); - if (strncmp(applet, "service_", strlen("service_")) =3D=3D 0) exit(do_service(argc, argv)); =20 if (strncmp(applet, "mark_service_", strlen("mark_service_")) =3D=3D 0) exit(do_mark_service(argc, argv)); =20 - if (strcmp(applet, "is_runlevel_start") =3D=3D 0) - exit(rc_runlevel_starting() ? 0 : 1); - else if (strcmp (applet, "is_runlevel_stop") =3D=3D 0) - exit(rc_runlevel_stopping() ? 0 : 1); - - if (strcmp(applet, "rc-abort") =3D=3D 0) { - p =3D getenv("RC_PID"); - if (p && sscanf(p, "%d", &pid) =3D=3D 1) { - if (kill(pid, SIGUSR1) !=3D 0) - eerrorx("rc-abort: failed to signal parent %d: %s", - pid, strerror(errno)); - exit(EXIT_SUCCESS); - } - exit(EXIT_FAILURE); - } - if (strcmp(applet, "rc") !=3D 0) eerrorx("%s: unknown applet", applet); }