| Tom Cherry | 65a1ee8 | 2019-06-05 10:26:54 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <ctype.h> | 
|  | 18 | #include <stdlib.h> | 
|  | 19 | #include <sys/system_properties.h> | 
|  | 20 |  | 
|  | 21 | #include <iostream> | 
|  | 22 |  | 
|  | 23 | #include <android-base/properties.h> | 
|  | 24 | #include <android-base/strings.h> | 
|  | 25 |  | 
|  | 26 | using android::base::SetProperty; | 
|  | 27 | using android::base::StartsWith; | 
|  | 28 |  | 
|  | 29 | extern "C" int setprop_main(int argc, char** argv) { | 
|  | 30 | if (argc != 3) { | 
|  | 31 | std::cout << "usage: setprop NAME VALUE\n" | 
|  | 32 | "\n" | 
|  | 33 | "Sets an Android system property." | 
|  | 34 | << std::endl; | 
|  | 35 | return EXIT_FAILURE; | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | auto name = std::string{argv[1]}; | 
|  | 39 | auto value = std::string{argv[2]}; | 
|  | 40 |  | 
|  | 41 | // SetProperty() doesn't tell us why it failed, and actually can't recognize most failures, so | 
|  | 42 | // we duplicate some of init's checks here to help the user. | 
|  | 43 |  | 
|  | 44 | if (name.front() == '.' || name.back() == '.') { | 
|  | 45 | std::cerr << "Property names must not start or end with a '.'" << std::endl; | 
|  | 46 | return EXIT_FAILURE; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | if (name.find("..") != std::string::npos) { | 
|  | 50 | std::cerr << "'..' is not allowed in a property name" << std::endl; | 
|  | 51 | return EXIT_FAILURE; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | for (const auto& c : name) { | 
|  | 55 | if (!isalnum(c) && !strchr(":@_.-", c)) { | 
|  | 56 | std::cerr << "Invalid character '" << c << "' in name '" << name << "'" << std::endl; | 
|  | 57 | return EXIT_FAILURE; | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | if (value.size() >= PROP_VALUE_MAX && !StartsWith(value, "ro.")) { | 
|  | 62 | std::cerr << "Value '" << value << "' is too long, " << value.size() | 
|  | 63 | << " bytes vs a max of " << PROP_VALUE_MAX << std::endl; | 
|  | 64 | return EXIT_FAILURE; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | if (mbstowcs(nullptr, value.data(), 0) == static_cast<std::size_t>(-1)) { | 
|  | 68 | std::cerr << "Value '" << value << "' is not a UTF8 encoded string" << std::endl; | 
|  | 69 | return EXIT_FAILURE; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | if (!SetProperty(name, value)) { | 
|  | 73 | std::cerr << "Failed to set property '" << name << "' to '" << value | 
|  | 74 | << "'.\nSee dmesg for error reason." << std::endl; | 
|  | 75 | return EXIT_FAILURE; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | return EXIT_SUCCESS; | 
|  | 79 | } |