-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop.c
66 lines (59 loc) · 1.83 KB
/
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
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "server.h"
#include "html.h"
/* HTML source for the end of the page we generate. */
static char* page_end =
" </pre>\n"
"<div class=\"col-md-2 col-md-offset-3\">"
"<a href=\"/time\" class=\"btn btn-default btn-block\" role=\"button\">Previous</a>"
"</div>"
"<div class=\"col-md-2\">"
"<a href=\"/\" class=\"btn btn-default btn-block\" role=\"button\">Home</a>"
"</div>"
"<div class=\"col-md-2\">"
"<a href=\"#\" class=\"btn btn-default btn-block disabled\" role=\"button\">Next</a>"
"</div>"
" </body>\n"
"</html>\n";
void module_generate (int fd)
{
pid_t child_pid;
int rval;
/* Generate and write the start of the page. */
char* page_start = generate_head("Top Info", 6, 1);
write (fd, page_start, strlen (page_start));
/* Fork a child process. */
child_pid = fork ();
if (child_pid == 0) {
/* This is the child process. */
/* Set up an argument list for the invocation of df. */
char* argv[] = { "/usr/bin/top", "-bn1", NULL }; // <----- adapt here?
/* Duplicate stdout and stderr to send data to the client socket. */
rval = dup2 (fd, STDOUT_FILENO);
if (rval == -1)
system_error ("dup2");
rval = dup2 (fd, STDERR_FILENO);
if (rval == -1)
system_error ("dup2");
/* Run df to show the free space on mounted files systems. */
execv (argv[0], argv);
/* A call to execv does not return unless an error occurred. */
system_error ("execv");
}
else if (child_pid > 0) {
/* This is the parent process. Wait for the child process to
* finish. */
rval = waitpid (child_pid, NULL, 0);
if (rval == -1)
system_error ("waitpid");
}
else
/* The call to fork failed. */
system_error ("fork");
/* Write the end of the page. */
write (fd, page_end, strlen (page_end));
}