1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <errno.h>
32 #include <sys/sem.h>
33 
34 #include <android-base/file.h>
35 
36 #include "utils.h"
37 
TEST(sys_sem,smoke)38 TEST(sys_sem, smoke) {
39   if (semctl(-1, 0, IPC_RMID) == -1 && errno == ENOSYS) {
40     GTEST_SKIP() << "no <sys/sem.h> support in this kernel";
41   }
42 
43   // Create a semaphore.
44   TemporaryDir dir;
45   key_t key = ftok(dir.path, 1);
46   int id = semget(key, 1, IPC_CREAT|0666);
47   ASSERT_NE(id, -1);
48 
49   // Check semaphore info.
50   semid_ds ds;
51   memset(&ds, 0, sizeof(ds));
52   ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
53   ASSERT_EQ(1U, ds.sem_nsems);
54 
55   ASSERT_EQ(0, semctl(id, 0, GETVAL));
56 
57   // Increment.
58   sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
59   ASSERT_EQ(0, semop(id, ops, 1));
60   ASSERT_EQ(1, semctl(id, 0, GETVAL));
61 
62   // Test timeouts.
63   timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
64   ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
65   errno = 0;
66   ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
67   ASSERT_ERRNO(EAGAIN);
68   ASSERT_EQ(1, semctl(id, 0, GETVAL));
69 
70   // Decrement.
71   ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
72   ASSERT_EQ(0, semop(id, ops, 1));
73   ASSERT_EQ(0, semctl(id, 0, GETVAL));
74 
75   // Destroy the semaphore.
76   ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
77 }
78 
TEST(sys_sem,semget_failure)79 TEST(sys_sem, semget_failure) {
80   errno = 0;
81   ASSERT_EQ(-1, semget(-1, -1, 0));
82   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
83 }
84 
TEST(sys_sem,semctl_failure)85 TEST(sys_sem, semctl_failure) {
86   errno = 0;
87   ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
88   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
89 }
90 
TEST(sys_sem,semop_failure)91 TEST(sys_sem, semop_failure) {
92 #pragma clang diagnostic push
93 #pragma clang diagnostic ignored "-Wnonnull"
94   errno = 0;
95   ASSERT_EQ(-1, semop(-1, nullptr, 0));
96   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
97 #pragma clang diagnostic pop
98 }
99 
TEST(sys_sem,semtimedop_failure)100 TEST(sys_sem, semtimedop_failure) {
101 #pragma clang diagnostic push
102 #pragma clang diagnostic ignored "-Wnonnull"
103   errno = 0;
104   ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
105   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
106 #pragma clang diagnostic pop
107 }
108 
TEST(sys_sem,union_semun)109 TEST(sys_sem, union_semun) {
110   // https://github.com/android-ndk/ndk/issues/400
111 #if defined(__BIONIC__)
112   semun arg;
113   semid_ds i1;
114   seminfo i2;
115   unsigned short a[] = { 1u, 2u };
116   arg.val = 123;
117   arg.buf = &i1;
118   arg.array = a;
119   arg.__buf = &i2;
120 #else
121   // glibc already mostly removed this cruft (although it's still in <linux/sem.h>).
122 #endif
123 }
124