Skip to content

Commit 996f4a5

Browse files
committed
add enable and disable subcommands to linitctl
1 parent 413384f commit 996f4a5

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/linitctl.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,36 @@ void cmd_stop(int argc, char **argv, FILE *stream) {
168168
free(resp);
169169
}
170170
}
171-
171+
//run a command on a service file (basically enable and disable)
172+
void svcx(char *svc, char *op, FILE *stream) {
173+
fclose(stream); //stream not necessary
174+
//get path to initd file
175+
char pre[] = "/etc/init.d/";
176+
char svcp[strlen(pre) + strlen(svc) + 1];
177+
strcpy(svcp, pre);
178+
strcpy(svcp + strlen(pre), svc);
179+
//exec
180+
char *cmd[] = {svcp, op, NULL};
181+
execv(svcp, cmd);
182+
fprintf(stderr, "[FATAL] Exec error: %s\n", strerror(errno));
183+
exit(65);
184+
}
185+
//command to enable a service
186+
void cmd_enable(int argc, char **argv, FILE *stream) {
187+
if(argc != 1) {
188+
fprintf(stderr, "[FATAL] Missing arguments\n");
189+
exit(1);
190+
}
191+
svcx(argv[0], "enable", stream);
192+
}
193+
//command to disable a service
194+
void cmd_disable(int argc, char **argv, FILE *stream) {
195+
if(argc != 1) {
196+
fprintf(stderr, "[FATAL] Missing arguments\n");
197+
exit(1);
198+
}
199+
svcx(argv[0], "disable", stream);
200+
}
172201

173202
int main(int argc, char** argv) {
174203
if(argc < 2) {
@@ -180,6 +209,10 @@ int main(int argc, char** argv) {
180209
cmd_start(argc - 2, argv + 2, stream);
181210
} else if(strcmp(argv[1], "stop") == 0) {
182211
cmd_stop(argc - 2, argv + 2, stream);
212+
} else if(strcmp(argv[1], "enable") == 0) {
213+
cmd_enable(argc - 2, argv + 2, stream);
214+
} else if(strcmp(argv[1], "disable") == 0) {
215+
cmd_disable(argc - 2, argv + 2, stream);
183216
} else if(strcmp(argv[1], "state") == 0) {
184217
cmd_state(argc - 2, argv + 2, stream);
185218
} else {

0 commit comments

Comments
 (0)