version update
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcipc.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcipc.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "sema.h"
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <sys/sem.h>
29 #include <sys/msg.h>
30 #include <sys/shm.h>
31
32
33
34
35
36 static ArrayList<int> global_shmem_db;
37 static ArrayList<int> global_sema_db;
38 static ArrayList<int> global_msg_db;
39 static Mutex global_ipc_lock;
40 static int crashed = 0;
41
42 // These must be atomic routines
43
44 int bc_enter_id(ArrayList<int>* list, int id);
45 int bc_remove_id(ArrayList<int>* list, int id);
46
47 void bc_ipc_termination(int signum)
48 {
49         global_ipc_lock.lock();
50
51
52         if(!crashed)
53         {
54
55 #if 0
56                 union semun arg;
57                 int i;
58
59                 if(global_shmem_db.total || global_sema_db.total || global_msg_db.total)
60                         printf("Crash\n");
61                 for(i = 0; i < global_shmem_db.total; i++)
62                 {
63                         if(!shmctl(global_shmem_db.values[i], IPC_RMID, NULL))
64                         {
65                                 printf("Deleted shared memory %d\n", global_shmem_db.values[i]);
66                         }
67                 }
68
69                 for(i = 0; i < global_sema_db.total; i++)
70                 {
71                         if(!semctl(global_sema_db.values[i], 0, IPC_RMID, arg))
72                         {
73                                 printf("Deleted semaphore %d\n", global_sema_db.values[i]);
74                         }
75                 }
76
77                 for(i = 0; i < global_msg_db.total; i++)
78                 {
79                         if(!msgctl(global_msg_db.values[i], IPC_RMID, NULL))
80                         {
81                                 printf("Deleted message %d\n", global_msg_db.values[i]);
82                         }
83                 }
84
85                 global_shmem_db.remove_all();
86                 global_sema_db.remove_all();
87                 global_msg_db.remove_all();
88
89
90
91 #endif
92
93
94 // dispatch user specified signal handler
95                 if(BC_Resources::signal_handler)
96                         BC_Resources::signal_handler->signal_handler(signum);
97                 crashed = 1;
98         }
99         global_ipc_lock.unlock();
100         exit(0);
101 }
102
103 int bc_init_ipc()
104 {
105         if(signal(SIGSEGV, bc_ipc_termination) == SIG_IGN)
106                 signal(SIGSEGV, SIG_IGN);
107         if(signal(SIGBUS, bc_ipc_termination) == SIG_IGN)
108                 signal(SIGBUS, SIG_IGN);
109         //      SIGKILL can't be ignored
110         //      if(signal(SIGKILL, bc_ipc_termination) == SIG_IGN)
111         //              signal(SIGKILL, SIG_IGN);
112         if(signal(SIGINT, bc_ipc_termination) == SIG_IGN)
113                 signal(SIGINT, SIG_IGN);
114         if(signal(SIGHUP, bc_ipc_termination) == SIG_IGN)
115                 signal(SIGHUP, SIG_IGN);
116         if(signal(SIGTERM, bc_ipc_termination) == SIG_IGN)
117                 signal(SIGTERM, SIG_IGN);
118         return 0;
119 }
120
121
122 int bc_enter_shmem_id(int id)
123 {
124         return bc_enter_id(&global_shmem_db, id);
125 }
126
127 int bc_remove_shmem_id(int id)
128 {
129         return bc_remove_id(&global_shmem_db, id);
130 }
131
132 int bc_enter_sema_id(int id)
133 {
134         return bc_enter_id(&global_sema_db, id);
135 }
136
137 int bc_remove_sema_id(int id)
138 {
139         return bc_remove_id(&global_sema_db, id);
140 }
141
142 int bc_enter_msg_id(int id)
143 {
144         return bc_enter_id(&global_msg_db, id);
145 }
146
147 int bc_remove_msg_id(int id)
148 {
149         return bc_remove_id(&global_msg_db, id);
150 }
151
152 int bc_enter_id(ArrayList<int>* list, int id)
153 {
154         int i, result = 0;
155         global_ipc_lock.lock();
156         for(i = 0; i < list->total; i++)
157         {
158                 if(list->values[i] == id) result = 1;
159         }
160         if(!result) list->append(id);
161         global_ipc_lock.unlock();
162         return 0;
163 }
164
165 int bc_remove_id(ArrayList<int>* list, int id)
166 {
167         int i;
168         global_ipc_lock.lock();
169         for(i = 0; i < list->total; i++)
170         {
171                 if(list->values[i] == id) list->remove_number(i);
172         }
173         global_ipc_lock.unlock();
174         return 0;
175 }
176