setprop.c revision 629526f72ccd2853eadfc1905b6f9f45bc2f6300
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 10629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes help 11629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes usage: setprop NAME VALUE 12629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes 13629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes Sets an Android system property. 14629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes*/ 15629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes 16629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#define FOR_setprop 17629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#include "toys.h" 18629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes 19629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#if defined(__ANDROID__) 20629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#include <cutils/properties.h> 21629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#endif 22629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes 23629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughesvoid setprop_main(void) 24629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes{ 25629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes#if defined(__ANDROID__) 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#endif 52629526f72ccd2853eadfc1905b6f9f45bc2f6300Elliott Hughes} 53