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_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]);
}

View File

@ -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;

View File

@ -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)