1629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes/* setprop.c - Set an Android system property
2629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes *
3629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes * Copyright 2015 The Android Open Source Project
4629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
5629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott HughesUSE_SETPROP(NEWTOY(setprop, "<2>2", TOYFLAG_USR|TOYFLAG_SBIN))
6629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
7629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughesconfig SETPROP
8629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  bool "setprop"
9629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  default y
10e5fb6a28ffb09782e4df08251956f42c48445147Rob Landley  depends on TOYBOX_ON_ANDROID
11629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  help
12629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    usage: setprop NAME VALUE
13629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
14629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    Sets an Android system property.
15629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes*/
16629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
17629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#define FOR_setprop
18629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#include "toys.h"
19629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
20629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#if defined(__ANDROID__)
21c47e72553eea3038188702c024adaf3148546e1cElliott Hughes
22629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#include <cutils/properties.h>
23629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
24629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughesvoid setprop_main(void)
25629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes{
26629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  char *name = toys.optargs[0], *value = toys.optargs[1];
27629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  char *p;
28629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  size_t name_len = strlen(name), value_len = strlen(value);
29629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
30629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  // property_set doesn't tell us why it failed, and actually can't
31629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  // recognize most failures (because it doesn't wait for init), so
32629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  // we duplicate all of init's checks here to help the user.
33629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
34629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  if (name_len >= PROP_NAME_MAX)
35629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    error_exit("name '%s' too long; try '%.*s'",
36629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes               name, PROP_NAME_MAX - 1, name);
37629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  if (value_len >= PROP_VALUE_MAX)
38629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    error_exit("value '%s' too long; try '%.*s'",
39629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes               value, PROP_VALUE_MAX - 1, value);
40629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
41629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  if (*name == '.' || name[name_len - 1] == '.')
42629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    error_exit("property names must not start or end with '.'");
43629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  if (strstr(name, ".."))
44629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    error_exit("'..' is not allowed in a property name");
45629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  for (p = name; *p; ++p)
46629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    if (!isalnum(*p) && !strchr("_.-", *p))
47629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes      error_exit("invalid character '%c' in name '%s'", *p, name);
48629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes
49629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes  if (property_set(name, value))
50629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes    error_msg("failed to set property '%s' to '%s'", name, value);
51629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes}
52c47e72553eea3038188702c024adaf3148546e1cElliott Hughes
53c47e72553eea3038188702c024adaf3148546e1cElliott Hughes#else
54c47e72553eea3038188702c024adaf3148546e1cElliott Hughes
55c47e72553eea3038188702c024adaf3148546e1cElliott Hughesvoid setprop_main(void)
56c47e72553eea3038188702c024adaf3148546e1cElliott Hughes{
57c47e72553eea3038188702c024adaf3148546e1cElliott Hughes}
58c47e72553eea3038188702c024adaf3148546e1cElliott Hughes
59c47e72553eea3038188702c024adaf3148546e1cElliott Hughes#endif
60