Debian Codegraph @

examples/fork-test/srt_server.c

raw
1 #include <srt/srt.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <pthread.h>
9 #define PORT 9000
10
11 int run(char *command) {
12 pid_t pid = fork();
13 if (pid < 0) {
14 perror("fork (intermediate)");
15 exit(EXIT_FAILURE);
16 }
17 if (pid > 0) {
18 // Parent process
19 printf("[GRANDPARENT %d] waiting for grand-child process pid=%d to finish...\n",
20 getpid(), pid);
21 waitpid(pid, NULL, 0); // Wait for intermediate child
22 printf("[GRANDPARENT] returning\n");
23 return 0;
24 }
25 // Intermediate process
26 //srt_cleanup();
27 if (setsid() < 0) {
28 perror("setsid");
29 exit(EXIT_FAILURE);
30 }
31 pid_t grandchild_pid = fork();
32 if (grandchild_pid < 0) {
33 perror("fork (grandchild)");
34 exit(EXIT_FAILURE);
35 }
36 if (grandchild_pid > 0) {
37 printf("[PARENT %d] waiting for 10s with child process pid=%d ...\n",
38 getpid(), grandchild_pid);
39 // Intermediate process exits immediately
40 sleep(10);
41 printf("[PARENT] exiting\n");
42 exit(0);
43 }
44 // Grandchild process
45 // Redirect stdin to /dev/null
46 printf("[CHILD %d] Preparing descriptors...\n", getpid());
47 int devnull = open("/dev/null", O_RDONLY);
48 if (devnull >= 0) {
49 dup2(devnull, STDIN_FILENO);
50 close(devnull);
51 } else {
52 perror("open /dev/null");
53 }
54 // Redirect stdout to stderr
55 dup2(STDERR_FILENO, STDOUT_FILENO);
56 // Execute the command
57 printf("[CHILD] Executing process '%s'...\n", command);
58 execl("/bin/sh", "sh", "-c", command, (char *)NULL);
59 // If execl fails
60 perror("execl");
61 exit(EXIT_FAILURE);
62 }
63
64 int main() {
65 if (srt_startup() != 0) {
66 fprintf(stderr, "Error initializing SRT.\n");
67 return 1;
68 }
69
70 SRTSOCKET serv_sock = srt_create_socket();
71 if (serv_sock == SRT_INVALID_SOCK) {
72 fprintf(stderr, "Error creating SRT socket: %s\n", srt_getlasterror_str());
73 return 1;
74 }
75 struct sockaddr_in sa;
76 memset(&sa, 0, sizeof sa);
77 sa.sin_family = AF_INET;
78 sa.sin_port = htons(PORT);
79 sa.sin_addr.s_addr = INADDR_ANY;
80 if (srt_bind(serv_sock, (struct sockaddr*)&sa, sizeof sa) == SRT_ERROR) {
81 fprintf(stderr, "Error: srt_bind: %s\n", srt_getlasterror_str());
82 return 1;
83 }
84 if (srt_listen(serv_sock, 5) == SRT_ERROR) {
85 fprintf(stderr, "Error: srt_listen: %s\n", srt_getlasterror_str());
86 return 1;
87 }
88 printf("SRT server is listening on port %d...\n", PORT);
89 struct sockaddr_in client_addr;
90 int addr_len = sizeof(client_addr);
91 SRTSOCKET client_sock = srt_accept(serv_sock, (struct sockaddr*)&client_addr, &addr_len);
92 if (client_sock == SRT_INVALID_SOCK) {
93 fprintf(stderr, "Error: srt_accept: %s\n", srt_getlasterror_str());
94 return 1;
95 }
96 printf("Client connected via SRT !\n");
97 char buffer[1500];
98 int bytes = srt_recv(client_sock, buffer, sizeof(buffer));
99 if (bytes > 0) {
100 buffer[bytes] = '\0';
101 printf("Message received: %s\n", buffer);
102 const char resp [] = "We read you!";
103 srt_send(client_sock, resp, (sizeof resp)-1);
104 } else {
105 printf("Error: reading from srt_recv: %s.\n", srt_getlasterror_str());
106 }
107 run("date > /tmp/res");
108 printf("Server: sleep(1)\n");
109 sleep(1);
110 // Nettoyage
111 printf("Server: closing SRT sockets\n");
112 srt_close(client_sock);
113 srt_close(serv_sock);
114 printf("Server: cleanup\n");
115 srt_cleanup();
116 printf("Server: exit\n");
117 return 0;
118 }