OSDN Git Service

new Android toys: start/stop
[android-x86/external-toybox.git] / toys / android / start.c
1 /* start.c - Start/stop system services.
2  *
3  * Copyright 2016 The Android Open Source Project
4
5 USE_START(NEWTOY(start, "", TOYFLAG_USR|TOYFLAG_SBIN))
6 USE_STOP(NEWTOY(stop, "", TOYFLAG_USR|TOYFLAG_SBIN))
7
8 config START
9   bool "start"
10   depends on TOYBOX_ON_ANDROID
11   default y
12   help
13     usage: start [SERVICE...]
14
15     Starts the given system service, or netd/surfaceflinger/zygotes.
16
17 config STOP
18   bool "stop"
19   depends on TOYBOX_ON_ANDROID
20   default y
21   help
22     usage: stop [SERVICE...]
23
24     Stop the given system service, or netd/surfaceflinger/zygotes.
25 */
26
27 #define FOR_start
28 #include "toys.h"
29
30 #include <cutils/properties.h>
31
32 static const char *services[] = {
33   "netd", "surfaceflinger", "zygote", "zygote_secondary", NULL,
34 };
35
36 static void start_stop(int start)
37 {
38   const char* property = start ? "ctl.start" : "ctl.stop";
39   int i;
40
41   if (getuid() != 0) error_exit("must be root");
42
43   if (*toys.optargs) {
44     for (i = 0; toys.optargs[i]; i++) property_set(property, toys.optargs[i]);
45   } else if (start) {
46     for (i = 0; i < ARRAY_LEN(services); ++i) {
47       property_set(property, services[i]);
48     }
49   } else {
50     for (i = ARRAY_LEN(services) - 1; i >= 0; --i) {
51       property_set(property, services[i]);
52     }
53   }
54 }
55
56 void start_main(void)
57 {
58   start_stop(1);
59 }
60
61 #define CLEANUP_start
62 #define FOR_stop
63 #include "generated/flags.h"
64
65 void stop_main(void)
66 {
67   start_stop(0);
68 }