diff options
Diffstat (limited to 'linux-bin')
| -rw-r--r-- | linux-bin/.gitignore | 2 | ||||
| -rw-r--r-- | linux-bin/Makefile | 16 | ||||
| -rw-r--r-- | linux-bin/backlight.c | 176 | ||||
| -rwxr-xr-x | linux-bin/wpa | 60 | ||||
| -rw-r--r-- | linux-bin/zzz.c | 90 |
5 files changed, 344 insertions, 0 deletions
diff --git a/linux-bin/.gitignore b/linux-bin/.gitignore new file mode 100644 index 0000000..4711d71 --- /dev/null +++ b/linux-bin/.gitignore @@ -0,0 +1,2 @@ +zzz +backlight diff --git a/linux-bin/Makefile b/linux-bin/Makefile new file mode 100644 index 0000000..eb1db92 --- /dev/null +++ b/linux-bin/Makefile @@ -0,0 +1,16 @@ +WHEEL ?= wheel + +default: zzz backlight + +install-zzz: zzz + install -D -o root -g $(WHEEL) -m 755 zzz /sbin/ + chmod 6750 /sbin/zzz + +install-backlight: backlight + install -D -o root -g $(WHEEL) -m 755 backlight /sbin/ + chmod 6750 /sbin/backlight + +install: install-zzz install-backlight + +clean: + rm -f zzz backlight diff --git a/linux-bin/backlight.c b/linux-bin/backlight.c new file mode 100644 index 0000000..497fd72 --- /dev/null +++ b/linux-bin/backlight.c @@ -0,0 +1,176 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> +#include <sys/types.h> +#include <dirent.h> + +#define BACKLIGHT_DIR "/sys/class/backlight" + +void +die(const char *msg) +{ + puts(msg); + exit(1); +} + +void +err(const char *msg) +{ + perror(msg); + exit(1); +} + +void* +ecalloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + if (!ret) + err("ecalloc"); + return ret; +} + +char* +appendStr(size_t len, const char *a, const char *b) +{ + char *buf = ecalloc(len, sizeof(char)); + int ret = snprintf(buf, len, "%s%s", a, b); + if (ret == -1 || ret > len) + err("snprintf"); + return buf; +} + +/* returns errno */ +int +maxBrightness(const char *prefix, long *max) +{ + int e; + char *buf = appendStr(256, prefix, "/max_brightness"); + + int fd = open(buf, O_RDONLY); + if (fd == -1) { + e = errno; + free(buf); + return e; + } + + memset(buf, '\0', 256); + + int b = read(fd, buf, 256); + if (b==-1) { + e = errno; + free(buf); + close(fd); + return e; + } + + for (int i=0; i<256 ; i++) { + if (buf[i] == '\n') { + buf[i] = '\0'; + break; + } + } + + long n = strtol(buf, NULL, 10); + if (n == LONG_MIN || n == LONG_MAX || n == 0) + err("strtol"); + + free(buf); + buf = NULL; + close(fd); + *max = n; + + return 0; +} + +int +setBacklight(const char *prefix, int percent) +{ + char *fn = appendStr(256, prefix, "/brightness"); + + int fd = open(fn, O_WRONLY); + if (fd == -1) { + free(fn); + return -1; + } + + int n = dprintf(fd, "%d\n", percent); + if (n == -1) { + close(fd); + free(fn); + return -1; + } + + close(fd); + free(fn); + fn = NULL; + + return 0; +} + +int +main(int argc, char **argv) +{ + uid_t euid = geteuid(); + char **a = argv+1; + + if (euid != 0) + die("Program must be run as root/setuid"); + + if (!*a) + die("Brightness percentage must be supplied"); + + + long percent = strtol(*a, NULL, 10); + if (percent <= LONG_MIN || percent >= LONG_MAX) + err("invalid percent"); + if (percent < 0 || percent > 100) + die("percent must be between 0 and 100"); + + DIR *d = opendir(BACKLIGHT_DIR); + if (!d) + err("opendir"); + + errno = 0; + for (;;) { + struct dirent *dent = readdir(d); + if(!dent&&errno!=0) + err("readdir"); + + if (!dent) + break; + + if (strcmp("..", dent->d_name)==0 || strcmp(".", dent->d_name)==0) + continue; + + char *prefix = appendStr(1024, BACKLIGHT_DIR"/", dent->d_name); + + printf("found backlight: %s\n", prefix); + + int ret; + long max = 0; + ret = maxBrightness(prefix, &max); + if (ret != 0) { + const char *msg = strerror(ret); + printf("Error getting max brightness for: %s : %s\n", + prefix, msg); + free(prefix); + prefix = NULL; + continue; + } + + float p = (double)max*((double)percent/100.0); + + ret = setBacklight(prefix, (int)p); + if (ret != 0) { + perror("setBacklight"); + } + + free(prefix); + prefix = NULL; + } +} diff --git a/linux-bin/wpa b/linux-bin/wpa new file mode 100755 index 0000000..0ffbc14 --- /dev/null +++ b/linux-bin/wpa @@ -0,0 +1,60 @@ +#!/bin/sh +set -e +# manual configuration of wifi made a little bit easier with wpa_supplicant +session="wpa" +t=3 +interface= +nwid= +wpakey= + +help() { +cat <<EOF +$0 [-i <interface>] [-n <network_name>] [-p <password>] +EOF +exit 1; +} + +while [ $# -gt 0 ] ; do case $1 in + -i|if) interface="$2"; shift ; shift ;; + -n|nwid) nwid="$2"; shift ; shift ;; + -p|wpakey) wpakey="$2"; shift ; shift ;; + *) help ;; +esac ; done + +err=0 +for _v in interface nwid wpakey ; do + eval v="\$$_v"; + if [ -z "$v" ] ; then + echo "$_v cannot be empty" + err=1 + fi +done +if [ $err -eq 1 ] ; then exit 1 ; fi + + +wpakey="$(echo "$wpakey" | sed -e's/"/\\"/g')" + +conf_f="$(mktemp /dev/shm/wpa.XXXX)" +trap 'rm -f $conf_f; exit 1;' EXIT INT +cat > "$conf_f" <<EOF +network={ + ssid="$nwid" + scan_ssid=1 + key_mgmt=WPA-PSK + psk="$wpakey" +} +EOF + + +if tmux ls -F '#{session_name}' | grep -q "^$session\$" ; then + exec tmux att -t "$session" +fi + +tmux new-session -s "$session" \;\ + send-keys -t "${session}:0" "sudo wpa_supplicant -i \"$interface\" -c \"$conf_f\"" \;\ + send-keys -t "${session}:0" Enter \;\ + new-window -n "dhclient" -t 1 \;\ + send-keys -t "${session}:1" "sleep $t ; sudo dhclient -d -i \"$interface\"" \;\ + send-keys -t "${session}:1" Enter \;\ + select-window -t "${session}:0" \;\ + diff --git a/linux-bin/zzz.c b/linux-bin/zzz.c new file mode 100644 index 0000000..8bc3c22 --- /dev/null +++ b/linux-bin/zzz.c @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include <string.h> +#include <sys/types.h> + +#define POWER_STATE_F "/sys/power/state" + +void +die(const char *msg) +{ + puts(msg); + exit(1); +} + +void +err(const char *msg) +{ + perror(msg); + exit(1); +} + +void +suspend() +{ + int fh = open(POWER_STATE_F, O_WRONLY); + int n; + const char *cmd = "mem"; + + if (fh == -1) + err("Opening "POWER_STATE_F); + + n = write(fh, cmd, 3); + if (n == -1) + err("Writing to "POWER_STATE_F); + + exit(0); +} + +int +main(int argc, char **argv) +{ + uid_t uid = getuid(); + gid_t gid = getgid(); + uid_t euid = geteuid(); + char **a = argv+1; + char *lockProg = "slock"; + char *lockProgWl= "swaylock"; + pid_t pid; + int ret; + + if (euid != 0) + die("Program must be run as root/setuid"); + + if (strcmp("wayland", getenv("XDG_SESSION_TYPE")) == 0) + lockProg = lockProgWl; + + for (;*a;a++) { + if (strcmp("-l", *a) == 0) { + a++; + if (*a) + lockProg = *a; + else + die("-l must have an argument"); + } else { + printf("Unknown argument: '%s'\n", *a); + exit(1); + } + } + + pid = fork(); + switch (pid) { + case -1: + err("fork"); + case 0: + ret = setuid(uid); + if (ret == -1) + err("setuid"); + ret = setgid(gid); + if (ret == -1) + err("setgid"); + + execlp(lockProg, NULL); + err("execlp"); + default: + suspend(); + } + +} |
