Remove not used conditional.
[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 #include <sys/shm.h>
12 #include <sys/msg.h>
13
14
15 /** If buffer is too small to hold entire string, it is incremented by this value */
16 #define BUFFER_STEP 16
17
18 /** Buffer used to store line of characters */
19 char * buffer = NULL;
20
21 /** Named pipe used to communnicate with process2 */
22 char * write_pipe = "/tmp/process1pipe";
23
24 /** File descriptor of pipe */
25 int file_descriptor;
26
27
28 /**
29  * Shared memory variables
30  */
31 /**
32  * Memory key for processes. Must be same between all processes to properly
33  * communicate.
34  */
35 key_t shmkey = 18912;
36 /**
37  * Id of the shared memory
38  */
39 int shmid;
40
41 /**
42  * Message shared by processes. Contains array of process IDs
43  */
44 struct message {
45         pid_t pids[3];
46 };
47
48 struct message * processes = NULL;
49
50 /**
51  * Message queue variables
52  */
53 key_t qkey = 12356;
54 int qid;
55
56 struct queue_message {
57         long mtype;
58         int signo[1];
59 };
60
61 void notify_other_processes(int signo) {
62         int i = 0;
63         struct queue_message msg;
64         msg.signo[0] = signo;
65
66         for (; i < 3; i++) {
67                 pid_t pid = processes->pids[i];
68                 // Bleh
69                 if (i != 0 && pid != 0) {
70                         msg.mtype = i+1;
71                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
72                         msgsnd(qid, &msg, sizeof(int), 0);
73                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process1", strsignal(SIGUSR1), SIGUSR1, pid);
74                         kill(pid, SIGUSR1);
75                 }
76         }
77 }
78
79 /**
80  * Handler for signals.
81  */
82 void sig_handler(int signo)
83 {
84         fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
85         if (signo == SIGUSR1) {
86                 fprintf(stderr, "[%s] > Notified!\n", "process1");
87                 struct queue_message msg;
88                 /* Check queues from both other processes */
89                 if (msgrcv(qid, &msg, sizeof(int), 1, 0) > 0) {
90                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
91                         raise(msg.signo[0]);
92                 }
93         }
94         else if (signo == SIGTERM) {
95                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
96                 processes->pids[1] = 0;
97                 notify_other_processes(signo);
98
99                 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
100                 close(write_pipe);
101                 unlink(write_pipe);
102
103                 if (buffer) {
104                         free(buffer);
105                         buffer = NULL;
106                 }
107                 exit(0);
108         }
109         else if (signo == SIGTSTP) {
110                 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
111                 close(write_pipe);
112                 raise (SIGSTOP);
113         }
114         else if (signo == SIGCONT) {
115                 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
116                 file_descriptor = open(write_pipe, O_WRONLY);
117         }
118 }
119
120 /**
121  * Program reads entire lines of text from the standard input and pass them
122  * to the process2 using created pipe.
123  */
124 int main(void) {
125         /** Currently fetched from stdin character */
126         int c;
127
128         /** Current buffer length*/
129         int buffer_length = 0;
130
131         /** Index of the current character */
132         int i = 0;
133
134         /**
135          * Temporary buffer used as a proxy between
136          * checking memory allocation and copying data to real buffer 
137          */
138         char * tmp = NULL;
139
140         fprintf(stderr, "[%s] Init!\n", "process1");
141
142         /**
143          * Register signals handled by process
144          */
145         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
146                 fprintf(stderr, "can't catch SIGUSR1\n");
147         }
148         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
149                 fprintf(stderr, "can't catch SIGTERM\n");
150         }
151         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
152                 fprintf(stderr, "can't catch SIGTSTP\n");
153         }
154         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
155                 fprintf(stderr, "can't catch SIGCONT\n");
156         }
157
158         /*
159          * Register memory to share with other processes, and pass current
160          * process id to the array.
161          */
162         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
163
164         processes = (struct message *)shmat(shmid, NULL, 0);
165         processes->pids[0] = getpid();
166
167         fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
168
169         /**
170          * Register message queue to communicate with other processes
171          */
172         qid = msgget(qkey, IPC_CREAT | 0666);
173         
174         mkfifo(write_pipe, 0666);
175
176         file_descriptor = open(write_pipe, O_WRONLY);
177
178         do {
179                 c = fgetc(stdin);
180
181                 /*
182                  * Check if current index is bigger than current buffer size.
183                  * If so increment buffer size. On error release memory, and set
184                  * appropriate flags.
185                  */
186                 if (i >= buffer_length) {
187                         tmp = NULL;
188                         buffer_length += BUFFER_STEP;
189                         tmp = (char*) realloc(buffer, buffer_length);
190                         if (tmp == NULL) {
191                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
192                                 free(buffer);
193                                 buffer = NULL;
194                                 c = EOF;
195                         }
196                 }
197
198                 /*
199                  * If there were no errors or it was not just an empty newline:
200                  * parse data.
201                  */
202                 if (c != EOF || ((i == 0) && (c == 10))) {
203                         /* If newline has been found
204                          * return entire string and release the memory
205                          */
206                         if (c == 10 && (i != 0)) {
207                                 buffer[i] = '\n';
208                                 write(file_descriptor, buffer, strlen(buffer));
209                                 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
210
211                                 buffer_length = 0;
212                                 i = 0;
213
214                                 free(buffer);
215                                 buffer = NULL;
216                                 tmp = NULL;
217                         }
218                         /*
219                          * Normal character, add it to the buffer
220                          */
221                         else {
222                                 buffer = tmp;
223                                 buffer[i] = c;
224
225                                 /* Used only for debugging..*/
226                                 /*
227                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
228                                 */
229                                 i++;
230                         }
231                 }
232         } while(c != EOF);
233
234         /* Release resources in normal program flow exit. */
235         close(write_pipe);
236         unlink(write_pipe);
237
238         if (buffer) {
239                 free(buffer);
240                 buffer = NULL;
241         }
242
243         return 0;
244 }