18c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes/* start.c - Start/stop system services.
28c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes *
38c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes * Copyright 2016 The Android Open Source Project
48c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
58c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott HughesUSE_START(NEWTOY(start, "", TOYFLAG_USR|TOYFLAG_SBIN))
68c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott HughesUSE_STOP(NEWTOY(stop, "", TOYFLAG_USR|TOYFLAG_SBIN))
78c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
88c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughesconfig START
98c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  bool "start"
108c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  depends on TOYBOX_ON_ANDROID
118c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  default y
128c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  help
138c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes    usage: start [SERVICE...]
148c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
158c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes    Starts the given system service, or netd/surfaceflinger/zygotes.
168c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
178c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughesconfig STOP
188c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  bool "stop"
198c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  depends on TOYBOX_ON_ANDROID
208c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  default y
218c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  help
228c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes    usage: stop [SERVICE...]
238c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
248c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes    Stop the given system service, or netd/surfaceflinger/zygotes.
258c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes*/
268c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
278c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes#define FOR_start
288c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes#include "toys.h"
298c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
3045c6b12a0a0f09054ab1fee9197860aee27b2e8aElliott Hughes#include <sys/system_properties.h>
318c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
328c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughesstatic void start_stop(int start)
338c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes{
34cc1bc502de943356b360225692bb9b9818b0e63dElliott Hughes  char *property = start ? "ctl.start" : "ctl.stop";
35c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  // null terminated in both directions
36c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  char *services[] = {0,"netd","surfaceflinger","zygote","zygote_secondary",0},
37c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley       **ss = toys.optargs;
38c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  int direction = 1;
398c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
40c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  if (getuid()) error_exit("must be root");
418c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
42c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  if (!*ss) {
433325b3e9b0328ecdebe6cce913766c306721f334Elliott Hughes    // If we don't have optargs, iterate through services forward/backward.
44c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley    ss = services+1;
453325b3e9b0328ecdebe6cce913766c306721f334Elliott Hughes    if (!start) ss = services+ARRAY_LEN(services)-2, direction = -1;
468c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  }
47c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley
48c02e584c5915b8ab0793b51664d3e4a27c7bda6cRob Landley  for (; *ss; ss += direction)
4945c6b12a0a0f09054ab1fee9197860aee27b2e8aElliott Hughes    if (__system_property_set(property, *ss))
50cc1bc502de943356b360225692bb9b9818b0e63dElliott Hughes      error_exit("failed to set property '%s' to '%s'", property, *ss);
518c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes}
528c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
538c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughesvoid start_main(void)
548c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes{
558c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  start_stop(1);
568c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes}
578c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes
588c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughesvoid stop_main(void)
598c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes{
608c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes  start_stop(0);
618c5cc551ed02fbf4b67488c58e7462ad2538afb6Elliott Hughes}
62