blob: eaf2b8f821b26884589e3aeec0bab7388f9578f6 [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
34#include "TemporaryFile.h"
35
36TEST(sys_sem, smoke) {
Elliott Hughes40bae4f2016-08-26 18:33:19 -070037 if (semctl(-1, 0, IPC_RMID) == -1 && errno == ENOSYS) {
38 GTEST_LOG_(INFO) << "no <sys/sem.h> support in this kernel\n";
39 return;
40 }
41
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070042 // Create a semaphore.
43 TemporaryDir dir;
44 key_t key = ftok(dir.dirname, 1);
45 int id = semget(key, 1, IPC_CREAT|0666);
46 ASSERT_NE(id, -1);
47
48 // Check semaphore info.
49 semid_ds ds;
50 memset(&ds, 0, sizeof(ds));
51 ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
52 ASSERT_EQ(1U, ds.sem_nsems);
53
54 ASSERT_EQ(0, semctl(id, 0, GETVAL));
55
56 // Increment.
57 sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
58 ASSERT_EQ(0, semop(id, ops, 1));
59 ASSERT_EQ(1, semctl(id, 0, GETVAL));
60
61 // Test timeouts.
62 timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
63 ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
64 errno = 0;
65 ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
66 ASSERT_EQ(EAGAIN, errno);
67 ASSERT_EQ(1, semctl(id, 0, GETVAL));
68
69 // Decrement.
70 ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
71 ASSERT_EQ(0, semop(id, ops, 1));
72 ASSERT_EQ(0, semctl(id, 0, GETVAL));
73
74 // Destroy the semaphore.
75 ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
76}
77
78TEST(sys_sem, semget_failure) {
79 errno = 0;
80 ASSERT_EQ(-1, semget(-1, -1, 0));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070081 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070082}
83
84TEST(sys_sem, semctl_failure) {
85 errno = 0;
86 ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070087 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070088}
89
90TEST(sys_sem, semop_failure) {
91 errno = 0;
92 ASSERT_EQ(-1, semop(-1, nullptr, 0));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070093 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -070094}
95
96TEST(sys_sem, semtimedop_failure) {
97 errno = 0;
98 ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
Elliott Hughes40bae4f2016-08-26 18:33:19 -070099 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
Elliott Hughes7c59f3f2016-08-16 18:14:26 -0700100}
Elliott Hughes497ad302017-05-18 15:05:26 -0700101
102TEST(sys_sem, union_semun) {
103 // https://github.com/android-ndk/ndk/issues/400
104#if defined(__BIONIC__)
105 semun arg;
106 semid_ds i1;
107 seminfo i2;
108 unsigned short a[] = { 1u, 2u };
109 arg.val = 123;
110 arg.buf = &i1;
111 arg.array = a;
112 arg.__buf = &i2;
113#else
114 // glibc already mostly removed this cruft (although it's still in <linux/sem.h>).
115#endif
116}