/* This file is a port of posix named semaphore for Termux Android, based on musl-libc which is licensed under the standard MIT license. The ported files are listed as following. File(s): src/thread/sem_open.c src/thread/sem_unlink.c Copyright © 2005-2020 Rich Felker, et al. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include // sem_t, sem_init() #include // mmap(), munmap() #include // SEM_NSEMS_MAX, NAME_MAX, INT_MAX #include // open() #include // write(), close(), unlink() #include // strlen(), memcpy() #include // va_arg #include // errno #include // fstat() #include // calloc() #include // mutex #include // _PATH_TMP #ifndef SEM_NSEMS_MAX #define SEM_NSEMS_MAX 256 #endif // !SEM_NSEMS_MAX #define SEM_PREFIX _PATH_TMP "sem." static __inline__ char *__strchrnul(const char *s, int c) { c = (unsigned char)c; if (!c) return (char *)s + strlen(s); for (; *s && *(unsigned char *)s != c; s++); return (char *)s; } typedef struct { ino_t ino; sem_t *sem; int refcnt; } semtab_type; static semtab_type *semtab; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; #define LOCK(l) (pthread_mutex_lock(&l)) #define UNLOCK(l) (pthread_mutex_unlock(&l)) #define FLAGS (O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK) static char *__sem_mapname(const char *name, char *buf) { char *p; while (*name == '/') name++; if (*(p = __strchrnul(name, '/')) || p==name || (p-name <= 2 && name[0]=='.' && p[-1]=='.')) { errno = EINVAL; return 0; } if (p-name > NAME_MAX-4) { errno = ENAMETOOLONG; return 0; } memcpy(buf, SEM_PREFIX, strlen(SEM_PREFIX)); memcpy(buf+strlen(SEM_PREFIX), name, p-name+1); return buf; } sem_t *sem_open(const char *name, int flags, ...) { va_list ap; mode_t mode; unsigned value; int fd, i, slot, cnt; sem_t newsem; void *map; struct stat st; char buf[NAME_MAX+strlen(SEM_PREFIX)+1]; if (!(name = __sem_mapname(name, buf))) return SEM_FAILED; LOCK(lock); /* Allocate table if we don't have one yet */ if (!semtab && !(semtab = (semtab_type *)(calloc(SEM_NSEMS_MAX, sizeof *semtab)))) { UNLOCK(lock); return SEM_FAILED; } /* Reserve a slot in case this semaphore is not mapped yet; * this is necessary because there is no way to handle * failures after creation of the file. */ slot = -1; for (cnt=i=0; i