-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
230 lines (185 loc) · 5.05 KB
/
main.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
/*
* Copyright (c) 2008, 2009, 2019 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <curses.h>
#include <fcntl.h>
#include <inttypes.h>
#include <mach/clock_types.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>
#include "libtop.h"
#include "log.h"
#include "logging.h"
#include "options.h"
#include "preferences.h"
#include "sig.h"
#include "top.h"
#include "userinput.h"
const libtop_tsamp_t *tsamp;
static volatile sig_atomic_t resized = 1;
static int cached_lines = 0, cached_columns = 0;
static void init(void);
enum { MICROSECONDS = 1000000 };
static void
event_loop(void *tinst)
{
sigset_t sset, oldsset;
int samples;
struct timeval tlimit;
if (sigemptyset(&sset)) {
perror("sigemptyset");
exit(EXIT_FAILURE);
}
if (sigaddset(&sset, SIGWINCH)) {
perror("sigaddset");
exit(EXIT_FAILURE);
}
uint64_t beforens = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
while (1) {
bool sleep_expired = false;
fd_set fset;
int ready;
FD_ZERO(&fset);
FD_SET(STDIN_FILENO, &fset);
tlimit.tv_sec = top_prefs_get_sleep();
tlimit.tv_usec = 0;
ready = select(STDIN_FILENO + 1, &fset, NULL, NULL, &tlimit);
uint64_t nowns = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
if (nowns >= beforens + (uint64_t)tlimit.tv_sec * NSEC_PER_SEC) {
/*
* The sleep has expired, so we should insert new
* data for all stats. This is different than just
* the case where we handle user input and the rest
* of the data is awaiting a sleep interval update.
*/
sleep_expired = true;
beforens = nowns;
}
if (sleep_expired) {
samples = top_prefs_get_samples();
if (samples > -1) {
/* Samples was set in the preferences. */
if (0 == samples) {
/* We had N samples and now it's time to exit. */
endwin();
exit(EXIT_SUCCESS);
}
top_prefs_set_samples(samples - 1);
}
}
if (top_signal_is_exit_set()) {
exit(EXIT_SUCCESS);
}
if (ready && FD_ISSET(STDIN_FILENO, &fset))
(void)user_input_process(tinst);
if (sleep_expired)
top_insert(tinst);
/* Block SIGWINCH signals while we are in a relayout. */
if (-1 == sigprocmask(SIG_BLOCK, &sset, &oldsset)) {
perror("sigprocmask");
exit(EXIT_FAILURE);
}
if (top_need_relayout() || resized || LINES != cached_lines || COLS != cached_columns) {
cached_lines = LINES;
cached_columns = COLS;
if (top_layout(tinst)) {
resized = 1;
} else {
resized = 0;
}
}
if (-1 == sigprocmask(SIG_SETMASK, &oldsset, NULL)) {
perror("sigprocmask");
exit(EXIT_FAILURE);
}
top_draw(tinst);
}
}
void
exit_handler(void)
{
endwin();
}
void
init(void)
{
if (NULL == initscr()) {
fprintf(stderr, "error: unable to initscr!\n");
exit(EXIT_FAILURE);
}
atexit(exit_handler);
if (ERR == cbreak() /* disable line buffering */
|| ERR == noecho() /* disable echoing what the user types */
|| ERR == nonl() /* no newline */
|| ERR == intrflush(stdscr, FALSE) || ERR == meta(stdscr, TRUE)
|| ERR == keypad(stdscr, TRUE)) {
fprintf(stderr, "error: initializing curses\n");
exit(EXIT_FAILURE);
}
timeout(0);
}
int
main(int argc, char *argv[])
{
void *tinst;
top_prefs_init();
top_options_init();
if (top_options_parse(argc, argv)) {
top_options_usage(stderr, argv[0]);
return EXIT_FAILURE;
}
/* 18007048: If output isn't a tty and -l isn't specified, imply -l 0. */
if ((!isatty(STDOUT_FILENO)) && (top_prefs_get_samples() < 0)) {
top_prefs_set_samples(0);
}
if (top_prefs_get_samples() > -1)
top_prefs_set_logging_mode(true);
if (!top_prefs_get_logging_mode())
init();
top_signal_init();
if (libtop_init(NULL, NULL)) {
endwin();
fprintf(stderr, "libtop_init failed!\n");
return EXIT_FAILURE;
}
if (top_prefs_get_frameworks() && libtop_set_interval(top_prefs_get_frameworks_interval())) {
endwin();
fprintf(stderr, "error: setting framework update interval.\n");
exit(EXIT_FAILURE);
}
tinst = top_create(stdscr);
if (!top_prefs_get_logging_mode()) {
top_insert(tinst);
top_layout(tinst);
top_draw(tinst);
event_loop(tinst);
} else {
top_logging_loop(tinst);
}
return EXIT_SUCCESS;
}