12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #define _GNU_SOURCE
- #include "common-governors.h"
- #include "common-logging.h"
- #include <unistd.h>
- static int set_gov_state(const char *value)
- {
- char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
- int num = fetch_governors(governors);
- int retval = EXIT_SUCCESS;
- int res = 0;
- for (int i = 0; i < num; i++) {
- const char *gov = governors[i];
- FILE *f = fopen(gov, "w");
- if (!f) {
- LOG_ERROR("Failed to open file for write %s\n", gov);
- continue;
- }
- res = fprintf(f, "%s\n", value);
- if (res < 0) {
- LOG_ERROR("Failed to set governor %s to %s: %s", gov, value, strerror(errno));
- retval = EXIT_FAILURE;
- }
- fclose(f);
- }
- return retval;
- }
- int main(int argc, char *argv[])
- {
- if (argc == 2 && strncmp(argv[1], "get", 3) == 0) {
- printf("%s", get_gov_state());
- } else if (argc == 3 && strncmp(argv[1], "set", 3) == 0) {
- const char *value = argv[2];
-
- if (geteuid() != 0) {
- LOG_ERROR("This program must be run as root\n");
- return EXIT_FAILURE;
- }
- return set_gov_state(value);
- } else {
- fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
|