Replace SIGQUIT with SIGTERM.
[wsti_so.git] / src / process1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* open/read/write/close */
6 #include <fcntl.h>
7
8 /* Signals handling.. */
9 #include <signal.h>
10
11 /** If buffer is too small to hold entire string, it is incremented by this value */
12 #define BUFFER_STEP 16
13
14 /** Buffer used to store line of characters */
15 char * buffer = NULL;
16
17 /** Named pipe used to communnicate with process2 */
18 char * write_pipe = "/tmp/process1pipe";
19
20 /** File descriptor of pipe */
21 int file_descriptor;
22
23 /**
24  * Handler for signals.
25  */
26 void sig_handler(int signo)
27 {
28         if (signo == SIGUSR1) {
29                 fprintf(stderr, "[%s] Received SIGUSR1!\n", "process1");
30         }
31         else if (signo == SIGTERM) {
32                 fprintf(stderr, "[%s] Received SIGTERM!\n", "process1");
33                 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
34                 close(write_pipe);
35                 unlink(write_pipe);
36
37                 if (buffer) {
38                         free(buffer);
39                         buffer = NULL;
40                 }
41                 exit(0);
42         }
43         else if (signo == SIGINT) {
44                 fprintf(stderr, "[%s] Received SIGINT!\n", "process1");
45                 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
46                 close(write_pipe);
47         }
48         else if (signo == SIGCONT) {
49                 fprintf(stderr, "[%s] Received SIGCONT!\n", "process1");
50                 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
51                 file_descriptor = open(write_pipe, O_WRONLY);
52         }
53 }
54
55 /**
56  * Program reads entire lines of text from the standard input and pass them
57  * to the process2 using created pipe.
58  */
59 int main(void) {
60         /** Currently fetched from stdin character */
61         int c;
62
63         /** Current buffer length*/
64         int buffer_length = 0;
65
66         /** Index of the current character */
67         int i = 0;
68
69         /**
70          * Temporary buffer used as a proxy between
71          * checking memory allocation and copying data to real buffer 
72          */
73         char * tmp = NULL;
74
75         fprintf(stderr, "[%s] Init!\n", "process1");
76
77         /**
78          * Register signals handled by process
79          */
80         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
81                 fprintf(stderr, "can't catch SIGUSR1\n");
82         }
83         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
84                 fprintf(stderr, "can't catch SIGTERM\n");
85         }
86         if (signal(SIGINT, sig_handler) == SIG_ERR) {
87                 fprintf(stderr, "can't catch SIGINT\n");
88         }
89         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
90                 fprintf(stderr, "can't catch SIGCONT\n");
91         }
92
93         mkfifo(write_pipe, 0666);
94
95         file_descriptor = open(write_pipe, O_WRONLY);
96
97         do {
98                 c = fgetc(stdin);
99
100                 /*
101                  * Check if current index is bigger than current buffer size.
102                  * If so increment buffer size. On error release memory, and set
103                  * appropriate flags.
104                  */
105                 if (i >= buffer_length) {
106                         tmp = NULL;
107                         buffer_length += BUFFER_STEP;
108                         tmp = (char*) realloc(buffer, buffer_length);
109                         if (tmp == NULL) {
110                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
111                                 free(buffer);
112                                 buffer = NULL;
113                                 c = EOF;
114                         }
115                 }
116
117                 /*
118                  * If there were no errors or it was not just an empty newline:
119                  * parse data.
120                  */
121                 if (c != EOF || ((i == 0) && (c == 10))) {
122                         /* If newline has been found
123                          * return entire string and release the memory
124                          */
125                         if (c == 10 && (i != 0)) {
126                                 buffer[i] = '\n';
127                                 write(file_descriptor, buffer, strlen(buffer));
128                                 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
129
130                                 buffer_length = 0;
131                                 i = 0;
132
133                                 free(buffer);
134                                 buffer = NULL;
135                                 tmp = NULL;
136                         }
137                         /*
138                          * Normal character, add it to the buffer
139                          */
140                         else {
141                                 buffer = tmp;
142                                 buffer[i] = c;
143
144                                 /* Used only for debugging..*/
145                                 /*
146                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
147                                 */
148                                 i++;
149                         }
150                 }
151         } while(c != EOF);
152
153         /* Release resources in normal program flow exit. */
154         close(write_pipe);
155         unlink(write_pipe);
156
157         if (buffer) {
158                 free(buffer);
159                 buffer = NULL;
160         }
161
162         return 0;
163 }