-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneonate.c
239 lines (208 loc) · 6.34 KB
/
neonate.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
#include "headers.h"
#include "common.h"
void die(const char *s)
{
printf(ERROR_COLOR);
perror(s);
printf(DEFAULT_COLOR);
exit(1);
}
struct termios orig_termios;
void disableRawMode()
{
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
}
/**
* Enable row mode for the terminal
* The ECHO feature causes each key you type to be printed to the terminal, so you can see what you’re typing.
* Terminal attributes can be read into a termios struct by tcgetattr().
* After modifying them, you can then apply them to the terminal using tcsetattr().
* The TCSAFLUSH argument specifies when to apply the change: in this case, it waits for all pending output to be written to the terminal, and also discards any input that hasn’t been read.
* The c_lflag field is for “local flags”
*/
void enableRawMode()
{
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
die("tcgetattr");
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
die("tcsetattr");
}
/**
* stdout and stdin are buffered we disable buffering on that
* After entering in raw mode we read characters one by one
* Up arrow keys and down arrow keys are represented by 3 byte escape codes
* starting with ascii number 27 i.e. ESC key
* This way we interpret arrow keys
* Tabs are usually handled by the term, but here we are simulating tabs for the sake of simplicity
* Backspace move the cursor one control character to the left
* @return
*/
int pidverify(const char *name)
{
int l = strlen(name);
for (int i = 0; i < l; i++)
{
if (!isdigit(name[i]))
return 0;
}
return 1;
}
int kbhit()
{
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
void neonate(char *subcom)
{
// printf("%s\n", subcom);
char *token = strtok(subcom, " \t\n");
token = strtok(NULL, " \t\n");
if (token == NULL)
{
printf(ERROR_COLOR "Too less arguments for neonate\n" DEFAULT_COLOR);
return;
}
// printf("Flag is %s\n", token);
if (strcmp(token, "-n") != 0)
{
printf(ERROR_COLOR "Invalid flag for neonate\n" DEFAULT_COLOR);
return;
}
token = strtok(NULL, " \t\n");
if (token == NULL)
{
printf(ERROR_COLOR "Too less arguments for neonate\n" DEFAULT_COLOR);
return;
}
int timearg = atoi(token);
// printf("%s seconds\n", token);
token = strtok(NULL, " \t\n");
if (token != NULL)
{
printf(ERROR_COLOR "Too many arguments for neonate\n" DEFAULT_COLOR);
return;
}
if (timearg < 0)
{
printf(ERROR_COLOR "Invalid time argument for neonate\n" DEFAULT_COLOR);
return;
}
// printf("Time argument is %d\n", timearg);
enableRawMode();
// temporarily block signals
sigset_t mask, oldmask;
sigemptyset(&mask);
sigaddset(&mask, SIGTSTP);
sigaddset(&mask, SIGINT);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
while (1)
{
int maxtime = 0;
int maxpid = -2;
DIR *dir;
struct dirent *entry;
// Open the /proc directory
dir = opendir("/proc");
if (dir == NULL)
{
// perror("opendir");
printf(ERROR_COLOR "Failed to open /proc directory\n" DEFAULT_COLOR);
return;
}
// Iterate through the directory entries
while ((entry = readdir(dir)) != NULL)
{
if (pidverify(entry->d_name))
{
// printf("PID folder: %s\n", entry->d_name);
char fname[4096];
sprintf(fname, "/proc/%s/stat", entry->d_name);
FILE *statfile = fopen(fname, "r");
if (statfile == NULL)
{
// printf(ERROR_COLOR "Failed to open stat file for PID %s\n" DEFAULT_COLOR, entry->d_name);
// exit(EXIT_FAILURE);
continue;
}
char *line = NULL;
size_t len = 0;
ssize_t read;
if ((read = getline(&line, &len, statfile)) != -1)
{
char *token = strtok(line, " ");
int count = 0;
unsigned long long frtime;
while (token != NULL)
{
if (count == 21) // man proc for details
{
frtime = strtoul(token, NULL, 10);
break;
}
count++;
token = strtok(NULL, " ");
}
if (frtime > maxtime)
{
maxtime = frtime;
maxpid = atoi(entry->d_name);
}
}
// else
// printf(ERROR_COLOR "Failed to read stat file for PID %s\n" DEFAULT_COLOR, entry->d_name);
free(line);
fclose(statfile);
}
}
closedir(dir);
// printf("Most recent process has pid %d with starting time %d\n", maxpid, maxtime);
printf("%d\n", maxpid);
fflush(stdout);
if (timearg == 0)
{
char key;
if (kbhit())
{
key = getchar();
if (key == 'x')
{
// printf("Exiting neonate\n");
break;
}
}
}
else
{
long long int i;
int flag = 0;
for (i = 0; i < timearg * 1000; i++)
{
// check for key hit
char key;
if (kbhit())
{
key = getchar();
if (key == 'x')
{
// printf("Exiting neonate\n");
flag = 1;
break;
}
}
usleep(1000);
}
if (flag == 1)
break;
}
}
// unblock signals
sigprocmask(SIG_SETMASK, &oldmask, NULL);
disableRawMode();
}