Skip to content

Commit

Permalink
first step
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGantner committed Dec 2, 2024
1 parent 0dcdb51 commit 0971205
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
17 changes: 9 additions & 8 deletions devmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void devmgr_run(int sockfd, const char *devpath) {
}
int fd = open(msg.path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
int ret = errno;
send_msg(sockfd, ret ? -1 : fd, &ret, sizeof(ret));
send_msg(sockfd, ret ? -1 : fd, &ret, sizeof(int));
if (fd >= 0) {
close(fd);
}
Expand All @@ -123,12 +123,12 @@ static void devmgr_run(int sockfd, const char *devpath) {
exit(0);
}

int devmgr_start(int *fd, pid_t *pid, const char *devpath) {
int devmgr_start(bool allow_root, int *fd, pid_t *pid, const char *devpath) {
// TODO check for the need of setuid
if (geteuid() != 0) {
fprintf(stderr, "wshowkeys needs to be setuid to read input events\n");
return 1;
}
// if (geteuid() != 0) {
// fprintf(stderr, "wshowkeys needs to be setuid to read input events\n");
// return 1;
// }

int sock[2];
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) {
Expand Down Expand Up @@ -158,8 +158,9 @@ int devmgr_start(int *fd, pid_t *pid, const char *devpath) {
fprintf(stderr, "devmgr: setuid: %s\n", strerror(errno));
return 1;
}
if (setuid(0) != -1) {
fprintf(stderr, "devmgr: failed to drop root\n");

if (allow_root == false && setuid(0) != -1) {
fprintf(stderr, "devmgr: failed to drop root. Use --allow-root if this is expected\n");
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion devmgr.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef _DEVMGR_H
#define _DEVMGR_H
#include <fcntl.h>
#include <stdbool.h>

int devmgr_start(int *fd, pid_t *pid, const char *devpath);
int devmgr_start(bool allow_root, int *fd, pid_t *pid, const char *devpath);
int devmgr_open(int sockfd, const char *path);
void devmgr_finish(int sock, pid_t pid);

Expand Down
48 changes: 41 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ static uint32_t parse_color(const char *color) {
return res;
}


const char* ALLOW_ROOT = "--allow-root";

void usage(void){
fprintf(stderr, "usage: wshowkeys [-b|-f|-s #RRGGBB[AA]] [-F font] "
"[-t timeout]\n\t[-a top|left|right|bottom] [-m margin] "
"[-o output]\n\t%s\n", ALLOW_ROOT);
exit(2);
}

struct wsk_state state = { 0 };

void exit_devmgr(void){
Expand All @@ -514,8 +524,18 @@ void exit_wayland(void){
}

int main(int argc, char *argv[]) {
/* NOTICE: This code runs as root */
if (devmgr_start(&state.devmgr, &state.devmgr_pid, INPUTDEVPATH) > 0) {
/* NOTICE: This code may run as root */

int ret=1;
bool allow_root = false;
// blindly lookk for --allow-root
while( ret < argc && allow_root == false){
if(strncmp(argv[ret], ALLOW_ROOT, 14)==0)
allow_root = true;
ret++;
}

if (devmgr_start(allow_root,&state.devmgr, &state.devmgr_pid, INPUTDEVPATH) > 0) {
return 1;
}
atexit(exit_devmgr);
Expand All @@ -530,8 +550,9 @@ int main(int argc, char *argv[]) {
state.font = "monospace 24";
state.timeout = 1;

// Parse args
int c;
while ((c = getopt(argc, argv, "hb:f:s:F:t:a:m:o:")) != -1) {
while ((c = getopt(argc, argv, "hb:f:s:F:t:a:m:o:-:")) != -1) {
switch (c) {
case 'b':
state.background = parse_color(optarg);
Expand Down Expand Up @@ -565,12 +586,25 @@ int main(int argc, char *argv[]) {
case 'o':
fprintf(stderr, "-o is unimplemented\n");
return 0;
case '-':
if( strncmp(optarg, (ALLOW_ROOT+2), 13) != 0){
fprintf(stderr,"unsupported option: --%s\n", optarg);
usage();
}
break;

default:
fprintf(stderr, "usage: wshowkeys [-b|-f|-s #RRGGBB[AA]] [-F font] "
"[-t timeout]\n\t[-a top|left|right|bottom] [-m margin] "
"[-o output]\n");
return 1;
fprintf(stderr, "unsupported option: %c\n", c);
usage();
}
}

if(optind < argc){
while(optind<argc){
fprintf(stderr, "extraneous argument on CLI: %s\n", argv[optind]);
optind++;
}
usage();
}

state.udev = udev_new();
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'wshowkeys',
'c',
version: '0.1.1',
version: '0.2.0',
license: 'GPL',
meson_version: '>=0.58.0',
default_options: [
Expand Down

0 comments on commit 0971205

Please sign in to comment.