seatd: Command-line argument for loglevel

SEATD_LOGLEVEL was used to set the loglevel despite already having
getopt in place. Remove the environment variable and make a command-line
argument for it instead.
This commit is contained in:
Kenny Levinsen 2022-02-26 19:05:49 +01:00
parent 3eb0db57bb
commit 9bbdf0f0b8
4 changed files with 39 additions and 39 deletions

View file

@ -10,6 +10,10 @@ seatd-launch - Start a process with its own seatd instance
# OPTIONS # OPTIONS
*-l <loglevel>*
Log-level to pass to seatd. See *seatd*(1) for information about
available log-levels.
*-h* *-h*
Show help message and quit. Show help message and quit.

View file

@ -27,6 +27,10 @@ seatd - A seat management daemon
*-s <path>* *-s <path>*
Where to create the seatd socket. Defaults to `/run/seatd.sock`. Where to create the seatd socket. Defaults to `/run/seatd.sock`.
*-l <loglevel>*
Log-level to use. Must be one of debug, info, error or silent. Defaults
to error.
*-v* *-v*
Show the version number and quit. Show the version number and quit.
@ -38,18 +42,6 @@ such as displays and input devices in a multi-session, multi-seat environment.
seatd operates over a UNIX domain socket, with *libseat* providing the seatd operates over a UNIX domain socket, with *libseat* providing the
client-side of the protocol. client-side of the protocol.
# ENVIRONMENT
[[ *VARIABLE*
:[ *VALUES*
:< *DESCRIPTION*
| SEATD_SOCK
: File path
: Informs libseat of the socket location, needed if it differs from `/run/seatd.sock`
| SEATD_LOGLEVEL
: silent, error, info, debug
: Sets the seatd log level. Defaults to "error"
# SEE ALSO # SEE ALSO
The libseat library, *<libseat.h>*, *seatd-launch*(1) The libseat library, *<libseat.h>*, *seatd-launch*(1)

View file

@ -13,13 +13,19 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
const char *usage = "Usage: seatd-launch [options] [--] command\n" const char *usage = "Usage: seatd-launch [options] [--] command\n"
"\n" "\n"
" -l <loglevel> Log-level to pass to seatd\n"
" -h Show this help message\n" " -h Show this help message\n"
" -v Show the version number\n" " -v Show the version number\n"
"\n"; "\n";
int c; int c;
while ((c = getopt(argc, argv, "vh")) != -1) { char loglevel[16] = "info";
while ((c = getopt(argc, argv, "vhl:")) != -1) {
switch (c) { switch (c) {
case 'l':
strncpy(loglevel, optarg, sizeof loglevel);
loglevel[sizeof loglevel - 1] = '\0';
break;
case 'v': case 'v':
printf("seatd-launch version %s\n", SEATD_VERSION); printf("seatd-launch version %s\n", SEATD_VERSION);
return 0; return 0;
@ -60,15 +66,8 @@ int main(int argc, char *argv[]) {
char pipebuf[16] = {0}; char pipebuf[16] = {0};
snprintf(pipebuf, sizeof pipebuf, "%d", readiness_pipe[1]); snprintf(pipebuf, sizeof pipebuf, "%d", readiness_pipe[1]);
char *env[2] = {NULL, NULL}; char *env[1] = {NULL};
char loglevelbuf[32] = {0}; char *command[] = {"seatd", "-n", pipebuf, "-s", sockpath, "-l", loglevel, NULL};
char *cur_loglevel = getenv("SEATD_LOGLEVEL");
if (cur_loglevel != NULL) {
snprintf(loglevelbuf, sizeof loglevelbuf, "SEATD_LOGLEVEL=%s", cur_loglevel);
env[0] = loglevelbuf;
}
char *command[] = {"seatd", "-n", pipebuf, "-s", sockpath, NULL};
execve(SEATD_INSTALLPATH, command, env); execve(SEATD_INSTALLPATH, command, env);
perror("Could not start seatd"); perror("Could not start seatd");
_exit(1); _exit(1);

View file

@ -57,20 +57,6 @@ error:
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char *loglevel = getenv("SEATD_LOGLEVEL");
enum libseat_log_level level = LIBSEAT_LOG_LEVEL_ERROR;
if (loglevel != NULL) {
if (strcmp(loglevel, "silent") == 0) {
level = LIBSEAT_LOG_LEVEL_SILENT;
} else if (strcmp(loglevel, "info") == 0) {
level = LIBSEAT_LOG_LEVEL_INFO;
} else if (strcmp(loglevel, "debug") == 0) {
level = LIBSEAT_LOG_LEVEL_DEBUG;
}
}
log_init();
libseat_set_log_level(level);
const char *usage = "Usage: seatd [options]\n" const char *usage = "Usage: seatd [options]\n"
"\n" "\n"
" -h Show this help message\n" " -h Show this help message\n"
@ -78,14 +64,16 @@ int main(int argc, char *argv[]) {
" -u <user> User to own the seatd socket\n" " -u <user> User to own the seatd socket\n"
" -g <group> Group to own the seatd socket\n" " -g <group> Group to own the seatd socket\n"
" -s <path> Where to create the seatd socket\n" " -s <path> Where to create the seatd socket\n"
" -l <loglevel> Log-level, one of debug, info, error or silent\n"
" -v Show the version number\n" " -v Show the version number\n"
"\n"; "\n";
int c; int c;
int uid = -1, gid = -1; int uid = -1, gid = -1;
int readiness = -1; int readiness = -1;
enum libseat_log_level level = LIBSEAT_LOG_LEVEL_ERROR;
const char *socket_path = SEATD_DEFAULTPATH; const char *socket_path = SEATD_DEFAULTPATH;
while ((c = getopt(argc, argv, "vhn:s:g:u:")) != -1) { while ((c = getopt(argc, argv, "vhn:s:g:u:l:")) != -1) {
switch (c) { switch (c) {
case 'n': case 'n':
readiness = atoi(optarg); readiness = atoi(optarg);
@ -117,6 +105,20 @@ int main(int argc, char *argv[]) {
} }
break; break;
} }
case 'l':
if (strcmp(optarg, "debug") == 0) {
level = LIBSEAT_LOG_LEVEL_DEBUG;
} else if (strcmp(optarg, "info") == 0) {
level = LIBSEAT_LOG_LEVEL_INFO;
} else if (strcmp(optarg, "error") == 0) {
level = LIBSEAT_LOG_LEVEL_ERROR;
} else if (strcmp(optarg, "silent") == 0) {
level = LIBSEAT_LOG_LEVEL_SILENT;
} else {
fprintf(stderr, "Invalid loglevel: %s\n", optarg);
return 1;
}
break;
case 'v': case 'v':
printf("seatd version %s\n", SEATD_VERSION); printf("seatd version %s\n", SEATD_VERSION);
return 0; return 0;
@ -131,6 +133,9 @@ int main(int argc, char *argv[]) {
} }
} }
log_init();
libseat_set_log_level(level);
struct stat st; struct stat st;
if (stat(socket_path, &st) == 0) { if (stat(socket_path, &st) == 0) {
if (!S_ISSOCK(st.st_mode)) { if (!S_ISSOCK(st.st_mode)) {