mirror of
https://github.com/troglobit/finit.git
synced 2026-11-06 15:51:03 +00:00
initctl: refactor serv_enable() for readability and coding style
Also apply the same logic to serv_disable() to drop 40 char limit on service names. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
9a6c68c952
commit
b6c5498f43
1 file changed
+53
-34
+53
-34
@@ -253,18 +253,23 @@ static char *conf(char *path, size_t len, char *name, int creat)
|
|||||||
|
|
||||||
int serv_enable(char *arg)
|
int serv_enable(char *arg)
|
||||||
{
|
{
|
||||||
char src[strlen(arg) + 6];
|
char *serv, *ptr, *src, *dst;
|
||||||
size_t arglen, len;
|
size_t len;
|
||||||
char *ptr;
|
int rc;
|
||||||
|
|
||||||
if (!arg || !arg[0]) {
|
if (!arg || !arg[0]) {
|
||||||
WARNX("missing argument to enable, may be one of:");
|
WARNX("missing argument to enable, may be one of:");
|
||||||
return serv_list("available");
|
return serv_list("available");
|
||||||
}
|
}
|
||||||
|
|
||||||
strlcpy(src, arg, sizeof(src));
|
len = strlen(arg);
|
||||||
if (!strstr(src, ".conf"))
|
serv = alloca(len + 6);
|
||||||
strlcat(src, ".conf", sizeof(src));
|
if (!serv)
|
||||||
|
ERR(71, "failed allocating stack");
|
||||||
|
|
||||||
|
strlcpy(serv, arg, len + 6);
|
||||||
|
if (len < 6 || strcmp(&serv[len - 5], ".conf"))
|
||||||
|
strlcat(serv, ".conf", len + 6);
|
||||||
|
|
||||||
if (chdir(finit_rcsd))
|
if (chdir(finit_rcsd))
|
||||||
ERR(72, "failed cd %s", finit_rcsd);
|
ERR(72, "failed cd %s", finit_rcsd);
|
||||||
@@ -272,58 +277,72 @@ int serv_enable(char *arg)
|
|||||||
if (icreate && mkdir("enabled", 0755) && EEXIST != errno)
|
if (icreate && mkdir("enabled", 0755) && EEXIST != errno)
|
||||||
ERR(73, "failed creating %s/enabled directory", finit_rcsd);
|
ERR(73, "failed creating %s/enabled directory", finit_rcsd);
|
||||||
|
|
||||||
len = snprintf(NULL, 0, "%s/available/%s", finit_rcsd, src);
|
if (asprintf(&src, "%s/available/%s", finit_rcsd, serv) == -1)
|
||||||
arglen = snprintf(NULL, 0, "%s/enabled/%s.conf", finit_rcsd, arg);
|
goto fail;
|
||||||
char argpath[arglen + 1], path[len + 1];
|
if (asprintf(&dst, "%s/enabled/%s", finit_rcsd, serv) == -1) {
|
||||||
|
free(src);
|
||||||
snprintf(path, sizeof(path), "%s/available/%s", finit_rcsd, src);
|
fail:
|
||||||
|
ERR(71, "failed allocating memory");
|
||||||
/* check for template instatiation */
|
|
||||||
ptr = strchr(path, '@');
|
|
||||||
if (ptr++) {
|
|
||||||
*ptr = 0;
|
|
||||||
strlcat(path, ".conf", sizeof(path));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fexist(path))
|
if (fexist(dst)) {
|
||||||
ERRX(72, "cannot find %s%s", ptr ? "template ": "", path);
|
WARNX("%s already enabled", arg);
|
||||||
|
rc = noerr ? 0 : 1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(argpath, sizeof(argpath), "%s/enabled/%s", finit_rcsd, arg);
|
/* check for template instantiation */
|
||||||
if (!strstr(argpath, ".conf"))
|
ptr = strchr(src, '@');
|
||||||
strlcat(argpath, ".conf", sizeof(argpath));
|
if (ptr)
|
||||||
if (fexist(argpath))
|
strcpy(++ptr, ".conf");
|
||||||
ERRX(noerr ? 0 : 1, "%s already enabled", arg);
|
|
||||||
|
|
||||||
return symlink(path, argpath) != 0;
|
if (!fexist(src)) {
|
||||||
|
WARNX("cannot find %s%s", ptr ? "template ": "", src);
|
||||||
|
rc = 72;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = symlink(src, dst);
|
||||||
|
if (rc)
|
||||||
|
WARN("failed enabling %s", arg);
|
||||||
|
done:
|
||||||
|
free(src);
|
||||||
|
free(dst);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_disable(char *arg, int check)
|
int do_disable(char *arg, int check)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char corr[40];
|
size_t len;
|
||||||
|
char *serv;
|
||||||
|
|
||||||
if (!arg || !arg[0]) {
|
if (!arg || !arg[0]) {
|
||||||
WARNX("missing argument to disable, may be one of:");
|
WARNX("missing argument to disable, may be one of:");
|
||||||
return serv_list("enabled");
|
return serv_list("enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strstr(arg, ".conf")) {
|
len = strlen(arg);
|
||||||
snprintf(corr, sizeof(corr), "%s.conf", arg);
|
serv = alloca(len + 6);
|
||||||
arg = corr;
|
if (!serv)
|
||||||
}
|
ERR(71, "failed allocating stack");
|
||||||
|
|
||||||
|
strlcpy(serv, arg, len + 6);
|
||||||
|
if (len < 6 || strcmp(&serv[len - 5], ".conf"))
|
||||||
|
strlcat(serv, ".conf", len + 6);
|
||||||
|
|
||||||
if (chdir(finit_rcsd))
|
if (chdir(finit_rcsd))
|
||||||
ERR(72, "failed cd %s", finit_rcsd);
|
ERR(72, "failed cd %s", finit_rcsd);
|
||||||
if (chdir("enabled")) /* System *may* have enabled/ dir. */
|
if (chdir("enabled")) /* System *may* have enabled/ dir. */
|
||||||
dbg("Failed changing to %s/enabled/: %s", finit_rcsd, strerror(errno));
|
dbg("Failed changing to %s/enabled/: %s", finit_rcsd, strerror(errno));
|
||||||
|
|
||||||
if (check && stat(arg, &st))
|
if (check && stat(serv, &st))
|
||||||
ERRX(noerr ? 0 : 6, "%s not (an) enabled (service).", arg);
|
ERRX(noerr ? 0 : 6, "%s not (an) enabled (service).", serv);
|
||||||
|
|
||||||
if (check && (st.st_mode & S_IFMT) == S_IFLNK)
|
if (check && (st.st_mode & S_IFMT) == S_IFLNK)
|
||||||
ERRX(noerr ? 0 : 1, "cannot disable %s, not a symlink.", arg);
|
ERRX(noerr ? 0 : 1, "cannot disable %s, not a symlink.", serv);
|
||||||
|
|
||||||
return remove(arg) != 0;
|
return remove(serv) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int serv_disable(char *arg)
|
int serv_disable(char *arg)
|
||||||
|
|||||||
Reference in new issue
Block a user