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