main: add --print-pid=FILE|FD

When specified, our PID is written to the specified file (or FD),
after we've successfully started up.
This commit is contained in:
Daniel Eklöf 2020-02-05 19:54:16 +01:00
parent 00d76784f4
commit 82b8853f17
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
8 changed files with 80 additions and 18 deletions

View file

@ -32,15 +32,15 @@ print_usage(const char *prog_name)
printf("Usage: %s [OPTIONS]... -- command\n", prog_name); printf("Usage: %s [OPTIONS]... -- command\n", prog_name);
printf("\n"); printf("\n");
printf("Options:\n"); printf("Options:\n");
printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n" printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
" -s,--server-socket=PATH path to the server UNIX domain socket (default=XDG_RUNTIME_DIR/foot.sock)\n" " -s,--server-socket=PATH path to the server UNIX domain socket (default=XDG_RUNTIME_DIR/foot.sock)\n"
" -v,--version show the version number and quit\n"); " -l,--log-colorize=[never|always|auto] enable/disable colorization of log output on stderr\n"
" -v,--version show the version number and quit\n");
} }
int int
main(int argc, char *const *argv) main(int argc, char *const *argv)
{ {
log_init(LOG_FACILITY_USER, LOG_CLASS_WARNING);
int ret = EXIT_FAILURE; int ret = EXIT_FAILURE;
const char *const prog_name = argv[0]; const char *const prog_name = argv[0];
@ -48,6 +48,7 @@ main(int argc, char *const *argv)
static const struct option longopts[] = { static const struct option longopts[] = {
{"term", required_argument, 0, 't'}, {"term", required_argument, 0, 't'},
{"server-socket", required_argument, 0, 's'}, {"server-socket", required_argument, 0, 's'},
{"log-colorize", optional_argument, NULL, 'l'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{NULL, no_argument, 0, 0}, {NULL, no_argument, 0, 0},
@ -55,9 +56,10 @@ main(int argc, char *const *argv)
const char *term = ""; const char *term = "";
const char *server_socket_path = NULL; const char *server_socket_path = NULL;
enum log_colorize log_colorize = LOG_COLORIZE_AUTO;
while (true) { while (true) {
int c = getopt_long(argc, argv, ":t:s:hv", longopts, NULL); int c = getopt_long(argc, argv, ":t:s:l::hv", longopts, NULL);
if (c == -1) if (c == -1)
break; break;
@ -70,6 +72,19 @@ main(int argc, char *const *argv)
server_socket_path = optarg; server_socket_path = optarg;
break; break;
case 'l':
if (optarg == NULL || strcmp(optarg, "auto") == 0)
log_colorize = LOG_COLORIZE_AUTO;
else if (strcmp(optarg, "never") == 0)
log_colorize = LOG_COLORIZE_NEVER;
else if (strcmp(optarg, "always") == 0)
log_colorize = LOG_COLORIZE_ALWAYS;
else {
fprintf(stderr, "%s: argument must be one of 'never', 'always' or 'auto'\n", optarg);
return EXIT_FAILURE;
}
break;
case 'v': case 'v':
printf("footclient version %s\n", FOOT_VERSION); printf("footclient version %s\n", FOOT_VERSION);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -91,6 +106,8 @@ main(int argc, char *const *argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
log_init(log_colorize, false, LOG_FACILITY_USER, LOG_CLASS_WARNING);
int fd = socket(AF_UNIX, SOCK_STREAM, 0); int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) { if (fd == -1) {
LOG_ERRNO("failed to create socket"); LOG_ERRNO("failed to create socket");

View file

@ -9,6 +9,8 @@ _arguments \
'(-s --server)'{-s,--server}'[run as server; open terminals by running footclient]:server:_files' \ '(-s --server)'{-s,--server}'[run as server; open terminals by running footclient]:server:_files' \
'--hold[remain open after child process exits]' \ '--hold[remain open after child process exits]' \
'(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running (server mode only)]:pidfile:_files' \ '(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running (server mode only)]:pidfile:_files' \
'(-l --log-colorize)'{-l,--log-colorize}'[enable or disable colorization of log output on stderr]:logcolor:(never always auto)' \
'(-S --log-no-syslog)'{-s,--log-no-syslog}'[disable syslog logging (server mode only)]' \
'(-v --version)'{-v,--version}'[show the version number and quit]' \ '(-v --version)'{-v,--version}'[show the version number and quit]' \
'(-h --help)'{-h,--help}'[show help message and quit]' '(-h --help)'{-h,--help}'[show help message and quit]'

View file

@ -4,6 +4,7 @@ _arguments \
-s \ -s \
'(-t --term)'{-t,--term}'[value to set the environment variable TERM to (foot)]:term:->terms' \ '(-t --term)'{-t,--term}'[value to set the environment variable TERM to (foot)]:term:->terms' \
'(-s --server-socket)'{-s,--server-socket}'[override the default path to the foot server socket (XDG_RUNTIME_DIR/foot.sock)]:server:_files' \ '(-s --server-socket)'{-s,--server-socket}'[override the default path to the foot server socket (XDG_RUNTIME_DIR/foot.sock)]:server:_files' \
'(-l --log-colorize)'{-l,--log-colorize}'[enable or disable colorization of log output on stderr]:logcolor:(never always auto)' \
'(-v --version)'{-v,--version}'[show the version number and quit]' \ '(-v --version)'{-v,--version}'[show the version number and quit]' \
'(-h --help)'{-h,--help}'[show help message and quit]' '(-h --help)'{-h,--help}'[show help message and quit]'

View file

@ -72,6 +72,13 @@ execute (instead of the shell).
This option can only be used in combination with *-s*,*--server*. This option can only be used in combination with *-s*,*--server*.
*-l*,*--log-colorize*=[{*never*,*always*,*auto*}]
Enables or disables colorization of log output on stderr.
*-S*,*--log-no-syslog*
Disables syslog logging. Logging is only done on stderr. This
option can only be used in combination with *-s*,*--server*.
*-v*,*--version* *-v*,*--version*
Show the version number and quit. Show the version number and quit.

View file

@ -18,6 +18,9 @@ execute (instead of the shell).
*-s*,*--server-socket*=_PATH_ *-s*,*--server-socket*=_PATH_
Connect to _PATH_ instead of _XDG\_RUNTIME\_DIR/foot.sock_. Connect to _PATH_ instead of _XDG\_RUNTIME\_DIR/foot.sock_.
*-l*,*--log-colorize*=[{*never*,*always*,*auto*}]
Enables or disables colorization of log output on stderr.
*-v*,*--version* *-v*,*--version*
Show the version number and quit Show the version number and quit

25
log.c
View file

@ -12,15 +12,11 @@
#include <syslog.h> #include <syslog.h>
static bool colorize = false; static bool colorize = false;
static bool do_syslog = true;
static void __attribute__((constructor))
init(void)
{
colorize = isatty(STDERR_FILENO);
}
void void
log_init(enum log_facility syslog_facility, enum log_class syslog_level) log_init(enum log_colorize _colorize, bool _do_syslog,
enum log_facility syslog_facility, enum log_class syslog_level)
{ {
static const int facility_map[] = { static const int facility_map[] = {
[LOG_FACILITY_USER] = LOG_USER, [LOG_FACILITY_USER] = LOG_USER,
@ -34,14 +30,20 @@ log_init(enum log_facility syslog_facility, enum log_class syslog_level)
[LOG_CLASS_DEBUG] = LOG_DEBUG, [LOG_CLASS_DEBUG] = LOG_DEBUG,
}; };
openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]); colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO);
setlogmask(LOG_UPTO(level_map[syslog_level])); do_syslog = _do_syslog;
if (do_syslog) {
openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
setlogmask(LOG_UPTO(level_map[syslog_level]));
}
} }
void void
log_deinit(void) log_deinit(void)
{ {
closelog(); if (do_syslog)
closelog();
} }
static void static void
@ -85,6 +87,9 @@ _sys_log(enum log_class log_class, const char *module,
int lineno __attribute__((unused)), int lineno __attribute__((unused)),
const char *fmt, int sys_errno, va_list va) const char *fmt, int sys_errno, va_list va)
{ {
if (!do_syslog)
return;
/* Map our log level to syslog's level */ /* Map our log level to syslog's level */
int level = -1; int level = -1;
switch (log_class) { switch (log_class) {

5
log.h
View file

@ -1,9 +1,12 @@
#pragma once #pragma once
#include <stdbool.h>
enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO };
enum log_facility { LOG_FACILITY_USER, LOG_FACILITY_DAEMON }; enum log_facility { LOG_FACILITY_USER, LOG_FACILITY_DAEMON };
enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG }; enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG };
void log_init(enum log_facility syslog_facility, enum log_class syslog_level); void log_init(enum log_colorize colorize, bool do_syslog,
enum log_facility syslog_facility, enum log_class syslog_level);
void log_deinit(void); void log_deinit(void);
void log_msg(enum log_class log_class, const char *module, void log_msg(enum log_class log_class, const char *module,

28
main.c
View file

@ -51,6 +51,8 @@ print_usage(const char *prog_name)
" Without PATH, XDG_RUNTIME_DIR/foot.sock will be used.\n" " Without PATH, XDG_RUNTIME_DIR/foot.sock will be used.\n"
" --hold remain open after child process exits\n" " --hold remain open after child process exits\n"
" -p,--print-pid=FILE|FD print PID to file or FD (only applicable in server mode)\n" " -p,--print-pid=FILE|FD print PID to file or FD (only applicable in server mode)\n"
" -l,--log-colorize=[never|always|auto] enable/disable colorization of log output on stderr\n"
" -s,--log-no-syslog disable syslog logging (only applicable in server mode)\n"
" -v,--version show the version number and quit\n", " -v,--version show the version number and quit\n",
prog_name, prog_name); prog_name, prog_name);
} }
@ -140,6 +142,8 @@ main(int argc, char *const *argv)
{"hold", no_argument, NULL, 'H'}, {"hold", no_argument, NULL, 'H'},
{"presentation-timings", no_argument, NULL, 'P'}, /* Undocumented */ {"presentation-timings", no_argument, NULL, 'P'}, /* Undocumented */
{"print-pid", required_argument, NULL, 'p'}, {"print-pid", required_argument, NULL, 'p'},
{"log-colorize", optional_argument, NULL, 'l'},
{"log-no-syslog", no_argument, NULL, 'S'},
{"version", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{NULL, no_argument, NULL, 0}, {NULL, no_argument, NULL, 0},
@ -156,9 +160,11 @@ main(int argc, char *const *argv)
bool hold = false; bool hold = false;
bool unlink_pid_file = false; bool unlink_pid_file = false;
const char *pid_file = NULL; const char *pid_file = NULL;
enum log_colorize log_colorize = LOG_COLORIZE_AUTO;
bool log_syslog = true;
while (true) { while (true) {
int c = getopt_long(argc, argv, "c:tf:g:s::pvh", longopts, NULL); int c = getopt_long(argc, argv, "c:tf:g:s::Pp:l::Svh", longopts, NULL);
if (c == -1) if (c == -1)
break; break;
@ -223,6 +229,23 @@ main(int argc, char *const *argv)
pid_file = optarg; pid_file = optarg;
break; break;
case 'l':
if (optarg == NULL || strcmp(optarg, "auto") == 0)
log_colorize = LOG_COLORIZE_AUTO;
else if (strcmp(optarg, "never") == 0)
log_colorize = LOG_COLORIZE_NEVER;
else if (strcmp(optarg, "always") == 0)
log_colorize = LOG_COLORIZE_ALWAYS;
else {
fprintf(stderr, "%s: argument must be one of 'never', 'always' or 'auto'\n", optarg);
return EXIT_FAILURE;
}
break;
case 'S':
log_syslog = false;
break;
case 'v': case 'v':
printf("foot version %s\n", FOOT_VERSION); printf("foot version %s\n", FOOT_VERSION);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -236,7 +259,8 @@ main(int argc, char *const *argv)
} }
} }
log_init(as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER, LOG_CLASS_WARNING); log_init(log_colorize, as_server && log_syslog,
as_server ? LOG_FACILITY_DAEMON : LOG_FACILITY_USER, LOG_CLASS_WARNING);
argc -= optind; argc -= optind;
argv += optind; argv += optind;