fix failure behavior of sem_open when sem does not exist
authorRich Felker <dalias@aerifal.cx>
Fri, 11 Mar 2011 03:05:16 +0000 (22:05 -0500)
committerRich Felker <dalias@aerifal.cx>
Fri, 11 Mar 2011 03:05:16 +0000 (22:05 -0500)
src/thread/sem_open.c

index 6fff71a..809925d 100644 (file)
@@ -100,7 +100,7 @@ sem_t *sem_open(const char *name, int flags, ...)
                                        close(tfd);
                                        unlink(tmp);
                                }
                                        close(tfd);
                                        unlink(tmp);
                                }
-                               if (fstat(fd, &st) < 0) {
+                               if (fd >= 0 && fstat(fd, &st) < 0) {
                                        close(fd);
                                        fd = -1;
                                }
                                        close(fd);
                                        fd = -1;
                                }
@@ -118,6 +118,10 @@ sem_t *sem_open(const char *name, int flags, ...)
                                break;
                        }
                }
                                break;
                        }
                }
+               if (!(flags & O_CREAT)) {
+                       pthread_spin_unlock(&lock);
+                       return SEM_FAILED;
+               }
                if (!linkat(AT_FDCWD, tmp, dir, name, 0)) {
                        fd = tfd;
                        close(dir);
                if (!linkat(AT_FDCWD, tmp, dir, name, 0)) {
                        fd = tfd;
                        close(dir);