123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #define _GNU_SOURCE
- #include "governors-query.h"
- #include "logging.h"
- #include <ctype.h>
- #include <errno.h>
- #include <sys/types.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;
- }
|