blob: 4bac92fc880b4a3fea221462909cb6b87805ddff [file] [log] [blame]
Elliott Hughes7c59f3f2016-08-16 18:14:26 -07001/*
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
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080034#include <android-base/file.h>
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070035
36TEST(sys_sem, smoke) {
Elliott Hughes40bae4f2016-08-26 18:33:19 -070037 if (semctl(-1, 0, IPC_RMID) == -1 && errno == ENOSYS) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080038 GTEST_SKIP() << "no <sys/sem.h> support in this kernel";
Elliott Hughes40bae4f2016-08-26 18:33:19 -070039 }
40
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070041 // Create a semaphore.
42 TemporaryDir dir;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080043 key_t key = ftok(dir.path, 1);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070044 int id = semget(key, 1, IPC_CREAT|0666);
45 ASSERT_NE(id, -1);
46
47 // Check semaphore info.
48 semid_ds ds;
49 memset(&ds, 0, sizeof(ds));
50 ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
51 ASSERT_EQ(1U, ds.sem_nsems);
52
53 ASSERT_EQ(0, semctl(id, 0, GETVAL));
54
55 // Increment.
56 sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
57 ASSERT_EQ(0, semop(id, ops, 1));
58 ASSERT_EQ(1, semctl(id, 0, GETVAL));
59
60 // Test timeouts.
61 timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
62 ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
63 errno = 0;
64 ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
65 ASSERT_EQ(EAGAIN, errno);
66 ASSERT_EQ(1, semctl(id, 0, GETVAL));
67
68 // Decrement.
69 ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
70 ASSERT_EQ(0, semop(id, ops, 1));
71 ASSERT_EQ(0, semctl(id, 0, GETVAL));
72
73 // Destroy the semaphore.
74 ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
75}
76
77TEST(sys_sem, semget_failure) {
78 errno = 0;
79 ASSERT_EQ(-1, semget(-1, -1, 0));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070080 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070081}
82
83TEST(sys_sem, semctl_failure) {
84 errno = 0;
85 ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070086 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070087}
88
89TEST(sys_sem, semop_failure) {
zijunzhao271d4d22023-04-20 20:46:01 +000090#pragma clang diagnostic push
91#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070092 errno = 0;
93 ASSERT_EQ(-1, semop(-1, nullptr, 0));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070094 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
zijunzhao271d4d22023-04-20 20:46:01 +000095#pragma clang diagnostic pop
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070096}
97
98TEST(sys_sem, semtimedop_failure) {
zijunzhao271d4d22023-04-20 20:46:01 +000099#pragma clang diagnostic push
100#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes7c59f3f2016-08-16 18:14:26 -0700101 errno = 0;
102 ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
Elliott Hughes40bae4f2016-08-26 18:33:19 -0700103 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
zijunzhao271d4d22023-04-20 20:46:01 +0000104#pragma clang diagnostic pop
Elliott Hughes7c59f3f2016-08-16 18:14:26 -0700105}
Elliott Hughes497ad302017-05-18 15:05:26 -0700106
107TEST(sys_sem, union_semun) {
108 // https://github.com/android-ndk/ndk/issues/400
109#if defined(__BIONIC__)
110 semun arg;
111 semid_ds i1;
112 seminfo i2;
113 unsigned short a[] = { 1u, 2u };
114 arg.val = 123;
115 arg.buf = &i1;
116 arg.array = a;
117 arg.__buf = &i2;
118#else
119 // glibc already mostly removed this cruft (although it's still in <linux/sem.h>).
120#endif
121}