Replace previous change by different test
[minix.git] / servers / ipc / main.c
blobde371e598b8197b7b880e7c440b4d98b39e1b3fb
1 #include "inc.h"
3 int identifier = 0x1234;
4 endpoint_t who_e;
5 int call_type;
6 endpoint_t SELF_E;
8 static struct {
9 int type;
10 int (*func)(message *);
11 int reply; /* whether the reply action is passed through */
12 } ipc_calls[] = {
13 { IPC_SHMGET, do_shmget, 0 },
14 { IPC_SHMAT, do_shmat, 0 },
15 { IPC_SHMDT, do_shmdt, 0 },
16 { IPC_SHMCTL, do_shmctl, 0 },
17 { IPC_SEMGET, do_semget, 0 },
18 { IPC_SEMCTL, do_semctl, 0 },
19 { IPC_SEMOP, do_semop, 1 },
22 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
24 static int verbose = 0;
26 /* SEF functions and variables. */
27 static void sef_local_startup(void);
28 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
29 static void sef_cb_signal_handler(int signo);
31 int main(int argc, char *argv[])
33 message m;
35 /* SEF local startup. */
36 env_setargs(argc, argv);
37 sef_local_startup();
39 while (TRUE) {
40 int r;
41 int ipc_number;
43 if ((r = sef_receive(ANY, &m)) != OK)
44 printf("sef_receive failed %d.\n", r);
45 who_e = m.m_source;
46 call_type = m.m_type;
48 if(verbose)
49 printf("IPC: get %d from %d\n", call_type, who_e);
51 if (call_type & NOTIFY_MESSAGE) {
52 switch (who_e) {
53 case VM_PROC_NR:
54 /* currently, only semaphore needs such information. */
55 sem_process_vm_notify();
56 break;
57 default:
58 printf("IPC: ignoring notify() from %d\n",
59 who_e);
60 break;
62 continue;
66 * The ipc number in the table can be obtained
67 * with a simple equation because the values of
68 * IPC system calls are consecutive and begin
69 * at ( IPC_BASE + 1 )
72 ipc_number = call_type - (IPC_BASE + 1);
74 /* dispatch message */
75 if (ipc_number >= 0 && ipc_number < SIZE(ipc_calls)) {
76 int result;
78 if (ipc_calls[ipc_number].type != call_type)
79 panic("IPC: call table order mismatch");
81 /* If any process does an IPC call,
82 * we have to know about it exiting.
83 * Tell VM to watch it for us.
85 if(vm_watch_exit(m.m_source) != OK) {
86 printf("IPC: watch failed on %d\n", m.m_source);
89 result = ipc_calls[ipc_number].func(&m);
92 * The handler of the IPC call did not
93 * post a reply.
95 if (!ipc_calls[ipc_number].reply) {
97 m.m_type = result;
99 if(verbose && result != OK)
100 printf("IPC: error for %d: %d\n",
101 call_type, result);
103 if ((r = sendnb(who_e, &m)) != OK)
104 printf("IPC send error %d.\n", r);
106 } else {
107 /* warn and then ignore */
108 printf("IPC unknown call type: %d from %d.\n",
109 call_type, who_e);
111 update_refcount_and_destroy();
114 /* no way to get here */
115 return -1;
118 /*===========================================================================*
119 * sef_local_startup *
120 *===========================================================================*/
121 static void sef_local_startup()
123 /* Register init callbacks. */
124 sef_setcb_init_fresh(sef_cb_init_fresh);
125 sef_setcb_init_restart(sef_cb_init_fresh);
127 /* No live update support for now. */
129 /* Register signal callbacks. */
130 sef_setcb_signal_handler(sef_cb_signal_handler);
132 /* Let SEF perform startup. */
133 sef_startup();
136 /*===========================================================================*
137 * sef_cb_init_fresh *
138 *===========================================================================*/
139 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
141 /* Initialize the ipc server. */
143 SELF_E = getprocnr();
145 if(verbose)
146 printf("IPC: self: %d\n", SELF_E);
148 return(OK);
151 /*===========================================================================*
152 * sef_cb_signal_handler *
153 *===========================================================================*/
154 static void sef_cb_signal_handler(int signo)
156 /* Only check for termination signal, ignore anything else. */
157 if (signo != SIGTERM) return;
159 /* Checkout if there are still IPC keys. Inform the user in that case. */
160 if (!is_sem_nil() || !is_shm_nil())
161 printf("IPC: exit with un-clean states.\n");