ipc.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* $Id: ipc.c,v 1.11 2001/04/29 22:07:44 kilobug Exp $ */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <signal.h>
  6. #include <sys/ipc.h>
  7. #include <sys/shm.h>
  8. #include <server.h>
  9. static GSList *shm_memory_list = NULL;
  10. /*
  11. ** SHM
  12. */
  13. int get_shm_key(char c)
  14. {
  15. int i;
  16. i = ftok(".", c);
  17. if (i != -1)
  18. {
  19. return i;
  20. }
  21. perror("ftok");
  22. abort();
  23. }
  24. int get_shm_conf_id()
  25. {
  26. return get_shm_key('C');
  27. }
  28. int get_shm_world_id()
  29. {
  30. return get_shm_key('W');
  31. }
  32. int get_shm_plr_id()
  33. {
  34. return get_shm_key('p');
  35. }
  36. void *shm_alloc(int size, int id)
  37. {
  38. int shmid;
  39. void *res = NULL;
  40. if ((shmid = shmget(id, size, IPC_CREAT | IPC_EXCL | 0600)) == -1)
  41. {
  42. if ((shmid = shmget(id, size, 0)) == -1)
  43. {
  44. perror("shmget");
  45. abort();
  46. }
  47. }
  48. if ((res = shmat(shmid, 0, 0)) == (void *)-1)
  49. {
  50. perror("shmat");
  51. abort();
  52. }
  53. shm_memory_list = g_slist_prepend(shm_memory_list, GINT_TO_POINTER(shmid));
  54. return res;
  55. }
  56. void clean_shm()
  57. {
  58. GSList *l;
  59. if (gl_config->messager_pid)
  60. {
  61. kill(gl_config->messager_pid, SIGUSR1);
  62. }
  63. for (l = gl_config->players ; l != NULL ; l = l->next)
  64. {
  65. gl_player = l->data;
  66. if (gl_player->pid)
  67. {
  68. kill(gl_player->pid, SIGKILL);
  69. }
  70. }
  71. for (l = shm_memory_list ; l != NULL ; l = l->next)
  72. {
  73. shmctl(GPOINTER_TO_INT(l->data), IPC_RMID, NULL);
  74. }
  75. g_slist_free(shm_memory_list);
  76. shm_memory_list = NULL;
  77. }