ipc.c 1.3 KB

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