summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormpi <mpi@openbsd.org>2012-01-04 17:59:28 +0000
committermpi <mpi@openbsd.org>2012-01-04 17:59:28 +0000
commit349e3315c537cc1c44cfe6f4c6665a61b37d68bd (patch)
treef60dcfffec3b553860f3c61d7953c437e97e6311
parentSplit out the semaphore functions. (diff)
downloadwireguard-openbsd-349e3315c537cc1c44cfe6f4c6665a61b37d68bd.tar.xz
wireguard-openbsd-349e3315c537cc1c44cfe6f4c6665a61b37d68bd.zip
Add some sanity checks, set errno accordingly to POSIX and add the named
semaphore stubs already provided by libpthread. We may move them in their own file in the future when we figure out a real implementation. Discussed with and looks ok to guenther@
-rw-r--r--lib/librthread/rthread_sem.c60
1 files changed, 56 insertions, 4 deletions
diff --git a/lib/librthread/rthread_sem.c b/lib/librthread/rthread_sem.c
index ea855d5d1d3..879fa75d4a9 100644
--- a/lib/librthread/rthread_sem.c
+++ b/lib/librthread/rthread_sem.c
@@ -126,9 +126,16 @@ sem_init(sem_t *semp, int pshared, unsigned int value)
return (-1);
}
+ if (value > SEM_VALUE_MAX) {
+ errno = EINVAL;
+ return (-1);
+ }
+
sem = calloc(1, sizeof(*sem));
- if (!sem)
+ if (!sem) {
+ errno = ENOSPC;
return (-1);
+ }
sem->value = value;
*semp = sem;
@@ -138,13 +145,17 @@ sem_init(sem_t *semp, int pshared, unsigned int value)
int
sem_destroy(sem_t *semp)
{
- if (!*semp)
- return (EINVAL);
+ if (!semp || !*semp) {
+ errno = EINVAL;
+ return (-1);
+ }
+
if ((*semp)->waitcount) {
#define MSG "sem_destroy on semaphore with waiters!\n"
write(2, MSG, sizeof(MSG) - 1);
#undef MSG
- return (EBUSY);
+ errno = EBUSY;
+ return (-1);
}
free(*semp);
*semp = NULL;
@@ -157,6 +168,11 @@ sem_getvalue(sem_t *semp, int *sval)
{
sem_t sem = *semp;
+ if (!semp || !*semp) {
+ errno = EINVAL;
+ return (-1);
+ }
+
_spinlock(&sem->lock);
*sval = sem->value;
_spinunlock(&sem->lock);
@@ -169,6 +185,11 @@ sem_post(sem_t *semp)
{
sem_t sem = *semp;
+ if (!semp || !*semp) {
+ errno = EINVAL;
+ return (-1);
+ }
+
_sem_post(sem);
return (0);
@@ -179,6 +200,11 @@ sem_wait(sem_t *semp)
{
sem_t sem = *semp;
+ if (!semp || !*semp) {
+ errno = EINVAL;
+ return (-1);
+ }
+
_sem_wait(sem, 0);
return (0);
@@ -190,6 +216,11 @@ sem_trywait(sem_t *semp)
sem_t sem = *semp;
int rv;
+ if (!semp || !*semp) {
+ errno = EINVAL;
+ return (-1);
+ }
+
rv = _sem_wait(sem, 1);
if (!rv) {
@@ -200,3 +231,24 @@ sem_trywait(sem_t *semp)
return (0);
}
+sem_t *
+sem_open(const char *name, int oflag, ...)
+{
+ errno = ENOSYS;
+ return (SEM_FAILED);
+}
+
+int
+sem_close(sem_t *sem)
+{
+ errno = ENOSYS;
+ return (-1);
+}
+
+int
+sem_unlink(const char *name)
+{
+ errno = ENOSYS;
+ return (-1);
+}
+