Print server process ID in session list

This commit is contained in:
Marc André Tanner 2018-03-17 19:29:43 +01:00
parent e48ea73ce0
commit 884e3bb2ca
3 changed files with 26 additions and 7 deletions

View File

@ -68,6 +68,7 @@ enum PacketType {
MSG_DETACH = 2, MSG_DETACH = 2,
MSG_RESIZE = 3, MSG_RESIZE = 3,
MSG_EXIT = 4, MSG_EXIT = 4,
MSG_PID = 5,
}; };
typedef struct { typedef struct {
@ -80,6 +81,7 @@ typedef struct {
uint16_t cols; uint16_t cols;
} ws; } ws;
uint32_t i; uint32_t i;
uint64_t l;
} u; } u;
} Packet; } Packet;
@ -256,11 +258,15 @@ static int session_connect(const char *name) {
return fd; return fd;
} }
static bool session_exists(const char *name) { static pid_t session_exists(const char *name) {
int fd = session_connect(name); Packet pkt;
if (fd != -1) pid_t pid = 0;
close(fd); if ((server.socket = session_connect(name)) == -1)
return fd != -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) { static bool session_alive(const char *name) {
@ -553,19 +559,20 @@ static int list_session(void) {
while (n--) { while (n--) {
struct stat sb; char buf[255]; struct stat sb; char buf[255];
if (stat(namelist[n]->d_name, &sb) == 0 && S_ISSOCK(sb.st_mode)) { 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)); strftime(buf, sizeof(buf), "%a%t %F %T", localtime(&sb.st_mtime));
char status = ' '; char status = ' ';
char *local = strstr(namelist[n]->d_name, server.host); char *local = strstr(namelist[n]->d_name, server.host);
if (local) { if (local) {
*local = '\0'; /* truncate hostname if we are 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; continue;
} }
if (sb.st_mode & S_IXUSR) if (sb.st_mode & S_IXUSR)
status = '*'; status = '*';
else if (sb.st_mode & S_IXGRP) else if (sb.st_mode & S_IXGRP)
status = '+'; 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]); free(namelist[n]);
} }

View File

@ -17,6 +17,7 @@ static void print_packet(const char *prefix, Packet *pkt) {
[MSG_DETACH] = "DETACH", [MSG_DETACH] = "DETACH",
[MSG_RESIZE] = "RESIZE", [MSG_RESIZE] = "RESIZE",
[MSG_EXIT] = "EXIT", [MSG_EXIT] = "EXIT",
[MSG_PID] = "PID",
}; };
const char *type = "UNKNOWN"; const char *type = "UNKNOWN";
if (pkt->type < countof(msgtype) && msgtype[pkt->type]) if (pkt->type < countof(msgtype) && msgtype[pkt->type])
@ -38,6 +39,9 @@ static void print_packet(const char *prefix, Packet *pkt) {
case MSG_EXIT: case MSG_EXIT:
fprintf(stderr, "status: %"PRIu32, pkt->u.i); fprintf(stderr, "status: %"PRIu32, pkt->u.i);
break; break;
case MSG_PID:
fprintf(stderr, "pid: %"PRIu32, pkt->u.i);
break;
default: default:
fprintf(stderr, "len: %"PRIu32, pkt->len); fprintf(stderr, "len: %"PRIu32, pkt->len);
break; break;

View File

@ -150,6 +150,14 @@ static Client *server_accept_client(void) {
c->next = server.clients; c->next = server.clients;
server.clients = c; server.clients = c;
server.read_pty = true; server.read_pty = true;
Packet pkt = {
.type = MSG_PID,
.len = sizeof pkt.u.l,
.u.l = getpid(),
};
server_send_packet(c, &pkt);
return c; return c;
error: error:
if (newfd != -1) if (newfd != -1)