-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.c
More file actions
68 lines (62 loc) · 1.7 KB
/
worker.c
File metadata and controls
68 lines (62 loc) · 1.7 KB
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "utils.h"
int read_all(int sock, unsigned int size, void *buffer) {
unsigned int bytes_read = 0;
int result;
while (bytes_read < size) {
result = read(sock, buffer + bytes_read, size - bytes_read);
if (result < 1) {
return -1;
}
bytes_read += result;
}
return bytes_read;
}
int handler(int sock) {
int pid = getpid();
fprintf(stdout, "handler(%d): waiting data(%d)\n", pid, sock);
while (1) {
char buf[5];
int read_total = read_all(sock, 5, buf);
if (read_total < 0) {
close(sock);
break;
}
fprintf(stdout, "handler(%d): received data: %s\n", pid, buf);
}
return 0;
}
int main(int argc, char *argv[]) {
int pid = getpid();
fprintf(stdout, "worker(%d): starting\n", pid);
fprintf(stdout, "worker(%d): creating pipe\n", pid);
int pipe = create_pipe();
if (pipe < 0) {
fprintf(stderr, "worker(%d): %s", pid, strerror(errno));
exit(EXIT_FAILURE);
}
while (1) {
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
socklen_t addr_len = 0;
int conn_fd = accept(pipe, (struct sockaddr *)&addr, &addr_len);
int sock_fd = recv_fd(conn_fd);
if (sock_fd < 0) {
fprintf(stderr, "worker(%d): error reading socket descriptor\n", pid);
continue;
}
int pid = fork();
if (pid == 0) {
return handler(sock_fd);
}
close(conn_fd);
}
return 0;
}