-
Notifications
You must be signed in to change notification settings - Fork 15
/
pg_top.c
1025 lines (879 loc) · 23.7 KB
/
pg_top.c
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
char *copyright =
"Copyright (c) 1984 through 2007, William LeFebvre";
/*
* Top users/processes display for Unix
*
* This program may be freely redistributed,
* but this entire comment MUST remain intact.
*
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
* Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
* Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
* Copyright (c) 1996, William LeFebvre, Group sys Consulting
* Copyright (c) 2007-2019, Mark Wong
*/
/*
* See the file "HISTORY" for information on version-to-version changes.
*/
/*
* This file contains "main" and other high-level routines.
*/
/*
* The following preprocessor variables, when defined, are used to
* distinguish between different Unix implementations:
*
* FD_SET - macros FD_SET and FD_ZERO are used when defined
*/
#include "os.h"
#include <signal.h>
#include <setjmp.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
/* determine which type of signal functions to use */
#ifdef HAVE_SIGACTION
#undef HAVE_SIGHOLD
#else
#if !defined(HAVE_SIGHOLD) || !defined(HAVE_SIGRELSE)
#define BSD_SIGNALS
#endif
#endif
/* includes specific to top */
#include "pg_top.h"
#include "remote.h"
#include "commands.h"
#include "display.h" /* interface to display package */
#include "screen.h" /* interface to screen package */
#include "boolean.h"
#include "utils.h"
#include "version.h"
#ifdef ENABLE_COLOR
#include "color.h"
#endif
#include "port.h"
/* Size of the stdio buffer given to stdout */
#define Buffersize 2048
/* The buffer that stdio will use */
char stdoutbuf[Buffersize];
/* build signal masks */
#ifndef sigmask
#define sigmask(s) (1 << ((s) - 1))
#endif
/* for getopt: */
extern int optind;
extern char *optarg;
/* imported from screen.c */
extern int overstrike;
/* values which need to be accessed by signal handlers */
int max_topn; /* maximum displayable processes */
/* miscellaneous things */
char *myname = "pg_top";
jmp_buf jmp_int;
/* internal variables */
static const char *progname = "pg_top";
void process_commands(struct pg_top_context *);
static void usage(const char *progname);
/* List of all the options available */
static struct option long_options[] = {
{"batch", no_argument, NULL, 'b'},
{"show-command", no_argument, NULL, 'c'},
{"color-mode", no_argument, NULL, 'C'},
{"interactive", no_argument, NULL, 'i'},
{"hide-idle", no_argument, NULL, 'I'},
{"non-interactive", no_argument, NULL, 'n'},
{"order-field", required_argument, NULL, 'o'},
{"remote-mode", no_argument, NULL, 'r'},
{"set-delay", required_argument, NULL, 's'},
{"show-tags", no_argument, NULL, 'T'},
{"version", no_argument, NULL, 'V'},
{"set-display", required_argument, NULL, 'x'},
{"show-username", required_argument, NULL, 'z'},
{"help", no_argument, NULL, '?'},
{"dbname", required_argument, NULL, 'd'},
{"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"username", required_argument, NULL, 'U'},
{"password", no_argument, NULL, 'W'},
{NULL, 0, NULL, 0}
};
/* pointers to display routines */
void (*d_loadave) (int, double *) = i_loadave;
void (*d_minibar) (
int (*) (char *, int)) = i_minibar;
void (*d_uptime) (time_t *, time_t *) = i_uptime;
void (*d_procstates) (int, int *) = i_procstates;
void (*d_cpustates) (int64_t *) = i_cpustates;
void (*d_memory) (long *) = i_memory;
void (*d_swap) (long *) = i_swap;
void (*d_message) () = i_message;
void (*d_process) (int, char *) = i_process;
/*
* usage - print help message with details about commands
*/
static void
usage(const char *progname)
{
printf("%s monitors a PostgreSQL database cluster.\n\n", progname);
printf("Usage:\n");
printf(" %s [OPTION]... [COUNT]\n", progname);
printf("\nGeneral options:\n");
printf(" -b, --batch use batch mode\n");
printf(" -c, --show-command display command name of each process\n");
printf(" -C, --color-mode turn off color mode\n");
printf(" -i, --interactive use interactive mode\n");
printf(" -I, --hide-idle hide idle processes\n");
printf(" -n, --non-interactive use non-interactive mode\n");
printf(" -o, --order-field=FIELD select sort order\n");
printf(" -r, --remote-mode activate remote mode\n");
printf(" -R display replication stats\n");
printf(" -s, --set-delay=SECOND set delay between screen updates\n");
printf(" -T, --show-tags show color tags\n");
printf(" -V, --version output version information, then exit\n");
printf(" -x, --set-display=COUNT set maximum number of displays\n");
printf(" exit once this number is reached\n");
printf(" -X display i/o stats\n");
printf(" -z, --show-username=NAME display only processes owned by given\n");
printf(" username\n");
printf(" -?, --help show this help, then exit\n");
printf("\nConnection options:\n");
printf(" -d, --dbname=DBNAME database to connect to\n");
printf(" -h, --host=HOSTNAME database server host or socket directory\n");
printf(" -p, --port=PORT database server port\n");
printf(" -U, --username=USERNAME user name to connect as\n");
printf(" -W, --password force password prompt, and persistent connection\n");
}
RETSIGTYPE
onalrm(int i) /* SIGALRM handler */
{
/* this is only used in batch mode to break out of the pause() */
/* return; */
}
void
do_display(struct pg_top_context *pgtctx)
{
register int i = 0;
register int active_procs;
caddr_t processes;
time_t curr_time;
static struct ext_decl exts = {NULL, NULL};
/* get the current stats and processes */
if (pgtctx->mode_remote == 0)
{
get_system_info(&pgtctx->system_info);
processes = get_process_info(&pgtctx->system_info, &pgtctx->ps,
pgtctx->order_index, &pgtctx->conninfo,
pgtctx->mode);
}
else
{
get_system_info_r(&pgtctx->system_info, &pgtctx->conninfo);
processes = get_process_info_r(&pgtctx->system_info, &pgtctx->ps,
pgtctx->order_index, &pgtctx->conninfo, pgtctx->mode);
}
/* display the load averages */
(*d_loadave) (pgtctx->system_info.last_pid, pgtctx->system_info.load_avg);
/* this method of getting the time SHOULD be fairly portable */
time(&curr_time);
/* if we have a minibar extension, use it, otherwise show uptime */
if (exts.f_minibar != NULL)
{
(*d_minibar) (exts.f_minibar);
}
else
{
(*d_uptime) (&pgtctx->statics.boottime, &curr_time);
}
/* display the current time */
i_timeofday(&curr_time);
/* display process state breakdown */
(*d_procstates) (pgtctx->system_info.p_total, pgtctx->system_info.procstates);
/* display the cpu state percentage breakdown */
if (pgtctx->dostates) /* but not the first time */
{
(*d_cpustates) (pgtctx->system_info.cpustates);
}
else
{
/* we'll do it next time */
if (smart_terminal)
{
z_cpustates();
}
pgtctx->dostates = Yes;
}
/* display memory stats */
(*d_memory) (pgtctx->system_info.memory);
/* display swap stats */
(*d_swap) (pgtctx->system_info.swap);
/* handle message area */
(*d_message) ();
/* update the header area */
pgtctx->d_header(pgtctx->header_text);
if (pgtctx->topn > 0)
{
/* determine number of processes to actually display */
/*
* this number will be the smallest of: active processes, number user
* requested, number current screen accomodates
*/
active_procs = pgtctx->system_info.P_ACTIVE;
if (active_procs > pgtctx->topn)
{
active_procs = pgtctx->topn;
}
if (active_procs > max_topn)
{
active_procs = max_topn;
}
/* Now show the top "n" processes or other statistics. */
switch (pgtctx->mode)
{
#if defined(__linux__) || defined(__FreeBSD__)
case MODE_IO_STATS:
for (i = 0; i < active_procs; i++)
{
if (pgtctx->mode_remote == 0)
(*d_process) (i, format_next_io(processes));
else
(*d_process) (i, format_next_io_r(processes));
}
break;
#endif /* defined(__linux__) || defined(__FreeBSD__) */
case MODE_REPLICATION:
for (i = 0; i < active_procs; i++)
{
if (pgtctx->mode_remote == 0)
(*d_process) (i, format_next_replication(processes));
else
(*d_process) (i, format_next_replication_r(processes));
}
break;
case MODE_PROCESSES:
default:
for (i = 0; i < active_procs; i++)
{
if (pgtctx->mode_remote == 0)
(*d_process) (i, format_next_process(processes));
else
(*d_process) (i, format_next_process_r(processes));
}
}
}
else
{
i = 0;
}
/* do end-screen processing */
u_endscreen(i);
/* now, flush the output buffer */
if (fflush(stdout) != 0)
{
new_message(MT_standout, " Write error on stdout");
putchar('\r');
quit(1);
/* NOTREACHED */
}
/* only do the rest if we have more displays to show */
if (pgtctx->displays)
{
/* switch out for new display on smart terminals */
if (smart_terminal)
{
if (overstrike)
{
reset_display(pgtctx);
}
else
{
d_loadave = u_loadave;
d_minibar = u_minibar;
d_uptime = u_uptime;
d_procstates = u_procstates;
d_cpustates = u_cpustates;
d_memory = u_memory;
d_swap = u_swap;
d_message = u_message;
pgtctx->d_header = u_header;
d_process = u_process;
}
}
if (!pgtctx->interactive)
{
/* set up alarm */
(void) signal(SIGALRM, onalrm);
(void) alarm((unsigned) pgtctx->delay);
/* wait for the rest of it .... */
pause();
}
else
process_commands(pgtctx);
}
}
void
process_arguments(struct pg_top_context *pgtctx, int ac, char **av)
{
int i;
int option_index;
while ((i = getopt_long(ac, av, "CDITbcinRrVh:s:d:U:o:Wp:Xx:z:",
long_options, &option_index)) != EOF)
{
switch (i)
{
#ifdef ENABLE_COLOR
case 'C':
pgtctx->color_on = !pgtctx->color_on;
break;
#endif
case 'D':
debug_set(1);
break;
case 'V': /* show version number */
printf("pg_top %s\n", version_string());
exit(0);
break;
case 'z': /* display only username's processes */
strncpy(pgtctx->ps.usename, optarg, NAMEDATALEN);
break;
case 'I': /* show idle processes */
pgtctx->ps.idle = !pgtctx->ps.idle;
break;
case 'T': /* show color tags */
pgtctx->show_tags = 1;
break;
case 'i': /* go interactive regardless */
pgtctx->interactive = Yes;
break;
case 'c':
pgtctx->ps.fullcmd = No;
break;
case 'n': /* batch, or non-interactive */
case 'b':
pgtctx->interactive = No;
break;
case 'x': /* number of displays to show */
if ((i = atoiwi(optarg)) == Invalid || i == 0)
{
new_message(MT_standout | MT_delayed,
" Bad display count (ignored)");
}
else
{
pgtctx->displays = i;
}
break;
case 's':
if ((pgtctx->delay = atoi(optarg)) < 0 ||
(pgtctx->delay == 0 && getuid() != 0))
{
new_message(MT_standout | MT_delayed,
" Bad seconds delay (ignored)");
pgtctx->delay = Default_DELAY;
}
break;
case 'o': /* select sort order */
pgtctx->order_name = optarg;
break;
case 'p': /* database port */
if ((i = atoiwi(optarg)) == Invalid || i == 0)
{
new_message(MT_standout | MT_delayed,
" Bad port number (ignored)");
}
else
{
pgtctx->conninfo.values[PG_PORT] = strdup(optarg);
}
break;
case 'W': /* prompt for database password */
pgtctx->conninfo.persistent = 1;
pgtctx->conninfo.values[PG_PASSWORD] =
simple_prompt("Password: ", 1000, 0);
break;
case 'U': /* database user name */
pgtctx->conninfo.values[PG_USER] = strdup(optarg);
break;
case 'd': /* database name */
pgtctx->conninfo.values[PG_DBNAME] = strdup(optarg);
break;
case 'h': /* socket location */
pgtctx->conninfo.values[PG_HOST] = strdup(optarg);
break;
case 'R': /* replication mode */
pgtctx->mode = MODE_REPLICATION;
break;
case 'r': /* remote mode */
pgtctx->mode_remote = 1;
break;
case 'X': /* i/O mode */
pgtctx->mode = MODE_IO_STATS;
break;
default:
fprintf(stderr, "Try \"%s --help\" for more information.\n",
progname);
exit(1);
}
}
}
void
process_commands(struct pg_top_context *pgtctx)
{
int no_command;
fd_set readfds;
char ch;
do
{
no_command = No;
/* set up arguments for select with timeout */
FD_ZERO(&readfds);
FD_SET(0, &readfds); /* for standard input */
pgtctx->timeout.tv_sec = pgtctx->delay;
pgtctx->timeout.tv_usec = 0;
/* wait for either input or the end of the delay period */
if (select(32, &readfds, (fd_set *) NULL, (fd_set *) NULL,
&pgtctx->timeout) > 0)
{
/* something to read -- clear the message area first */
clear_message();
/* now read it and convert to command strchr */
/* (use "change" as a temporary to hold strchr) */
if (read(0, &ch, 1) != 1)
{
/* read error: either 0 or -1 */
new_message(MT_standout, " Read error on stdin");
putchar('\r');
quit(1);
/* NOTREACHED */
}
no_command = execute_command(pgtctx, ch);
/* flush out stuff that may have been written */
fflush(stdout);
}
} while (no_command);
}
/*
* reset_display() - reset all the display routine pointers so that entire
* screen will get redrawn.
*/
void
reset_display(struct pg_top_context *pgtctx)
{
d_loadave = i_loadave;
d_minibar = i_minibar;
d_uptime = i_uptime;
d_procstates = i_procstates;
d_cpustates = i_cpustates;
d_memory = i_memory;
d_swap = i_swap;
d_message = i_message;
pgtctx->d_header = i_header;
d_process = i_process;
}
/*
* signal handlers
*/
void
set_signal(int sig, RETSIGTYPE(*handler) (int))
{
#ifdef HAVE_SIGACTION
struct sigaction action;
action.sa_handler = handler;
action.sa_flags = 0;
(void) sigaction(sig, &action, NULL);
#else
(void) signal(sig, handler);
#endif
}
RETSIGTYPE
leave(int i) /* exit under normal conditions -- INT handler */
{
end_screen();
exit(0);
}
RETSIGTYPE
tstop(int i) /* SIGTSTP handler */
{
#ifdef HAVE_SIGACTION
sigset_t set;
#endif
/* move to the lower left */
end_screen();
fflush(stdout);
/* default the signal handler action */
set_signal(SIGTSTP, SIG_DFL);
/* unblock the TSTP signal */
#ifdef HAVE_SIGACTION
sigemptyset(&set);
sigaddset(&set, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &set, NULL);
#endif
#ifdef HAVE_SIGHOLD
sigrelse(SIGTSTP);
#endif
#ifdef BSD_SIGNALS
(void) sigsetmask(sigblock(0) & ~(sigmask(SIGTSTP)));
#endif
/* send ourselves a TSTP to stop the process */
(void) kill(0, SIGTSTP);
/* reset the signal handler */
set_signal(SIGTSTP, tstop);
/* reinit screen */
reinit_screen();
/* jump to appropriate place */
longjmp(jmp_int, 1);
/* NOTREACHED */
}
#ifdef SIGWINCH
RETSIGTYPE
winch(int i) /* SIGWINCH handler */
{
/* reascertain the screen dimensions */
get_screensize();
/* tell display to resize */
max_topn = display_resize();
#ifndef HAVE_SIGACTION
/* reset the signal handler */
set_signal(SIGWINCH, winch);
#endif
/* jump to appropriate place */
longjmp(jmp_int, 1);
}
#endif
void
quit(int status) /* exit under duress */
{
end_screen();
exit(status);
/* NOTREACHED */
}
int
main(int argc, char *argv[])
{
register int i;
struct pg_top_context pgtctx;
#ifdef BSD_SIGNALS
int old_sigmask; /* only used for BSD-style signals */
#endif /* BSD_SIGNALS */
char *uname_field = "USERNAME";
char *env_top;
char **preset_argv;
int preset_argc = 0;
char **av;
int ac;
#ifndef FD_SET
/* FD_SET and friends are not present: fake it */
typedef int fd_set;
#define FD_ZERO(x) (*(x) = 0)
#define FD_SET(f, x) (*(x) = 1<<f)
#endif
#ifdef HAVE_SIGPROCMASK
sigset_t signalset;
#endif
/* initialize some selection options */
memset(&pgtctx, 0, sizeof(struct pg_top_context));
#ifdef ENABLE_COLOR
pgtctx.color_on = 1;
#endif
pgtctx.d_header = i_header;
pgtctx.delay = Default_DELAY;
pgtctx.displays = 0; /* indicates unspecified */
pgtctx.dostates = No;
pgtctx.do_unames = Yes;
pgtctx.interactive = Maybe;
pgtctx.mode = MODE_PROCESSES;
pgtctx.mode_remote = No;
pgtctx.order_index = -1;
pgtctx.ps.idle = Yes;
pgtctx.ps.fullcmd = Yes;
pgtctx.ps.command = NULL;
pgtctx.ps.usename[0] = '\0';
pgtctx.show_tags = No;
pgtctx.topn = 0;
pgtctx.conninfo.connection = NULL;
pgtctx.conninfo.persistent = 0;
/* Show help or version number if necessary */
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
printf("pg_top %s\n", version_string());
exit(0);
}
}
/* set the buffer for stdout */
#ifdef HAVE_SETVBUF
setvbuf(stdout, stdoutbuf, _IOFBF, BUFFERSIZE);
#else
#ifdef HAVE_SETBUFFER
setbuffer(stdout, stdoutbuf, Buffersize);
#endif /* HAVE_SETBUFFER */
#endif /* HAVE_SETVBUF */
/* get our name */
if (argc > 0)
{
if ((myname = strrchr(argv[0], '/')) == 0)
{
myname = argv[0];
}
else
{
myname++;
}
}
/* get preset options from the environment */
if ((env_top = getenv("PG_TOP")) != NULL)
{
av = preset_argv = argparse(env_top, &preset_argc);
ac = preset_argc;
/*
* set the dummy argument to an explanatory message, in case getopt
* encounters a bad argument
*/
preset_argv[0] = "while processing environment";
}
/* process options */
do
{
/* if we're done doing the presets, then process the real arguments */
if (preset_argc == 0)
{
ac = argc;
av = argv;
/* this should keep getopt happy... */
optind = 1;
}
process_arguments(&pgtctx, ac, av);
/* get count of top processes to display (if any) */
if (optind < ac && *av[optind])
{
if ((i = atoiwi(av[optind])) == Invalid)
{
new_message(MT_standout | MT_delayed,
" Process count not a number (ignored)");
}
else
{
pgtctx.displays = i;
}
}
/* tricky: remember old value of preset_argc & set preset_argc = 0 */
i = preset_argc;
preset_argc = 0;
/* repeat only if we really did the preset arguments */
} while (i != 0);
/* set constants for username/uid display correctly */
if (!pgtctx.do_unames)
{
uname_field = " UID ";
}
/*
* in order to support forward compatability, we have to ensure that the
* entire statics structure is set to a known value before we call
* machine_init. This way fields that a module does not know about will
* retain their default values
*/
memzero((void *) &pgtctx.statics, sizeof(pgtctx.statics));
pgtctx.statics.boottime = -1;
#ifdef ENABLE_COLOR
/* If colour has been turned on read in the settings. */
env_top = getenv("PG_TOPCOLOURS");
if (!env_top)
{
env_top = getenv("PG_TOPCOLORS");
}
/* must do something about error messages */
color_env_parse(env_top);
#endif
/* call the platform-specific init */
if (pgtctx.mode_remote == 0)
i = machine_init(&pgtctx.statics);
else
i = machine_init_r(&pgtctx.statics, &pgtctx.conninfo);
if (i == -1)
exit(1);
/* determine sorting order index, if necessary */
if (pgtctx.order_name != NULL)
{
if (pgtctx.statics.order_names == NULL)
{
new_message(MT_standout | MT_delayed,
" This platform does not support arbitrary ordering");
}
else if ((pgtctx.order_index = string_index(pgtctx.order_name,
pgtctx.statics.order_names)) == -1)
{
char **pp;
fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n",
myname, pgtctx.order_name);
fprintf(stderr, "\tTry one of these:");
pp = pgtctx.statics.order_names;
while (*pp != NULL)
{
fprintf(stderr, " %s", *pp++);
}
fputc('\n', stderr);
exit(1);
}
}
#ifdef WITH_EXT
/* initialize extensions */
init_ext(&exts);
#endif
/* initialize termcap */
init_termcap(pgtctx.interactive);
/* 0 corresponds to machine headers definitions */
pgtctx.header_options[0][MODE_PROCESSES] = format_header(uname_field);
#if defined(__linux__) || defined(__FreeBSD__)
pgtctx.header_options[0][MODE_IO_STATS] = fmt_header_io;
#endif /* defined(__linux__) || defined(__FreeBSD__) */
pgtctx.header_options[0][MODE_REPLICATION] = fmt_header_replication;
/* 1 corresponds to headers definitions when remotely connecting to pg */
pgtctx.header_options[1][MODE_PROCESSES] = format_header_r(uname_field);
pgtctx.header_options[1][MODE_IO_STATS] = fmt_header_io_r;
pgtctx.header_options[1][MODE_REPLICATION] = fmt_header_replication_r;
/* get the string to use for the process area header */
pgtctx.header_text =
pgtctx.header_options[pgtctx.mode_remote][pgtctx.mode];
#ifdef ENABLE_COLOR
/* Disable colours on non-smart terminals */
if (!smart_terminal)
{
pgtctx.color_on = 0;
}
#endif
/* initialize display interface */
if ((max_topn = display_init(&pgtctx.statics)) == -1)
{
fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
exit(4);
}
/* handle request for color tags */
if (pgtctx.show_tags)
{
color_dump(stdout);
exit(0);
}
/*
* Set topn based on the current screensize when starting up if it was not
* specified on the command line.
*/
if (pgtctx.topn == 0)
{
get_screensize();
pgtctx.topn = display_resize();
}
/* print warning if user requested more processes than we can display */
if (pgtctx.topn > max_topn)
{
new_message(MT_standout | MT_delayed,
" This terminal can only display %d processes.",
max_topn);
}
/* set header display accordingly */
display_header(pgtctx.topn > 0);
/* determine interactive state */
if (pgtctx.interactive == Maybe)
{
pgtctx.interactive = smart_terminal;
}
/* if # of displays not specified, fill it in */
if (pgtctx.displays == 0)
{
pgtctx.displays = smart_terminal ? Infinity : 1;
}
/* hold interrupt signals while setting up the screen and the handlers */
#ifdef HAVE_SIGPROCMASK
sigemptyset(&signalset);
sigaddset(&signalset, SIGINT);
sigaddset(&signalset, SIGQUIT);
sigaddset(&signalset, SIGTSTP);
#ifdef SIGWINCH
sigaddset(&signalset, SIGWINCH);
#endif
sigprocmask(SIG_BLOCK, &signalset, NULL);
#endif
#ifdef HAVE_SIGHOLD
sighold(SIGINT);
sighold(SIGQUIT);
sighold(SIGTSTP);
#ifdef SIGWINCH
sighold(SIGWINCH);
#endif
#endif
#ifdef BSD_SIGNALS
#ifdef SIGWINCH
old_sigmask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) |
sigmask(SIGTSTP) | sigmask(SIGWINCH));
#else
old_sigmask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) |
sigmask(SIGTSTP));
#endif
#endif
init_screen();
(void) set_signal(SIGINT, leave);
(void) set_signal(SIGQUIT, leave);
(void) set_signal(SIGTSTP, tstop);
#ifdef SIGWINCH
(void) set_signal(SIGWINCH, winch);
#endif
/* setup the jump buffer for stops */
if (setjmp(jmp_int) != 0)
{
/* control ends up here after an interrupt */
reset_display(&pgtctx);
}
/*
* Ready to release the signals. This will also happen (needlessly) after
* a longjmp, but that's okay.
*/
#ifdef HAVE_SIGPROCMASK
sigprocmask(SIG_UNBLOCK, &signalset, NULL);
#endif
#ifdef HAVE_SIGHOLD
sigrelse(SIGINT);
sigrelse(SIGQUIT);
sigrelse(SIGTSTP);
#ifdef SIGWINCH
sigrelse(SIGWINCH);
#endif
#endif
#ifdef BSD_SIGNALS
(void) sigsetmask(old_sigmask);
#endif
/* some systems require a warmup */
if (pgtctx.statics.flags.warmup)
{
if (pgtctx.mode_remote == 0)
{
get_system_info(&pgtctx.system_info);
(void) get_process_info(&pgtctx.system_info, &pgtctx.ps, -1,
&pgtctx.conninfo, pgtctx.mode);
}
else
{
get_system_info_r(&pgtctx.system_info, &pgtctx.conninfo);
(void) get_process_info_r(&pgtctx.system_info, &pgtctx.ps, -1,