[Toybox] new Android toys: start/stop

enh enh at google.com
Sat Jun 25 14:10:05 PDT 2016


On Sat, Jun 25, 2016 at 1:07 PM, Rob Landley <rob at landley.net> wrote:
> On 06/24/2016 05:03 PM, enh wrote:
>> * start.c - Start/stop system services.
>>  *
>>  * Copyright 2016 The Android Open Source Project
>>
>> USE_START(NEWTOY(start, "", TOYFLAG_USR|TOYFLAG_SBIN))
>> USE_STOP(NEWTOY(stop, "", TOYFLAG_USR|TOYFLAG_SBIN))
> ...
>> static void start_stop(int start)
>> {
>>   const char* property = start ? "ctl.start" : "ctl.stop";
>>   int i;
>>
>>   if (getuid() != 0) error_exit("must be root");
>
> In theory we have TOYFLAG_NEEDROOT that can check this (and error out)
> before main gets called. But that:
>
> A) depends on CFG_TOYBOX_SUID (which doesn't require the suid bit is
> set, but enables the chunk of infrastructure for dropping permissions if
> it is).
>
> B) Checks geteuid() rather than getuid(). :)
>
>>   if (*toys.optargs) {
>>     for (i = 0; toys.optargs[i]; i++) property_set(property, toys.optargs[i]);
>
> Error checking? (Looks like we always return true, even for something
> like "start rutabega"?)

like "reboot" on Android, this is all completely asynchronous so there
was never much motivation to add error-checking here. but, yeah,
changing the property_set to

  if (property_set(property, toys.optargs[i]))
    error_msg("failed to set property '%s' to '%s'", property, toys.optargs[i]);

would be more correct, even if it's not likely to ever fire. ("start
<string-longer-than-92-chars>" would be the obvious -- if unlikely --
example.)

>>   } else if (start) {
>>     for (i = 0; i < ARRAY_LEN(services); ++i) {
>>       property_set(property, services[i]);
>>     }
>>   } else {
>>     for (i = ARRAY_LEN(services) - 1; i >= 0; --i) {
>>       property_set(property, services[i]);
>>     }
>>   }
>> }
>>
>> void start_main(void)
>> {
>>   start_stop(1);
>> }
>>
>> #define CLEANUP_start
>> #define FOR_stop
>> #include "generated/flags.h"
>
> You're not using any flags or globals after this, so no need to context
> shift.
>
> I checked in a curly bracket cleanup. (Ok, and I unified the three loops
> calling "property_set()" identially 3 times. I really, REALLY need to
> install an AOSP environment on here. Lemme see if I can clear enough
> space. 9 gigs free ain't gonna cut it...)
>
> Rob



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.



More information about the Toybox mailing list