From 22c3fafb25b742973d7122edc626b30d03bdb36c Mon Sep 17 00:00:00 2001 From: "Gary E. Miller" Date: Fri, 15 Apr 2016 21:01:17 -0700 Subject: Do not let the SHM linger forever. If the SHM hangs around, and then the size increases, then the SHM can no longer be opened. --- shmexport.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'shmexport.c') diff --git a/shmexport.c b/shmexport.c index 179a6142..2a0e4166 100644 --- a/shmexport.c +++ b/shmexport.c @@ -34,6 +34,7 @@ PERMISSIONS bool shm_acquire(struct gps_context_t *context) /* initialize the shared-memory segment to be used for export */ { + int status; long shmkey = getenv("GPSD_SHM_KEY") ? strtol(getenv("GPSD_SHM_KEY"), NULL, 0) : GPSD_SHM_KEY; int shmid = shmget((key_t)shmkey, sizeof(struct shmexport_t), (int)(IPC_CREAT|0666)); @@ -57,6 +58,17 @@ bool shm_acquire(struct gps_context_t *context) context->shmexport = NULL; return false; } + /* mark shmid to go away when no longer used + * Having it linger forever is bad, and when the size enlarges + * it can no longer be opened */ + status = shmctl(shmid, IPC_RMID, NULL); + if (status == -1) { + gpsd_log(&context->errout, LOG_ERROR, + "shmctl failed, errno = %d (%s)\n", + errno, strerror(errno)); + exit(1); + } + gpsd_log(&context->errout, LOG_PROG, "shmat() for SHM export succeeded, segment %d\n", shmid); return true; -- cgit v1.2.1