-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathattach.c
More file actions
829 lines (726 loc) · 19.2 KB
/
attach.c
File metadata and controls
829 lines (726 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include "atch.h"
#ifndef VDISABLE
#ifdef _POSIX_VDISABLE
#define VDISABLE _POSIX_VDISABLE
#else
#define VDISABLE 0377
#endif
#endif
/*
** The current terminal settings. After coming back from a suspend, we
** restore this.
*/
static struct termios cur_term;
/* 1 if the window size changed */
static int win_changed;
/* Socket creation time, used to compute session age in messages. */
time_t session_start;
char const *clear_csi_data(void)
{
if (no_ansiterm || clear_method == CLEAR_NONE ||
(clear_method == CLEAR_UNSPEC && dont_have_tty))
return "\r\n";
/* CLEAR_MOVE, or CLEAR_UNSPEC with a real tty: move to bottom */
return "\033[999H\r\n";
}
/* Write buf to fd handling partial writes. Exit on failure. */
void write_buf_or_fail(int fd, const void *buf, size_t count)
{
while (count != 0) {
ssize_t ret = write(fd, buf, count);
if (ret >= 0) {
buf = (const char *)buf + ret;
count -= ret;
} else if (ret < 0 && errno == EINTR)
continue;
else {
if (session_start) {
char age[32];
session_age(age, sizeof(age));
printf
("%s[%s: session '%s' write failed after %s]\r\n",
clear_csi_data(), progname,
session_shortname(), age);
} else {
printf("%s[%s: write failed]\r\n",
clear_csi_data(), progname);
}
exit(1);
}
}
}
/* Write pkt to fd. Exit on failure. */
void write_packet_or_fail(int fd, const struct packet *pkt)
{
while (1) {
ssize_t ret = write(fd, pkt, sizeof(struct packet));
if (ret == sizeof(struct packet))
return;
else if (ret < 0 && errno == EINTR)
continue;
else {
if (session_start) {
char age[32];
session_age(age, sizeof(age));
printf
("%s[%s: session '%s' write failed after %s]\r\n",
clear_csi_data(), progname,
session_shortname(), age);
} else {
printf("%s[%s: write failed]\r\n",
clear_csi_data(), progname);
}
exit(1);
}
}
}
/* Restores the original terminal settings. */
static void restore_term(void)
{
tcsetattr(0, TCSADRAIN, &orig_term);
if (!no_ansiterm) {
printf("\033[0m\033[?25h");
}
fflush(stdout);
if (no_ansiterm)
(void)system("tput init 2>/dev/null");
}
/* Connects to a unix domain socket */
static int connect_socket(char *name)
{
int s;
struct sockaddr_un sockun;
if (strlen(name) > sizeof(sockun.sun_path) - 1)
return socket_with_chdir(name, connect_socket);
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
sockun.sun_family = AF_UNIX;
memcpy(sockun.sun_path, name, strlen(name) + 1);
if (connect(s, (struct sockaddr *)&sockun, sizeof(sockun)) < 0) {
close(s);
/* ECONNREFUSED is also returned for regular files, so make
** sure we are trying to connect to a socket. */
if (errno == ECONNREFUSED) {
struct stat st;
if (stat(name, &st) < 0)
return -1;
else if (!S_ISSOCK(st.st_mode))
errno = ENOTSOCK;
}
return -1;
}
return s;
}
void format_age(time_t secs, char *buf, size_t size)
{
int d = (int)(secs / 86400);
int h = (int)((secs % 86400) / 3600);
int m = (int)((secs % 3600) / 60);
int s = (int)(secs % 60);
if (d > 0)
snprintf(buf, size, "%dd %dh %dm %ds", d, h, m, s);
else if (h > 0)
snprintf(buf, size, "%dh %dm %ds", h, m, s);
else if (m > 0)
snprintf(buf, size, "%dm %ds", m, s);
else
snprintf(buf, size, "%ds", s);
}
void session_age(char *buf, size_t size)
{
time_t now = time(NULL);
format_age(now > session_start ? now - session_start : 0, buf, size);
}
/* Signal */
static RETSIGTYPE die(int sig)
{
char age[32];
session_age(age, sizeof(age));
/* Print a nice pretty message for some things. */
if (sig == SIGHUP || sig == SIGINT)
printf("%s[%s: session '%s' detached after %s]\r\n",
clear_csi_data(), progname, session_shortname(), age);
else
printf
("%s[%s: session '%s' got signal %d - exiting after %s]\r\n",
clear_csi_data(), progname, session_shortname(), sig, age);
exit(1);
}
/* Window size change. */
static RETSIGTYPE win_change(ATTRIBUTE_UNUSED int sig)
{
signal(SIGWINCH, win_change);
win_changed = 1;
}
/* Handles input from the keyboard. */
static void process_kbd(int s, struct packet *pkt)
{
/* Suspend? */
if (!no_suspend && (pkt->u.buf[0] == cur_term.c_cc[VSUSP])) {
/* Tell the master that we are suspending. */
pkt->type = MSG_DETACH;
write_packet_or_fail(s, pkt);
/* And suspend... */
tcsetattr(0, TCSADRAIN, &orig_term);
printf("%s", clear_csi_data());
kill(getpid(), SIGTSTP);
tcsetattr(0, TCSADRAIN, &cur_term);
/* Tell the master that we are returning. */
pkt->type = MSG_ATTACH;
pkt->len = 0; /* normal ring replay on resume */
write_packet_or_fail(s, pkt);
/* We would like a redraw, too. */
pkt->type = MSG_REDRAW;
pkt->len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt->u.ws);
write_packet_or_fail(s, pkt);
return;
}
/* Detach char? */
else if (pkt->u.buf[0] == detach_char) {
char age[32];
session_age(age, sizeof(age));
printf("%s[%s: session '%s' detached after %s]\r\n",
clear_csi_data(), progname, session_shortname(), age);
exit(0);
}
/* Just in case something pukes out. */
else if (pkt->u.buf[0] == '\f')
win_changed = 1;
/* Push it out */
write_packet_or_fail(s, pkt);
}
/* Set to 1 once we have replayed the on-disk log, so attach_main
** knows not to replay it a second time. */
static int log_already_replayed;
/* Replay sockname+".log" to stdout, if it exists.
** saved_errno is from the failed connect: ECONNREFUSED means the session was
** killed/crashed (socket still on disk), ENOENT means clean exit (socket was
** unlinked; end marker is already in the log).
** Pass 0 when replaying for a running session (no end message printed).
** Returns 1 if a log was found and replayed, 0 if no log exists. */
int replay_session_log(int saved_errno)
{
char log_path[600];
int logfd;
const char *name;
snprintf(log_path, sizeof(log_path), "%s.log", sockname);
logfd = open(log_path, O_RDONLY);
if (logfd < 0)
return 0;
{
unsigned char rbuf[BUFSIZE];
ssize_t n;
while ((n = read(logfd, rbuf, sizeof(rbuf))) > 0)
write(1, rbuf, (size_t)n);
close(logfd);
}
/* Socket still on disk = killed/crashed; clean exit already wrote its
* end marker into the log, so no extra message needed. */
if (saved_errno == ECONNREFUSED) {
name = session_shortname();
printf("%s[%s: session '%s' ended unexpectedly]\r\n",
clear_csi_data(), progname, name);
}
log_already_replayed = 1;
return 1;
}
int attach_main(int noerror)
{
struct packet pkt;
unsigned char buf[BUFSIZE];
fd_set readfds;
int s;
/* Refuse to attach to any session in our ancestry chain (catches both
* direct self-attach and indirect loops like A -> B -> A).
* SESSION_ENVVAR is the colon-separated chain, so scanning it covers
* all ancestors. */
{
const char *tosearch = getenv(SESSION_ENVVAR);
if (tosearch && *tosearch) {
size_t slen = strlen(sockname);
const char *p = tosearch;
while (*p) {
const char *colon = strchr(p, ':');
size_t tlen =
colon ? (size_t)(colon - p) : strlen(p);
if (tlen == slen
&& strncmp(p, sockname, tlen) == 0) {
if (!noerror)
printf
("%s: cannot attach to session '%s' from within itself\n",
progname,
session_shortname());
return 1;
}
if (!colon)
break;
p = colon + 1;
}
}
}
/* Attempt to open the socket. Don't display an error if noerror is
** set. */
s = connect_socket(sockname);
if (s < 0) {
int saved_errno = errno;
const char *name = session_shortname();
if (!noerror) {
if (!replay_session_log(saved_errno)) {
if (saved_errno == ENOENT)
printf
("%s: session '%s' does not exist\n",
progname, name);
else if (saved_errno == ECONNREFUSED)
printf
("%s: session '%s' is not running\n",
progname, name);
else if (saved_errno == ENOTSOCK)
printf
("%s: '%s' is not a valid session\n",
progname, name);
else
printf("%s: %s: %s\n", progname,
sockname, strerror(saved_errno));
}
}
return 1;
}
/* Replay the on-disk log so the user sees full session history.
** Skip if already replayed by the error path (exited-session case). */
int skip_ring = 0;
if (!log_already_replayed && replay_session_log(0))
skip_ring = 1;
/* Record session start time from the socket file's ctime. */
{
struct stat st;
session_start =
(stat(sockname, &st) == 0) ? st.st_mtime : time(NULL);
}
/* The current terminal settings are equal to the original terminal
** settings at this point. */
cur_term = orig_term;
/* Set a trap to restore the terminal when we die. */
atexit(restore_term);
/* Set some signals. */
signal(SIGPIPE, SIG_IGN);
signal(SIGXFSZ, SIG_IGN);
signal(SIGHUP, die);
signal(SIGTERM, die);
signal(SIGINT, die);
signal(SIGQUIT, die);
signal(SIGWINCH, win_change);
/* Set raw mode. */
cur_term.c_iflag &=
~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
cur_term.c_iflag &= ~(IXON | IXOFF);
cur_term.c_oflag &= ~(OPOST);
cur_term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
cur_term.c_cflag &= ~(CSIZE | PARENB);
cur_term.c_cflag |= CS8;
cur_term.c_cc[VLNEXT] = VDISABLE;
cur_term.c_cc[VMIN] = 1;
cur_term.c_cc[VTIME] = 0;
tcsetattr(0, TCSADRAIN, &cur_term);
/* Clear the screen on attach. Only do a full reset when explicitly
** requested (CLEAR_MOVE); default/unspec just emits a blank line so
** any preceding log replay remains visible.
** When log replay was done for a running session (skip_ring=1), skip
** the separator: the log ends at the exact pty cursor position, so
** the prompt is already visible and correctly placed. */
if (clear_method == CLEAR_MOVE && !no_ansiterm) {
write_buf_or_fail(1, "\033c", 2);
} else if (!quiet && !skip_ring) {
write_buf_or_fail(1, "\r\n", 2);
}
/* Tell the master that we want to attach.
** pkt.len=1 means the client already loaded the log; master skips
** the in-memory ring replay to avoid showing history twice. */
memset(&pkt, 0, sizeof(struct packet));
pkt.type = MSG_ATTACH;
pkt.len = skip_ring;
write_packet_or_fail(s, &pkt);
/* We would like a redraw, too. */
pkt.type = MSG_REDRAW;
pkt.len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write_packet_or_fail(s, &pkt);
/* Wait for things to happen */
while (1) {
int n;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
FD_SET(s, &readfds);
n = select(s + 1, &readfds, NULL, NULL, NULL);
if (n < 0 && errno != EINTR && errno != EAGAIN) {
char age[32];
session_age(age, sizeof(age));
printf
("%s[%s: session '%s' select failed after %s]\r\n",
clear_csi_data(), progname, session_shortname(),
age);
exit(1);
}
/* Pty activity */
if (n > 0 && FD_ISSET(s, &readfds)) {
ssize_t len = read(s, buf, sizeof(buf));
if (len == 0) {
if (!quiet) {
char age[32];
session_age(age, sizeof(age));
printf
("%s[%s: session '%s' exited after %s]\r\n",
clear_csi_data(), progname,
session_shortname(), age);
}
exit(0);
} else if (len < 0) {
char age[32];
session_age(age, sizeof(age));
printf
("%s[%s: session '%s' read error after %s]\r\n",
clear_csi_data(), progname,
session_shortname(), age);
exit(1);
}
/* Send the data to the terminal. */
write_buf_or_fail(1, buf, len);
n--;
}
/* stdin activity */
if (n > 0 && FD_ISSET(0, &readfds)) {
ssize_t len;
pkt.type = MSG_PUSH;
memset(pkt.u.buf, 0, sizeof(pkt.u.buf));
len = read(0, pkt.u.buf, sizeof(pkt.u.buf));
if (len <= 0)
exit(1);
pkt.len = len;
process_kbd(s, &pkt);
n--;
}
/* Window size changed? */
if (win_changed) {
win_changed = 0;
pkt.type = MSG_WINCH;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write_packet_or_fail(s, &pkt);
}
}
return 0;
}
int push_main()
{
struct packet pkt;
int s;
/* Attempt to open the socket. */
s = connect_socket(sockname);
if (s < 0) {
printf("%s: %s: %s\n", progname, sockname, strerror(errno));
return 1;
}
/* Set some signals. */
signal(SIGPIPE, SIG_IGN);
/* Push the contents of standard input to the socket. */
pkt.type = MSG_PUSH;
for (;;) {
ssize_t len;
memset(pkt.u.buf, 0, sizeof(pkt.u.buf));
len = read(0, pkt.u.buf, sizeof(pkt.u.buf));
if (len == 0)
return 0;
else if (len < 0) {
printf("%s: %s: %s\n", progname, sockname,
strerror(errno));
return 1;
}
pkt.len = len;
len = write(s, &pkt, sizeof(struct packet));
if (len != sizeof(struct packet)) {
if (len >= 0)
errno = EPIPE;
printf("%s: %s: %s\n", progname, sockname,
strerror(errno));
return 1;
}
}
}
static int send_kill(int sig)
{
struct packet pkt;
int s;
ssize_t ret;
s = connect_socket(sockname);
if (s < 0)
return -1;
memset(&pkt, 0, sizeof(pkt));
pkt.type = MSG_KILL;
pkt.len = (unsigned char)sig;
ret = write(s, &pkt, sizeof(pkt));
close(s);
return (ret == sizeof(pkt)) ? 0 : -1;
}
static int session_gone(void)
{
struct stat st;
return stat(sockname, &st) < 0 && errno == ENOENT;
}
int kill_main(int force)
{
const char *name = session_shortname();
int i;
signal(SIGPIPE, SIG_IGN);
if (force) {
/* Skip the grace period — send SIGKILL immediately. */
if (send_kill(SIGKILL) < 0) {
if (errno == ENOENT)
printf("%s: session '%s' does not exist\n",
progname, name);
else if (errno == ECONNREFUSED)
printf("%s: session '%s' is not running\n",
progname, name);
else
printf("%s: %s: %s\n", progname, sockname,
strerror(errno));
return 1;
}
for (i = 0; i < 20; i++) {
usleep(100000);
if (session_gone()) {
if (!quiet)
printf("%s: session '%s' killed\n",
progname, name);
return 0;
}
}
printf("%s: session '%s' did not stop\n", progname, name);
return 1;
}
if (send_kill(SIGTERM) < 0) {
if (errno == ENOENT)
printf("%s: session '%s' does not exist\n",
progname, name);
else if (errno == ECONNREFUSED)
printf("%s: session '%s' is not running\n",
progname, name);
else
printf("%s: %s: %s\n", progname, sockname,
strerror(errno));
return 1;
}
/* Wait up to 5 seconds for graceful exit. */
for (i = 0; i < 50; i++) {
usleep(100000);
if (session_gone()) {
printf("%s: session '%s' stopped\n", progname, name);
return 0;
}
}
/* Still alive — escalate to SIGKILL. */
send_kill(SIGKILL);
for (i = 0; i < 20; i++) {
usleep(100000);
if (session_gone()) {
printf("%s: session '%s' killed\n", progname, name);
return 0;
}
}
printf("%s: session '%s' did not stop\n", progname, name);
return 1;
}
int rm_main(int all)
{
char log_path[600];
if (!all) {
const char *name = session_shortname();
int s;
snprintf(log_path, sizeof(log_path), "%s.log", sockname);
s = connect_socket(sockname);
if (s >= 0) {
close(s);
printf("%s: session '%s' is running"
" (use '%s kill %s' first)\n",
progname, name, progname, name);
return 1;
}
if (errno == ECONNREFUSED) {
unlink(sockname);
unlink(log_path);
if (!quiet)
printf("%s: session '%s' removed\n",
progname, name);
return 0;
}
if (errno == ENOENT) {
struct stat st;
if (stat(log_path, &st) == 0) {
unlink(log_path);
if (!quiet)
printf("%s: session '%s' removed\n",
progname, name);
return 0;
}
printf("%s: session '%s' does not exist\n",
progname, name);
return 1;
}
printf("%s: %s: %s\n", progname, sockname, strerror(errno));
return 1;
}
/* Remove all stale + exited sessions. */
{
char dir[512];
char path[768];
char log_full[800];
DIR *d;
struct dirent *ent;
int count = 0;
get_session_dir(dir, sizeof(dir));
/* Pass 1: stale sockets (ECONNREFUSED). */
d = opendir(dir);
if (d) {
while ((ent = readdir(d)) != NULL) {
struct stat st;
int s;
if (ent->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s",
dir, ent->d_name);
if (stat(path, &st) < 0 ||
!S_ISSOCK(st.st_mode))
continue;
s = connect_socket(path);
if (s >= 0) {
close(s);
continue;
}
if (errno != ECONNREFUSED)
continue;
snprintf(log_full, sizeof(log_full),
"%s/%s.log", dir, ent->d_name);
unlink(path);
unlink(log_full);
if (!quiet)
printf("%s: removed %s\n",
progname, ent->d_name);
count++;
}
closedir(d);
}
/* Pass 2: exited sessions (log only, no socket). */
d = opendir(dir);
if (d) {
while ((ent = readdir(d)) != NULL) {
size_t nlen = strlen(ent->d_name);
if (nlen <= 4 ||
strcmp(ent->d_name + nlen - 4,
".log") != 0)
continue;
snprintf(path, sizeof(path), "%s/%.*s",
dir, (int)(nlen - 4), ent->d_name);
if (access(path, F_OK) == 0)
continue;
snprintf(log_full, sizeof(log_full),
"%s/%s", dir, ent->d_name);
unlink(log_full);
if (!quiet)
printf("%s: removed %.*s\n",
progname, (int)(nlen - 4),
ent->d_name);
count++;
}
closedir(d);
}
if (!quiet) {
if (count == 0)
printf("%s: nothing to remove\n", progname);
else
printf("%s: %d session(s) removed\n",
progname, count);
}
return 0;
}
}
int list_main(int show_all)
{
char dir[512];
char path[768]; /* sizeof(dir) + '/' + NAME_MAX */
DIR *d;
struct dirent *ent;
time_t now = time(NULL);
int count = 0;
get_session_dir(dir, sizeof(dir));
d = opendir(dir);
if (!d) {
if (errno == ENOENT)
goto empty;
printf("%s: %s: %s\n", progname, dir, strerror(errno));
return 1;
}
while ((ent = readdir(d)) != NULL) {
struct stat st;
char age[32];
int s;
if (ent->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", dir, ent->d_name);
if (stat(path, &st) < 0 || !S_ISSOCK(st.st_mode))
continue;
format_age(now > st.st_mtime ? now - st.st_mtime : 0,
age, sizeof(age));
s = connect_socket(path);
if (s >= 0) {
int attached = st.st_mode & S_IXUSR;
close(s);
if (attached)
printf("%-24s since %s ago [attached]\n",
ent->d_name, age);
else
printf("%-24s since %s ago\n",
ent->d_name, age);
count++;
} else if (errno == ECONNREFUSED) {
printf("%-24s since %s ago [stale]\n", ent->d_name,
age);
count++;
}
}
closedir(d);
/* Second pass: show exited sessions (log files without a socket). */
if (show_all) {
d = opendir(dir);
if (d) {
while ((ent = readdir(d)) != NULL) {
struct stat lst;
char age[32];
size_t nlen = strlen(ent->d_name);
if (nlen <= 4 ||
strcmp(ent->d_name + nlen - 4, ".log") != 0)
continue;
/* Build socket path (name without .log). */
snprintf(path, sizeof(path), "%s/%.*s",
dir, (int)(nlen - 4), ent->d_name);
/* Skip if the socket still exists (already listed). */
if (access(path, F_OK) == 0)
continue;
/* Stat the log file for its mtime. */
snprintf(path, sizeof(path), "%s/%s",
dir, ent->d_name);
if (stat(path, &lst) < 0)
continue;
format_age(now > lst.st_mtime ?
now - lst.st_mtime : 0,
age, sizeof(age));
printf("%-24.*s since %s ago [exited]\n",
(int)(nlen - 4), ent->d_name, age);
count++;
}
closedir(d);
}
}
empty:
if (count == 0 && !quiet)
printf("(no sessions)\n");
return 0;
}