Use basename properly

This commit is contained in:
Roy Marples 2007-04-13 13:54:53 +00:00
parent cb20c5da98
commit fc06063958

View File

@ -11,12 +11,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <errno.h> #include <errno.h>
#ifndef __linux__
/* Although linux should work fine, gcc likes to bitch with our default
CFLAGS so we just don't include the file and use the GNU one defined
in string.h */
#include <libgen.h> #include <libgen.h>
#endif
#include <limits.h> #include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
@ -188,6 +183,7 @@ bool rc_service_in_runlevel (const char *service, const char *runlevel)
{ {
char *file; char *file;
bool retval; bool retval;
char *svc;
if (! runlevel || ! service) if (! runlevel || ! service)
return (false); return (false);
@ -195,8 +191,10 @@ bool rc_service_in_runlevel (const char *service, const char *runlevel)
if (! rc_service_exists (service)) if (! rc_service_exists (service))
return (false); return (false);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), svc = rc_xstrdup (service);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
retval = rc_exists (file); retval = rc_exists (file);
free (file); free (file);
@ -209,17 +207,20 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
int i = 0; int i = 0;
int skip_state = -1; int skip_state = -1;
char *base; char *base;
char *svc;
char *init = rc_resolve_service (service); char *init = rc_resolve_service (service);
bool skip_wasinactive = false; bool skip_wasinactive = false;
if (! service) if (! service)
return (false); return (false);
base = basename (service); svc = rc_xstrdup (service);
base = basename (svc);
if (state != rc_service_stopped) { if (state != rc_service_stopped) {
if (! rc_is_file(init)) { if (! rc_is_file(init)) {
free (init); free (init);
free (svc);
return (false); return (false);
} }
@ -231,6 +232,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
if (i != 0) { if (i != 0) {
free (file); free (file);
free (init); free (init);
free (svc);
einfo ("%d %s %s", state, rc_service_state_names[state], base); einfo ("%d %s %s", state, rc_service_state_names[state], base);
eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno)); eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno));
return (false); return (false);
@ -242,6 +244,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
if (state == rc_service_coldplugged) { if (state == rc_service_coldplugged) {
free (init); free (init);
free (svc);
return (true); return (true);
} }
@ -337,6 +340,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
free (sdir); free (sdir);
} }
free (svc);
free (init); free (init);
return (true); return (true);
} }
@ -345,6 +349,7 @@ bool rc_service_state (const char *service, const rc_service_state_t state)
{ {
char *file; char *file;
bool retval; bool retval;
char *svc;
/* If the init script does not exist then we are stopped */ /* If the init script does not exist then we are stopped */
if (! rc_service_exists (service)) if (! rc_service_exists (service))
@ -370,8 +375,10 @@ bool rc_service_state (const char *service, const rc_service_state_t state)
/* Now we just check if a file by the service name rc_exists /* Now we just check if a file by the service name rc_exists
in the state dir */ in the state dir */
svc = rc_xstrdup (service);
file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state],
basename (service), (char*) NULL); basename (svc), (char*) NULL);
free (svc);
retval = rc_exists (file); retval = rc_exists (file);
free (file); free (file);
return (retval); return (retval);
@ -442,6 +449,7 @@ static pid_t _exec_service (const char *service, const char *arg)
pid_t pid = -1; pid_t pid = -1;
pid_t savedpid; pid_t savedpid;
int status; int status;
char *svc;
file = rc_resolve_service (service); file = rc_resolve_service (service);
if (! rc_is_file (file)) { if (! rc_is_file (file)) {
@ -451,8 +459,10 @@ static pid_t _exec_service (const char *service, const char *arg)
} }
/* We create a fifo so that other services can wait until we complete */ /* We create a fifo so that other services can wait until we complete */
fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service), svc = rc_xstrdup (service);
fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) { if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) {
eerror ("unable to create fifo `%s': %s", fifo, strerror (errno)); eerror ("unable to create fifo `%s': %s", fifo, strerror (errno));
@ -520,13 +530,16 @@ void rc_schedule_start_service (const char *service,
char *dir; char *dir;
char *init; char *init;
char *file; char *file;
char *svc;
/* service may be a provided service, like net */ /* service may be a provided service, like net */
if (! service || ! rc_service_exists (service_to_start)) if (! service || ! rc_service_exists (service_to_start))
return; return;
dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), svc = rc_xstrdup (service);
dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
if (! rc_is_dir (dir)) if (! rc_is_dir (dir))
if (mkdir (dir, 0755) != 0) { if (mkdir (dir, 0755) != 0) {
eerror ("mkdir `%s': %s", dir, strerror (errno)); eerror ("mkdir `%s': %s", dir, strerror (errno));
@ -535,7 +548,9 @@ void rc_schedule_start_service (const char *service,
} }
init = rc_resolve_service (service_to_start); init = rc_resolve_service (service_to_start);
file = rc_strcatpaths (dir, basename (service_to_start), (char *) NULL); svc = rc_xstrdup (service_to_start);
file = rc_strcatpaths (dir, basename (svc), (char *) NULL);
free (svc);
if (! rc_exists (file) && symlink (init, file) != 0) if (! rc_exists (file) && symlink (init, file) != 0)
eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno)); eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno));
@ -546,9 +561,11 @@ void rc_schedule_start_service (const char *service,
void rc_schedule_clear (const char *service) void rc_schedule_clear (const char *service)
{ {
char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), char *svc = rc_xstrdup (service);
char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
if (rc_is_dir (dir)) if (rc_is_dir (dir))
rc_rm_dir (dir, true); rc_rm_dir (dir, true);
free (dir); free (dir);
@ -556,13 +573,15 @@ void rc_schedule_clear (const char *service)
bool rc_wait_service (const char *service) bool rc_wait_service (const char *service)
{ {
char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service), char *svc = rc_xstrdup (service);
char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc),
(char *) NULL); (char *) NULL);
struct timeval tv; struct timeval tv;
struct timeval stopat; struct timeval stopat;
struct timeval now; struct timeval now;
bool retval = false; bool retval = false;
free (svc);
if (gettimeofday (&stopat, NULL) != 0) { if (gettimeofday (&stopat, NULL) != 0) {
eerror ("gettimeofday: %s", strerror (errno)); eerror ("gettimeofday: %s", strerror (errno));
return (false); return (false);
@ -660,6 +679,7 @@ bool rc_service_add (const char *runlevel, const char *service)
bool retval; bool retval;
char *init; char *init;
char *file; char *file;
char *svc;
if (! rc_runlevel_exists (runlevel)) { if (! rc_runlevel_exists (runlevel)) {
errno = ENOENT; errno = ENOENT;
@ -672,8 +692,10 @@ bool rc_service_add (const char *runlevel, const char *service)
} }
init = rc_resolve_service (service); init = rc_resolve_service (service);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), svc = rc_xstrdup (service);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
retval = (symlink (init, file) == 0); retval = (symlink (init, file) == 0);
free (init); free (init);
free (file); free (file);
@ -683,13 +705,16 @@ bool rc_service_add (const char *runlevel, const char *service)
bool rc_service_delete (const char *runlevel, const char *service) bool rc_service_delete (const char *runlevel, const char *service)
{ {
char *file; char *file;
char *svc;
bool retval = false; bool retval = false;
if (! runlevel || ! service) if (! runlevel || ! service)
return (false); return (false);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), svc = rc_xstrdup (service);
file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
(char *) NULL); (char *) NULL);
free (svc);
if (unlink (file) == 0) if (unlink (file) == 0)
retval = true; retval = true;
@ -718,13 +743,15 @@ char **rc_services_scheduled_by (const char *service)
char **rc_services_scheduled (const char *service) char **rc_services_scheduled (const char *service)
{ {
char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), char *svc = rc_xstrdup (service);
char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
(char *) NULL); (char *) NULL);
char **list = NULL; char **list = NULL;
if (rc_is_dir (dir)) if (rc_is_dir (dir))
list = rc_ls_dir (list, dir, RC_LS_INITD); list = rc_ls_dir (list, dir, RC_LS_INITD);
free (svc);
free (dir); free (dir);
return (list); return (list);
} }
@ -740,7 +767,7 @@ bool rc_allow_plug (char *service)
if (! match) if (! match)
return true; return true;
list = strdup (match); list = rc_xstrdup (match);
p = list; p = list;
while ((token = strsep (&p, " "))) { while ((token = strsep (&p, " "))) {
bool truefalse = true; bool truefalse = true;