Skip to content

Instantly share code, notes, and snippets.

@Geofferey
Created June 15, 2020 02:29
Show Gist options
  • Save Geofferey/e2a56d32b3d8347959b03814e002b76d to your computer and use it in GitHub Desktop.
Save Geofferey/e2a56d32b3d8347959b03814e002b76d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* This extremely simple program allows users/accounts to enable and disable wakelocks on Android based devices by to writing arbitray strings to /sys/power/wake_lock & /sys/power/wake_unlock.
Use case:
On my device I am running a service inside of a chroot as a system account. Because said service runs under a restrictive account and not root it can not write to /sys/power/wake_* even when adding the sys account to the aid_wakelock Adroid group. Instead of stupidly allowing a system account to run commands as root using sudo or some other means that would almost certainly grant more capabilities than required I wrote this program.
*/
void help(void) {
// printf("Wakelocker v1 by:\nGeofferey@github\n\n");
printf("Useage ./wakelock [option] [string]:\n\n");
printf("-l --lock enable a wakelock\n");
printf("-u --unlock disable a wakelock\n\n");
printf("e.g. # ./wakelock -u Android_wakelock\n");
}
int main(int argc, char **argv) {
void lock(void) {
FILE *lock_file = fopen("/sys/power/wake_lock", "a");
if (lock_file == NULL) {
printf("Could not open /sys/power/wake_lock\n");
exit(0);
}
if (!(lock_file == NULL)) {
fprintf(lock_file, "%s", argv[2]);
fclose(lock_file);
printf (argv[2]);
printf (" wakelock enabled...\n");
}
}
void unlock(void) {
FILE *unlock_file = fopen("/sys/power/wake_unlock", "a");
if (unlock_file == NULL) {
printf("Could not open /sys/power/wake_unlock\n");
exit(0);
}
if (!(unlock_file == NULL)) {
fprintf(unlock_file, "%s", argv[2]);
fclose(unlock_file);
printf (argv[2]);
printf(" wakelock disabled...\n");
}
}
if (argc > 2) {
if ((strcmp(argv[1], "--lock") == 0) || (strcmp(argv[1], "-l") == 0)) {
lock();
}
else if ((strcmp(argv[1], "--unlock") == 0) || (strcmp(argv[1], "-u") == 0)) {
unlock();
}
/*else if ((strcmp(argv[1], "--list") == 0) && (strcmp(argv[2], "--locks") == 0)) {
list_locks();
}*/
else
help();
}
else
help();
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment