Skip to content

Commit

Permalink
linux: Add options for setting group and mode
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron B. Haun <[email protected]>
  • Loading branch information
Laikulo committed Apr 28, 2023
1 parent bec1d95 commit d2e3a38
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/linux/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdio.h> // fprintf
#include <string.h> // memmove
#include <sys/stat.h> // chmod
#include <sys/types.h> // mode_t gid_t
#include <time.h> // struct timespec
#include <unistd.h> // ttyname
#include "board/irq.h" // irq_wait
Expand Down Expand Up @@ -64,7 +65,7 @@ set_close_on_exec(int fd)
}

int
console_setup(char *name)
console_setup(char *name, mode_t mode, gid_t group)
{
// Open pseudo-tty
struct termios ti;
Expand Down Expand Up @@ -98,12 +99,20 @@ console_setup(char *name)
report_errno("symlink", ret);
return -1;
}
ret = chmod(tname, 0660);
ret = chmod(tname, mode);
if (ret) {
report_errno("chmod", ret);
return -1;
}

if (group != (gid_t) -1) {
ret = chown(tname, (uid_t) -1 , group);
if (ret) {
report_errno("chgrp", ret);
return -1;
}
}

// Make sure stderr is non-blocking
ret = set_non_blocking(STDERR_FILENO);
if (ret)
Expand Down
3 changes: 2 additions & 1 deletion src/linux/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <signal.h> // sigset_t
#include <stdint.h> // uint32_t
#include <sys/types.h> // mode_t gid_t
#include "autoconf.h" // CONFIG_CLOCK_FREQ

#define MAX_GPIO_LINES 288
Expand All @@ -18,7 +19,7 @@
void report_errno(char *where, int rc);
int set_non_blocking(int fd);
int set_close_on_exec(int fd);
int console_setup(char *name);
int console_setup(char *name, mode_t mode, gid_t group);
void console_sleep(sigset_t *sigset);

// timer.c
Expand Down
42 changes: 39 additions & 3 deletions src/linux/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include <errno.h> // errno
#include </usr/include/sched.h> // sched_setscheduler sched_get_priority_max
#include <stdio.h> // fprintf
#include <string.h> // memset
#include <stdlib.h> // strtoul
#include <sys/types.h> // getgrent struct
#include <grp.h> // getgrnam
#include <unistd.h> // getopt
#include <sys/mman.h> // mlockall MCL_CURRENT MCL_FUTURE
#include "board/misc.h" // console_sendf
Expand Down Expand Up @@ -70,7 +74,9 @@ main(int argc, char **argv)
orig_argv = argv;
int opt, watchdog = 0, realtime = 0;
char *serial = "/tmp/klipper_host_mcu";
while ((opt = getopt(argc, argv, "wrI:")) != -1) {
char *pty_group_name = NULL;
mode_t pty_mode = 0660;
while ((opt = getopt(argc, argv, "wrI:g:m:")) != -1) {
switch (opt) {
case 'w':
watchdog = 1;
Expand All @@ -81,8 +87,28 @@ main(int argc, char **argv)
case 'I':
serial = optarg;
break;
case 'g':
pty_group_name = optarg;
break;
case 'm':
errno = 0; // see man 2 strtoul
unsigned long int input_pty_mode = strtoul(optarg, NULL, 8);
if (errno) {
fprintf(stderr, "unable to parse pty mode: %s", optarg);
return -1;
}
if (input_pty_mode > 0777ul) {
fprintf(stderr,
"mode %s invalid (setuid/gid/sticky not supported)",
optarg);
return -1;
}
pty_mode = input_pty_mode;
break;
default:
fprintf(stderr, "Usage: %s [-w] [-r] [-I path]\n", argv[0]);
fprintf(stderr,
"Usage: %s [-w] [-r] [-I path] [-g group] [-m filemode]\n",
argv[0]);
return -1;
}
}
Expand All @@ -93,7 +119,17 @@ main(int argc, char **argv)
if (ret)
return ret;
}
int ret = console_setup(serial);
gid_t pty_group_id = -1;
if (pty_group_name) {
struct group * pty_group = getgrnam(pty_group_name);
if (!pty_group) {
fprintf(stderr, "Unable to find group \"%s\"\n", pty_group_name);
return -1;
}
pty_group_id = pty_group->gr_gid;
}

int ret = console_setup(serial, pty_mode, pty_group_id);
if (ret)
return -1;
if (watchdog) {
Expand Down

0 comments on commit d2e3a38

Please sign in to comment.