summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Pieuchot <mpi@cvs.openbsd.org>2012-01-04 17:59:29 +0000
committerMartin Pieuchot <mpi@cvs.openbsd.org>2012-01-04 17:59:29 +0000
commited330b1b2bc29ed5af74d2fff88eddcc0c2e7967 (patch)
treeb3f39dd04a957d0e245e31561fa3039fa2dc3be2
parent0d5f7123c39d5acf2e4f31f2bbbc4b9e6e0e5f14 (diff)
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);
+}
+