toybox/toys/android/setprop.c
<<
>>
Prefs
   1/* setprop.c - Set an Android system property
   2 *
   3 * Copyright 2015 The Android Open Source Project
   4
   5USE_SETPROP(NEWTOY(setprop, "<2>2", TOYFLAG_USR|TOYFLAG_SBIN))
   6
   7config SETPROP
   8  bool "setprop"
   9  default y
  10  depends on TOYBOX_ON_ANDROID
  11  help
  12    usage: setprop NAME VALUE
  13
  14    Sets an Android system property.
  15*/
  16
  17#define FOR_setprop
  18#include "toys.h"
  19
  20#include <sys/system_properties.h>
  21
  22void setprop_main(void)
  23{
  24  char *name = toys.optargs[0], *value = toys.optargs[1];
  25  char *p;
  26  size_t name_len = strlen(name), value_len = strlen(value);
  27
  28  // property_set doesn't tell us why it failed, and actually can't
  29  // recognize most failures (because it doesn't wait for init), so
  30  // we duplicate all of init's checks here to help the user.
  31
  32  if (value_len >= PROP_VALUE_MAX && !strncmp(value, "ro.", 3))
  33    error_exit("value '%s' too long; try '%.*s'",
  34               value, PROP_VALUE_MAX - 1, value);
  35
  36  if (*name == '.' || name[name_len - 1] == '.')
  37    error_exit("property names must not start or end with '.'");
  38  if (strstr(name, ".."))
  39    error_exit("'..' is not allowed in a property name");
  40  for (p = name; *p; ++p)
  41    if (!isalnum(*p) && !strchr(":@_.-", *p))
  42      error_exit("invalid character '%c' in name '%s'", *p, name);
  43
  44  if (__system_property_set(name, value))
  45    error_msg("failed to set property '%s' to '%s'", name, value);
  46}
  47