@@ -48,8 +48,10 @@ static char *_oauth2_ipc_get_name(oauth2_log_t *log, const char *type,
4848{
4949 char * rv = NULL ;
5050 rv = oauth2_mem_alloc (_OAUTH2_IPC_NAME_MAX );
51- oauth2_snprintf (rv , _OAUTH2_IPC_NAME_MAX , "/zzo-%s-%ld.%p" , type ,
52- (long int )getpid (), ptr );
51+ // oauth2_snprintf(rv, _OAUTH2_IPC_NAME_MAX, "/zzo-%s-%ld.%p", type,
52+ // (long int)getpid(), ptr);
53+ oauth2_snprintf (rv , _OAUTH2_IPC_NAME_MAX , "/zzo-%s-%p" , type , ptr ? ptr : 0 );
54+ //oauth2_snprintf(rv, _OAUTH2_IPC_NAME_MAX, "/zzo-%s", type);
5355 return rv ;
5456}
5557
@@ -80,11 +82,6 @@ void oauth2_ipc_sema_free(oauth2_log_t *log, oauth2_ipc_sema_t *s)
8082 if (sem_close (s -> sema ) != 0 )
8183 oauth2_error (log , "sem_close() failed: %s " ,
8284 strerror (errno ));
83- if (sem_unlink (s -> name ) != 0 ) {
84- oauth2_error (log , "sem_unlink() failed: %s " ,
85- strerror (errno ));
86- goto end ;
87- }
8885 s -> sema = NULL ;
8986 }
9087
@@ -114,7 +111,7 @@ bool oauth2_ipc_sema_post_config(oauth2_log_t *log, oauth2_ipc_sema_t *sema)
114111 if (sema -> name == NULL )
115112 goto end ;
116113
117- sema -> sema = sem_open (sema -> name , O_CREAT | O_EXCL , 0644 , 0 );
114+ sema -> sema = sem_open (sema -> name , O_CREAT , 0644 , 0 );
118115 if (sema -> sema == SEM_FAILED ) {
119116 oauth2_error (
120117 log ,
@@ -124,6 +121,9 @@ bool oauth2_ipc_sema_post_config(oauth2_log_t *log, oauth2_ipc_sema_t *sema)
124121 goto end ;
125122 }
126123
124+ if (sem_unlink (sema -> name ) != 0 )
125+ oauth2_error (log , "sem_unlink() failed: %s " , strerror (errno ));
126+
127127 rc = true;
128128
129129end :
@@ -302,7 +302,7 @@ bool oauth2_ipc_mutex_unlock(oauth2_log_t *log, oauth2_ipc_mutex_t *m)
302302
303303typedef struct oauth2_ipc_shm_t {
304304 char * name ;
305- int fd ;
305+ // int fd;
306306 oauth2_ipc_mutex_t * mutex ;
307307 oauth2_ipc_sema_t * num ;
308308 size_t size ;
@@ -313,7 +313,7 @@ oauth2_ipc_shm_t *oauth2_ipc_shm_init(oauth2_log_t *log, size_t size)
313313{
314314 oauth2_ipc_shm_t * shm = oauth2_mem_alloc (sizeof (oauth2_ipc_shm_t ));
315315 shm -> mutex = oauth2_ipc_mutex_init (log );
316- shm -> fd = -1 ;
316+ // shm->fd = -1;
317317 shm -> num = oauth2_ipc_sema_init (log );
318318 shm -> name = NULL ;
319319 shm -> ptr = NULL ;
@@ -333,9 +333,11 @@ void oauth2_ipc_shm_free(oauth2_log_t *log, oauth2_ipc_shm_t *shm)
333333 oauth2_ipc_mutex_free (log , shm -> mutex );
334334 shm -> mutex = NULL ;
335335
336- if (shm -> fd != -1 ) {
337- close (shm -> fd );
338- shm -> fd = -1 ;
336+ if (shm -> ptr ) {
337+ if (munmap (shm -> ptr , shm -> size ) < 0 )
338+ oauth2_error (log , "munmap() failed: %s" ,
339+ strerror (errno ));
340+ shm -> ptr = NULL ;
339341 }
340342
341343 if (shm -> num ) {
@@ -365,6 +367,7 @@ void oauth2_ipc_shm_free(oauth2_log_t *log, oauth2_ipc_shm_t *shm)
365367bool oauth2_ipc_shm_post_config (oauth2_log_t * log , oauth2_ipc_shm_t * shm )
366368{
367369 bool rc = false;
370+ int fd = -1 ;
368371
369372 if (shm == NULL )
370373 goto end ;
@@ -383,30 +386,31 @@ bool oauth2_ipc_shm_post_config(oauth2_log_t *log, oauth2_ipc_shm_t *shm)
383386
384387 oauth2_debug (log , "creating shm with name: %s" , shm -> name );
385388
386- shm -> fd = shm_open (shm -> name , O_CREAT | O_RDWR , 0666 );
387- if (shm -> fd == -1 ) {
388- oauth2_error (log , "shm_open() failed:: %s" , strerror (errno ));
389+ fd = shm_open (shm -> name , O_CREAT | O_RDWR , 0666 );
390+ if (fd == -1 ) {
391+ oauth2_error (log , "shm_open() failed: %s" , strerror (errno ));
389392 goto end ;
390393 }
391394
392- if (ftruncate (shm -> fd , shm -> size ) != 0 ) {
395+ if (ftruncate (fd , shm -> size ) != 0 ) {
393396 oauth2_error (log , "ftruncate() failed: %s" , strerror (errno ));
394- goto end ;
397+ // goto end;
395398 }
396399
397400 shm -> ptr =
398- mmap (0 , shm -> size , PROT_READ | PROT_WRITE , MAP_SHARED , shm -> fd , 0 );
401+ mmap (0 , shm -> size , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_ANONYMOUS , -1 /*fd*/ , 0 );
399402 if (shm -> ptr == MAP_FAILED ) {
400- oauth2_error (log , "mmap() failed:: %s" , strerror (errno ));
403+ oauth2_error (log , "mmap() failed: %s" , strerror (errno ));
401404 goto end ;
402405 }
403406
404- // TODO: we could close fd here
405-
406407 rc = oauth2_ipc_sema_post (log , shm -> num );
407408
408409end :
409410
411+ if (fd != -1 )
412+ close (fd );
413+
410414 return rc ;
411415}
412416
0 commit comments