abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 884e3bb2ca3cbdb0e23799c10cce7d55139d1f1c
parent e48ea73ce0e636b8d7b86e4ed0609fdd403f433a
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 17 Mar 2018 19:29:43 +0100

Print server process ID in session list

Diffstat:
abduco.c | 21++++++++++++++-------
debug.c | 4++++
server.c | 8++++++++
3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/abduco.c b/abduco.c @@ -68,6 +68,7 @@ enum PacketType { MSG_DETACH = 2, MSG_RESIZE = 3, MSG_EXIT = 4, + MSG_PID = 5, }; typedef struct { @@ -80,6 +81,7 @@ typedef struct { uint16_t cols; } ws; uint32_t i; + uint64_t l; } u; } Packet; @@ -256,11 +258,15 @@ static int session_connect(const char *name) { return fd; } -static bool session_exists(const char *name) { - int fd = session_connect(name); - if (fd != -1) - close(fd); - return fd != -1; +static pid_t session_exists(const char *name) { + Packet pkt; + pid_t pid = 0; + if ((server.socket = session_connect(name)) == -1) + return pid; + if (client_recv_packet(&pkt) && pkt.type == MSG_PID) + pid = pkt.u.l; + close(server.socket); + return pid; } static bool session_alive(const char *name) { @@ -553,19 +559,20 @@ static int list_session(void) { while (n--) { struct stat sb; char buf[255]; if (stat(namelist[n]->d_name, &sb) == 0 && S_ISSOCK(sb.st_mode)) { + pid_t pid = 0; strftime(buf, sizeof(buf), "%a%t %F %T", localtime(&sb.st_mtime)); char status = ' '; char *local = strstr(namelist[n]->d_name, server.host); if (local) { *local = '\0'; /* truncate hostname if we are local */ - if (!session_exists(namelist[n]->d_name)) + if (!(pid = session_exists(namelist[n]->d_name))) continue; } if (sb.st_mode & S_IXUSR) status = '*'; else if (sb.st_mode & S_IXGRP) status = '+'; - printf("%c %s\t%s\n", status, buf, namelist[n]->d_name); + printf("%c %s\t%jd\t%s\n", status, buf, (intmax_t)pid, namelist[n]->d_name); } free(namelist[n]); } diff --git a/debug.c b/debug.c @@ -17,6 +17,7 @@ static void print_packet(const char *prefix, Packet *pkt) { [MSG_DETACH] = "DETACH", [MSG_RESIZE] = "RESIZE", [MSG_EXIT] = "EXIT", + [MSG_PID] = "PID", }; const char *type = "UNKNOWN"; if (pkt->type < countof(msgtype) && msgtype[pkt->type]) @@ -38,6 +39,9 @@ static void print_packet(const char *prefix, Packet *pkt) { case MSG_EXIT: fprintf(stderr, "status: %"PRIu32, pkt->u.i); break; + case MSG_PID: + fprintf(stderr, "pid: %"PRIu32, pkt->u.i); + break; default: fprintf(stderr, "len: %"PRIu32, pkt->len); break; diff --git a/server.c b/server.c @@ -150,6 +150,14 @@ static Client *server_accept_client(void) { c->next = server.clients; server.clients = c; server.read_pty = true; + + Packet pkt = { + .type = MSG_PID, + .len = sizeof pkt.u.l, + .u.l = getpid(), + }; + server_send_packet(c, &pkt); + return c; error: if (newfd != -1)