openrc-run: clean up string handling

- remove references to PATH_MAX
- use xasprintf to create strings
This commit is contained in:
William Hubbs 2018-02-15 15:10:24 -06:00
parent 9e14b35da8
commit 488d8989c5

View File

@ -109,7 +109,7 @@ static void
handle_signal(int sig) handle_signal(int sig)
{ {
int serrno = errno; int serrno = errno;
char signame[10] = { '\0' }; char *signame = NULL;
struct winsize ws; struct winsize ws;
switch (sig) { switch (sig) {
@ -134,20 +134,22 @@ handle_signal(int sig)
break; break;
case SIGINT: case SIGINT:
if (!signame[0]) if (!signame)
snprintf(signame, sizeof(signame), "SIGINT"); xasprintf(&signame, "SIGINT");
/* FALLTHROUGH */ /* FALLTHROUGH */
case SIGTERM: case SIGTERM:
if (!signame[0]) if (!signame)
snprintf(signame, sizeof(signame), "SIGTERM"); xasprintf(&signame, "SIGTERM");
/* FALLTHROUGH */ /* FALLTHROUGH */
case SIGQUIT: case SIGQUIT:
if (!signame[0]) if (!signame)
snprintf(signame, sizeof(signame), "SIGQUIT"); xasprintf(&signame, "SIGQUIT");
/* Send the signal to our children too */ /* Send the signal to our children too */
if (service_pid > 0) if (service_pid > 0)
kill(service_pid, sig); kill(service_pid, sig);
eerrorx("%s: caught %s, aborting", applet, signame); eerror("%s: caught %s, aborting", applet, signame);
free(signame);
exit(EXIT_FAILURE);
/* NOTREACHED */ /* NOTREACHED */
default: default:
@ -161,11 +163,12 @@ handle_signal(int sig)
static void static void
unhotplug() unhotplug()
{ {
char file[PATH_MAX]; char *file = NULL;
snprintf(file, sizeof(file), RC_SVCDIR "/hotplugged/%s", applet); xasprintf(&file, RC_SVCDIR "/hotplugged/%s", applet);
if (exists(file) && unlink(file) != 0) if (exists(file) && unlink(file) != 0)
eerror("%s: unlink `%s': %s", applet, file, strerror(errno)); eerror("%s: unlink `%s': %s", applet, file, strerror(errno));
free(file);
} }
static void static void
@ -485,7 +488,7 @@ svc_exec(const char *arg1, const char *arg2)
static bool static bool
svc_wait(const char *svc) svc_wait(const char *svc)
{ {
char file[PATH_MAX]; char *file = NULL;
int fd; int fd;
bool forever = false; bool forever = false;
RC_STRINGLIST *keywords; RC_STRINGLIST *keywords;
@ -498,8 +501,7 @@ svc_wait(const char *svc)
forever = true; forever = true;
rc_stringlist_free(keywords); rc_stringlist_free(keywords);
snprintf(file, sizeof(file), RC_SVCDIR "/exclusive/%s", xasprintf(&file, RC_SVCDIR "/exclusive/%s", basename_c(svc));
basename_c(svc));
interval.tv_sec = 0; interval.tv_sec = 0;
interval.tv_nsec = WAIT_INTERVAL; interval.tv_nsec = WAIT_INTERVAL;
@ -512,23 +514,29 @@ svc_wait(const char *svc)
if (fd != -1) { if (fd != -1) {
if (flock(fd, LOCK_SH | LOCK_NB) == 0) { if (flock(fd, LOCK_SH | LOCK_NB) == 0) {
close(fd); close(fd);
free(file);
return true; return true;
} }
close(fd); close(fd);
} }
if (errno == ENOENT) if (errno == ENOENT) {
free(file);
return true; return true;
if (errno != EWOULDBLOCK) }
eerrorx("%s: open `%s': %s", applet, file, if (errno != EWOULDBLOCK) {
eerror("%s: open `%s': %s", applet, file,
strerror(errno)); strerror(errno));
free(file);
exit(EXIT_FAILURE);
}
if (nanosleep(&interval, NULL) == -1) { if (nanosleep(&interval, NULL) == -1) {
if (errno != EINTR) if (errno != EINTR)
return false; goto finish;
} }
if (!forever) { if (!forever) {
timespecsub(&timeout, &interval, &timeout); timespecsub(&timeout, &interval, &timeout);
if (timeout.tv_sec <= 0) if (timeout.tv_sec <= 0)
return false; goto finish;
timespecsub(&warn, &interval, &warn); timespecsub(&warn, &interval, &warn);
if (warn.tv_sec <= 0) { if (warn.tv_sec <= 0) {
ewarn("%s: waiting for %s (%d seconds)", ewarn("%s: waiting for %s (%d seconds)",
@ -538,6 +546,8 @@ svc_wait(const char *svc)
} }
} }
} }
finish:
free(file);
return false; return false;
} }
@ -1105,9 +1115,10 @@ int main(int argc, char **argv)
bool runscript = false; bool runscript = false;
int retval, opt, depoptions = RC_DEP_TRACE; int retval, opt, depoptions = RC_DEP_TRACE;
RC_STRING *svc; RC_STRING *svc;
char path[PATH_MAX], lnk[PATH_MAX]; char *path = NULL;
char *lnk = NULL;
char *dir, *save = NULL, *saveLnk = NULL; char *dir, *save = NULL, *saveLnk = NULL;
char pidstr[10]; char *pidstr = NULL;
size_t l = 0, ll; size_t l = 0, ll;
const char *file; const char *file;
struct stat stbuf; struct stat stbuf;
@ -1134,10 +1145,12 @@ int main(int argc, char **argv)
* This works fine, provided that we ONLY allow multiplexed services * This works fine, provided that we ONLY allow multiplexed services
* to exist in the same directory as the master link. * to exist in the same directory as the master link.
* Also, the master link as to be a real file in the init dir. */ * Also, the master link as to be a real file in the init dir. */
if (!realpath(argv[1], path)) { path = realpath(argv[1], NULL);
if (!path) {
fprintf(stderr, "realpath: %s\n", strerror(errno)); fprintf(stderr, "realpath: %s\n", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
lnk = xmalloc(4096);
memset(lnk, 0, sizeof(lnk)); memset(lnk, 0, sizeof(lnk));
if (readlink(argv[1], lnk, sizeof(lnk)-1)) { if (readlink(argv[1], lnk, sizeof(lnk)-1)) {
dir = dirname(path); dir = dirname(path);
@ -1153,8 +1166,7 @@ int main(int argc, char **argv)
} else } else
file = basename_c(argv[1]); file = basename_c(argv[1]);
ll = strlen(dir) + strlen(file) + 2; ll = strlen(dir) + strlen(file) + 2;
service = xmalloc(ll); xasprintf(&service, "%s/%s", dir, file);
snprintf(service, ll, "%s/%s", dir, file);
if (stat(service, &stbuf) != 0) { if (stat(service, &stbuf) != 0) {
free(service); free(service);
service = xstrdup(lnk); service = xstrdup(lnk);
@ -1162,6 +1174,7 @@ int main(int argc, char **argv)
free(save); free(save);
free(saveLnk); free(saveLnk);
} }
free(lnk);
if (!service) if (!service)
service = xstrdup(path); service = xstrdup(path);
applet = basename_c(service); applet = basename_c(service);
@ -1185,7 +1198,7 @@ int main(int argc, char **argv)
/* Set an env var so that we always know our pid regardless of any /* Set an env var so that we always know our pid regardless of any
subshells the init script may create so that our mark_service_* subshells the init script may create so that our mark_service_*
functions can always instruct us of this change */ functions can always instruct us of this change */
snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid()); xasprintf(&pidstr, "%d", (int) getpid());
setenv("RC_OPENRC_PID", pidstr, 1); setenv("RC_OPENRC_PID", pidstr, 1);
/* /*
* RC_RUNSCRIPT_PID is deprecated, but we will keep it for a while * RC_RUNSCRIPT_PID is deprecated, but we will keep it for a while