-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_server.c
318 lines (281 loc) · 7.09 KB
/
ps_server.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>
#include <fcntl.h>
#include <dirent.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <pthread.h>
#include "sort.h"
// Simple struct that packs arguments to pass to thread
struct package {
pthread_t *thread;
int client_sock;
};
pthread_mutex_t file_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
struct link_node *head;
// NONE OF THESE NEED TO BE THREAD-SAFE SINCE THEY ARE
// ASSIGNED TO ONCE BY THE MAIN THREAD THEN READ THEREAFTER
int sig_flag;
char *filename;
int server_sock;
// Returns UID for user; -1 if failed
long get_uid(char *username)
{
struct passwd *user = getpwnam(username);
if (user == NULL)
return -1;
long uid = (long)user->pw_uid;
return uid;
}
// Checks if string is only numbers
int num_checker(char *dir_name)
{
int i;
for (i = 0; i < strnlen(dir_name, PATH_MAX); i++) {
if (!(dir_name[i] >= '0' && dir_name[i] <= '9'))
return 1;
}
return 0;
}
// Compare the given UID to the one in the status file of 'path';
// If its the same, return the associated process name
char *proc_checker(char *path, char *username)
{
FILE *fp;
int user_UID = get_uid(username);
int stored_UID;
char *line = NULL;
size_t len = 0;
char *proc_name = malloc(sizeof(char) * PATH_MAX);
if (user_UID == -1)
return NULL;
fp = fopen(path, "r");
if (fp != NULL) {
while (getline(&line, &len, fp) != -1) {
if (sscanf(line, "Name: %s", proc_name) == 1);
if (sscanf(line, "Uid: %d", &stored_UID) == 1);
}
fclose(fp);
free(line);
if (stored_UID == user_UID)
return proc_name;
free(proc_name);
return NULL;
}
free(proc_name);
return NULL;
}
// Runs through /proc folder, checking
// processes that match the given username.
// Returns root of tree created for user.
struct node *proc_skimmer(char *username, struct node *root)
{
DIR *stream;
struct dirent *dp;
char *path = malloc(sizeof(char) * PATH_MAX);
char *result;
stream = opendir("/proc");
if (stream == NULL) {
perror("Cannot open /proc");
exit(1);
}
// Now reading through /proc
while ((dp = readdir(stream)) != NULL) {
memset(path, 0, PATH_MAX);
if (dp->d_type == DT_DIR && num_checker(dp->d_name) == 0) {
// A process was found, extract info
strncpy(path, "/proc/", 6);
strncat(path, dp->d_name, PATH_MAX);
strncat(path, "/status", 7);
result = proc_checker(path, username);
if (result) {
root = insert(root, result);
free(result);
}
}
}
free(path);
closedir(stream);
return root;
}
// Take the username and run the proc_skimmer
// to find which are associated to the user;
// send results back over through the socket
void server_reply(char *username, int client_sock)
{
char *buf = malloc(sizeof(char) * PATH_MAX);
int count = 0;
struct node *root = NULL;
// Create tree full of procs and counts
root = proc_skimmer(username, root);
// Traverse it in-order, sending name followed by count to client
traverse_client(root, client_sock, buf);
free(buf);
count = total_count(root, count);
//Before destroying the tree, do logging
if (filename == NULL) {
printf("USER %s [%d processes]\n", username, count);
} else {
pthread_mutex_lock(&file_mutex);
FILE *fp = fopen(filename, "a");
fprintf(fp, "USER %s [%d processes]\n", username, count);
fclose(fp);
pthread_mutex_unlock(&file_mutex);
}
// Destroy tree to free memory (and reset global_count)
destroy(root);
}
// What the server does after SIGINT has been received and the
// flag has been flipped by the handle() function
void handle_action(void)
{
// Clean up remaining threads
join_list(&head);
printf("\nServer successfully stopped.\n");
close(server_sock);
}
// Loop on this function within the server code to continue
// accepting connections
void *server_accept(void *package)
{
// Unpack the struct
struct package local_pkg = *((struct package *)package);
int client_sock = local_pkg.client_sock;
pthread_t *thread = local_pkg.thread;
int bytes_rec;
char *buf = malloc(sizeof(char) * PATH_MAX);
memset(buf, 0, PATH_MAX);
bytes_rec = recv(client_sock, buf, PATH_MAX, 0);
strtok(buf, "\n");
if (bytes_rec == -1) {
perror("\nRECV ERROR");
close(client_sock);
}
server_reply(buf, client_sock);
free(buf);
close(client_sock);
free(package);
pthread_mutex_lock(&list_mutex);
head = insert_link(head, thread);
pthread_mutex_unlock(&list_mutex);
return NULL;
}
// Handler for SIGINT; simply flip a global flag to
// indicate that the signal was received
void handle(int sig)
{
sig_flag = 1;
}
// Primary server code; sets up the server and loops the
// server_accept() function until SIGINT is received
void server(char *sock_path)
{
int client_sock, rc;
socklen_t len;
char *header = NULL;
int backlog = 100;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
header = log_header();
if (filename == NULL) {
printf("%s\n", header);
} else {
FILE *fp = fopen(filename, "a");
fprintf(fp, "%s\n", header);
fclose(fp);
}
free(header);
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1) {
perror("\nSOCKET ERROR");
exit(1);
}
server_sockaddr.sun_family = AF_UNIX;
strncpy(server_sockaddr.sun_path, sock_path,
strnlen(sock_path, PATH_MAX));
len = sizeof(server_sockaddr);
unlink(sock_path);
rc = bind(server_sock, (struct sockaddr *)&server_sockaddr, len);
if (rc == -1) {
perror("\nBIND ERROR");
close(server_sock);
exit(1);
}
rc = listen(server_sock, backlog);
if (rc == -1) {
perror("\nLISTEN ERROR");
close(server_sock);
exit(1);
}
while (sig_flag == 0) {
client_sock = accept(server_sock, (struct sockaddr *)
&client_sockaddr, &len);
if (client_sock == -1) {
;
} else {
struct package *thread_pack =
malloc(sizeof(*thread_pack));
pthread_t *thread = malloc(sizeof(pthread_t));
thread_pack->thread = thread;
thread_pack->client_sock = client_sock;
pthread_create(thread, 0, server_accept,
(void *)(thread_pack));
}
pthread_mutex_lock(&list_mutex);
join_list(&head);
pthread_mutex_unlock(&list_mutex);
}
// SIGINT was received
handle_action();
}
int main(int argc, char *argv[])
{
int opt;
struct sigaction sa;
char *usage = malloc(PATH_MAX);
strncpy(usage, "\nUsage: ps_server socketname [-v filename]\n", 45);
sa.sa_handler = handle;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("\nSIGNAL ERROR");
exit(1);
}
if (argc == 4) {
while ((opt = getopt(argc, argv, "v:")) != -1) {
switch (opt) {
case 'v':
if (argv[1] == NULL) {
printf("%s", usage);
exit(1);
}
filename = optarg;
server(argv[1]);
break;
case '?':
printf("\nInvalid argument.\n");
exit(1);
}
}
} else if (argc == 2) {
server(argv[1]);
} else {
printf("%s", usage);
}
free(usage);
return 0;
}