2 #include "bcresources.h"
15 static ArrayList<int> global_shmem_db;
16 static ArrayList<int> global_sema_db;
17 static ArrayList<int> global_msg_db;
18 static Mutex global_ipc_lock;
19 static int crashed = 0;
21 // These must be atomic routines
23 int bc_enter_id(ArrayList<int>* list, int id);
24 int bc_remove_id(ArrayList<int>* list, int id);
26 void bc_ipc_termination(int signum)
28 global_ipc_lock.lock();
38 if(global_shmem_db.total || global_sema_db.total || global_msg_db.total)
40 for(i = 0; i < global_shmem_db.total; i++)
42 if(!shmctl(global_shmem_db.values[i], IPC_RMID, NULL))
44 printf("Deleted shared memory %d\n", global_shmem_db.values[i]);
48 for(i = 0; i < global_sema_db.total; i++)
50 if(!semctl(global_sema_db.values[i], 0, IPC_RMID, arg))
52 printf("Deleted semaphore %d\n", global_sema_db.values[i]);
56 for(i = 0; i < global_msg_db.total; i++)
58 if(!msgctl(global_msg_db.values[i], IPC_RMID, NULL))
60 printf("Deleted message %d\n", global_msg_db.values[i]);
64 global_shmem_db.remove_all();
65 global_sema_db.remove_all();
66 global_msg_db.remove_all();
73 // dispatch user specified signal handler
74 if(BC_Resources::signal_handler)
75 BC_Resources::signal_handler->signal_handler(signum);
78 global_ipc_lock.unlock();
84 if(signal(SIGSEGV, bc_ipc_termination) == SIG_IGN)
85 signal(SIGSEGV, SIG_IGN);
86 if(signal(SIGBUS, bc_ipc_termination) == SIG_IGN)
87 signal(SIGBUS, SIG_IGN);
88 // SIGKILL can't be ignored
89 // if(signal(SIGKILL, bc_ipc_termination) == SIG_IGN)
90 // signal(SIGKILL, SIG_IGN);
91 if(signal(SIGINT, bc_ipc_termination) == SIG_IGN)
92 signal(SIGINT, SIG_IGN);
93 if(signal(SIGHUP, bc_ipc_termination) == SIG_IGN)
94 signal(SIGHUP, SIG_IGN);
95 if(signal(SIGTERM, bc_ipc_termination) == SIG_IGN)
96 signal(SIGTERM, SIG_IGN);
101 int bc_enter_shmem_id(int id)
103 return bc_enter_id(&global_shmem_db, id);
106 int bc_remove_shmem_id(int id)
108 return bc_remove_id(&global_shmem_db, id);
111 int bc_enter_sema_id(int id)
113 return bc_enter_id(&global_sema_db, id);
116 int bc_remove_sema_id(int id)
118 return bc_remove_id(&global_sema_db, id);
121 int bc_enter_msg_id(int id)
123 return bc_enter_id(&global_msg_db, id);
126 int bc_remove_msg_id(int id)
128 return bc_remove_id(&global_msg_db, id);
131 int bc_enter_id(ArrayList<int>* list, int id)
134 global_ipc_lock.lock();
135 for(i = 0; i < list->total; i++)
137 if(list->values[i] == id) result = 1;
139 if(!result) list->append(id);
140 global_ipc_lock.unlock();
144 int bc_remove_id(ArrayList<int>* list, int id)
147 global_ipc_lock.lock();
148 for(i = 0; i < list->total; i++)
150 if(list->values[i] == id) list->remove_number(i);
152 global_ipc_lock.unlock();