4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcresources.h"
24 #include "bcsignals.h"
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;
42 // These must be atomic routines
44 int bc_enter_id(ArrayList<int>* list, int id);
45 int bc_remove_id(ArrayList<int>* list, int id);
47 void bc_ipc_termination(int signum)
49 global_ipc_lock.lock();
59 if(global_shmem_db.total || global_sema_db.total || global_msg_db.total)
61 for(i = 0; i < global_shmem_db.total; i++)
63 if(!shmctl(global_shmem_db.values[i], IPC_RMID, NULL))
65 printf("Deleted shared memory %d\n", global_shmem_db.values[i]);
69 for(i = 0; i < global_sema_db.total; i++)
71 if(!semctl(global_sema_db.values[i], 0, IPC_RMID, arg))
73 printf("Deleted semaphore %d\n", global_sema_db.values[i]);
77 for(i = 0; i < global_msg_db.total; i++)
79 if(!msgctl(global_msg_db.values[i], IPC_RMID, NULL))
81 printf("Deleted message %d\n", global_msg_db.values[i]);
85 global_shmem_db.remove_all();
86 global_sema_db.remove_all();
87 global_msg_db.remove_all();
94 // dispatch user specified signal handler
95 if(BC_Resources::signal_handler)
96 BC_Resources::signal_handler->signal_handler(signum);
99 global_ipc_lock.unlock();
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);
122 int bc_enter_shmem_id(int id)
124 return bc_enter_id(&global_shmem_db, id);
127 int bc_remove_shmem_id(int id)
129 return bc_remove_id(&global_shmem_db, id);
132 int bc_enter_sema_id(int id)
134 return bc_enter_id(&global_sema_db, id);
137 int bc_remove_sema_id(int id)
139 return bc_remove_id(&global_sema_db, id);
142 int bc_enter_msg_id(int id)
144 return bc_enter_id(&global_msg_db, id);
147 int bc_remove_msg_id(int id)
149 return bc_remove_id(&global_msg_db, id);
152 int bc_enter_id(ArrayList<int>* list, int id)
155 global_ipc_lock.lock();
156 for(i = 0; i < list->total; i++)
158 if(list->values[i] == id) result = 1;
160 if(!result) list->append(id);
161 global_ipc_lock.unlock();
165 int bc_remove_id(ArrayList<int>* list, int id)
168 global_ipc_lock.lock();
169 for(i = 0; i < list->total; i++)
171 if(list->values[i] == id) list->remove_number(i);
173 global_ipc_lock.unlock();