mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-20 05:33:47 -04:00
main: add --print-pid=FILE|FD
When specified, our PID is written to the specified file (or FD), after we've successfully started up. Only applicable in server mode.
This commit is contained in:
parent
9d52c422e1
commit
00d76784f4
3 changed files with 73 additions and 10 deletions
|
|
@ -8,6 +8,7 @@ _arguments \
|
||||||
'(-g --geometry)'{-g,--geometry}'[window WIDTHxHEIGHT, in pixels (80x24 cells)]:geometry:()' \
|
'(-g --geometry)'{-g,--geometry}'[window WIDTHxHEIGHT, in pixels (80x24 cells)]:geometry:()' \
|
||||||
'(-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' \
|
||||||
'(-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]'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,13 @@ execute (instead of the shell).
|
||||||
*--hold*
|
*--hold*
|
||||||
Remain open after child process exits.
|
Remain open after child process exits.
|
||||||
|
|
||||||
|
*-p*,*--print-pid*=_FILE_|_FD_
|
||||||
|
Print PID to this file, or FD, when successfully started. The file
|
||||||
|
(or FD) is closed immediately after writing the PID. When a _FILE_
|
||||||
|
as been specified, the file is unlinked when yambar exits.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
75
main.c
75
main.c
|
|
@ -9,7 +9,9 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/sysinfo.h>
|
#include <sys/sysinfo.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include <fcft/fcft.h>
|
#include <fcft/fcft.h>
|
||||||
|
|
||||||
|
|
@ -41,14 +43,15 @@ print_usage(const char *prog_name)
|
||||||
"Usage: %s [OPTIONS]... -- command\n"
|
"Usage: %s [OPTIONS]... -- command\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -c,--config=PATH load configuration from PATH (XDG_CONFIG_HOME/footrc)\n"
|
" -c,--config=PATH load configuration from PATH (XDG_CONFIG_HOME/footrc)\n"
|
||||||
" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n"
|
" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n"
|
||||||
" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
|
" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
|
||||||
" -g,--geometry=WIDTHxHEIGHT set initial width and height\n"
|
" -g,--geometry=WIDTHxHEIGHT set initial width and height\n"
|
||||||
" -s,--server[=PATH] run as a server (use 'footclient' to start terminals).\n"
|
" -s,--server[=PATH] run as a server (use 'footclient' to start terminals).\n"
|
||||||
" 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"
|
||||||
" -v,--version show the version number and quit\n",
|
" -p,--print-pid=FILE|FD print PID to file or FD (only applicable in server mode)\n"
|
||||||
|
" -v,--version show the version number and quit\n",
|
||||||
prog_name, prog_name);
|
prog_name, prog_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +83,43 @@ term_shutdown_cb(void *data, int exit_code)
|
||||||
ctx->exit_code = exit_code;
|
ctx->exit_code = exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
print_pid(const char *pid_file, bool *unlink_at_exit)
|
||||||
|
{
|
||||||
|
LOG_DBG("printing PID to %s", pid_file);
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
char *end;
|
||||||
|
int pid_fd = strtoul(pid_file, &end, 10);
|
||||||
|
|
||||||
|
if (errno != 0 || *end != '\0') {
|
||||||
|
if ((pid_fd = open(pid_file,
|
||||||
|
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
|
||||||
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
|
||||||
|
LOG_ERRNO("%s: failed to open", pid_file);
|
||||||
|
return false;
|
||||||
|
} else
|
||||||
|
*unlink_at_exit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid_fd >= 0) {
|
||||||
|
char pid[32];
|
||||||
|
snprintf(pid, sizeof(pid), "%u\n", getpid());
|
||||||
|
|
||||||
|
ssize_t bytes = write(pid_fd, pid, strlen(pid));
|
||||||
|
close(pid_fd);
|
||||||
|
|
||||||
|
if (bytes < 0) {
|
||||||
|
LOG_ERRNO("failed to write PID to FD=%u", pid_fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_DBG("wrote %zd bytes to FD=%d", bytes, pid_fd);
|
||||||
|
return true;
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *const *argv)
|
main(int argc, char *const *argv)
|
||||||
{
|
{
|
||||||
|
|
@ -98,7 +138,8 @@ main(int argc, char *const *argv)
|
||||||
{"geometry", required_argument, NULL, 'g'},
|
{"geometry", required_argument, NULL, 'g'},
|
||||||
{"server", optional_argument, NULL, 's'},
|
{"server", optional_argument, NULL, 's'},
|
||||||
{"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'},
|
||||||
{"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},
|
||||||
|
|
@ -113,6 +154,8 @@ main(int argc, char *const *argv)
|
||||||
const char *conf_server_socket_path = NULL;
|
const char *conf_server_socket_path = NULL;
|
||||||
bool presentation_timings = false;
|
bool presentation_timings = false;
|
||||||
bool hold = false;
|
bool hold = false;
|
||||||
|
bool unlink_pid_file = false;
|
||||||
|
const char *pid_file = NULL;
|
||||||
|
|
||||||
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::pvh", longopts, NULL);
|
||||||
|
|
@ -168,7 +211,7 @@ main(int argc, char *const *argv)
|
||||||
conf_server_socket_path = optarg;
|
conf_server_socket_path = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'p':
|
case 'P':
|
||||||
presentation_timings = true;
|
presentation_timings = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -176,6 +219,10 @@ main(int argc, char *const *argv)
|
||||||
hold = true;
|
hold = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'p':
|
||||||
|
pid_file = optarg;
|
||||||
|
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;
|
||||||
|
|
@ -277,6 +324,11 @@ main(int argc, char *const *argv)
|
||||||
if (as_server)
|
if (as_server)
|
||||||
LOG_INFO("running as server; launch terminals by running footclient");
|
LOG_INFO("running as server; launch terminals by running footclient");
|
||||||
|
|
||||||
|
if (as_server && pid_file != NULL) {
|
||||||
|
if (!print_pid(pid_file, &unlink_pid_file))
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
while (!aborted && (as_server || tll_length(wayl->terms) > 0)) {
|
while (!aborted && (as_server || tll_length(wayl->terms) > 0)) {
|
||||||
if (!fdm_poll(fdm))
|
if (!fdm_poll(fdm))
|
||||||
break;
|
break;
|
||||||
|
|
@ -295,6 +347,9 @@ out:
|
||||||
|
|
||||||
config_free(conf);
|
config_free(conf);
|
||||||
|
|
||||||
|
if (unlink_pid_file)
|
||||||
|
unlink(pid_file);
|
||||||
|
|
||||||
LOG_INFO("goodbye");
|
LOG_INFO("goodbye");
|
||||||
log_deinit();
|
log_deinit();
|
||||||
return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret;
|
return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue