android_reboot.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2011, 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. #include <cutils/android_reboot.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <cutils/properties.h>
  20. #define TAG "android_reboot"
  21. int android_reboot(unsigned cmd, int /*flags*/, const char* arg) {
  22. int ret;
  23. const char* restart_cmd = NULL;
  24. char* prop_value;
  25. switch (cmd) {
  26. case ANDROID_RB_RESTART: // deprecated
  27. case ANDROID_RB_RESTART2:
  28. restart_cmd = "reboot";
  29. break;
  30. case ANDROID_RB_POWEROFF:
  31. restart_cmd = "shutdown";
  32. break;
  33. case ANDROID_RB_THERMOFF:
  34. restart_cmd = "shutdown,thermal";
  35. break;
  36. }
  37. if (!restart_cmd) return -1;
  38. if (arg && arg[0]) {
  39. ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
  40. } else {
  41. ret = asprintf(&prop_value, "%s", restart_cmd);
  42. }
  43. if (ret < 0) return -1;
  44. ret = property_set(ANDROID_RB_PROPERTY, prop_value);
  45. free(prop_value);
  46. return ret;
  47. }