Merge "Flesh out <sys/msg.h>, <sys/sem.h>, <sys/shm.h>."

This commit is contained in:
Elliott Hughes 2016-08-18 23:58:42 +00:00 committed by Gerrit Code Review
commit 7d4e28a486
24 changed files with 676 additions and 9 deletions

View file

@ -1350,6 +1350,9 @@ cc_library_static {
"bionic/sync_file_range.cpp",
"bionic/sysinfo.cpp",
"bionic/syslog.cpp",
"bionic/sys_msg.cpp",
"bionic/sys_sem.cpp",
"bionic/sys_shm.cpp",
"bionic/sys_siglist.c",
"bionic/sys_signame.c",
"bionic/sys_time.cpp",

68
libc/bionic/sys_msg.cpp Normal file
View file

@ -0,0 +1,68 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/msg.h>
#include <sys/syscall.h>
#include <unistd.h>
int msgctl(int id, int cmd, msqid_ds* buf) {
#if !defined(__LP64__)
// Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
cmd |= IPC_64;
#endif
#if __i386__
return syscall(SYS_ipc, MSGCTL, id, cmd, 0, buf, 0);
#else
return syscall(SYS_msgctl, id, cmd, buf);
#endif
}
int msgget(key_t key, int flags) {
#if __i386__
return syscall(SYS_ipc, MSGGET, key, flags, 0, 0, 0);
#else
return syscall(SYS_msgget, key, flags);
#endif
}
ssize_t msgrcv(int id, void* msg, size_t n, long type, int flags) {
#if __i386__
return syscall(SYS_ipc, IPCCALL(1, MSGRCV), id, n, flags, msg, type);
#else
return syscall(SYS_msgrcv, id, msg, n, type, flags);
#endif
}
int msgsnd(int id, const void* msg, size_t n, int flags) {
#if __i386__
return syscall(SYS_ipc, MSGSND, id, n, flags, msg, 0);
#else
return syscall(SYS_msgsnd, id, msg, n, flags);
#endif
}

69
libc/bionic/sys_sem.cpp Normal file
View file

@ -0,0 +1,69 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/sem.h>
#include <stdarg.h>
#include <sys/syscall.h>
#include <unistd.h>
int semctl(int id, int num, int cmd, ...) {
#if !defined(__LP64__)
// Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
cmd |= IPC_64;
#endif
va_list ap;
va_start(ap, cmd);
semun arg = va_arg(ap, semun);
va_end(ap);
#if __i386__
return syscall(SYS_ipc, SEMCTL, id, num, cmd, &arg, 0);
#else
return syscall(SYS_semctl, id, num, cmd, arg);
#endif
}
int semget(key_t key, int n, int flags) {
#if __i386__
return syscall(SYS_ipc, SEMGET, key, n, flags, 0, 0);
#else
return syscall(SYS_semget, key, n, flags);
#endif
}
int semop(int id, sembuf* ops, size_t op_count) {
return semtimedop(id, ops, op_count, nullptr);
}
int semtimedop(int id, sembuf* ops, size_t op_count, const timespec* ts) {
#if __i386__
return syscall(SYS_ipc, SEMTIMEDOP, id, op_count, 0, ops, ts);
#else
return syscall(SYS_semtimedop, id, ops, op_count, ts);
#endif
}

73
libc/bionic/sys_shm.cpp Normal file
View file

@ -0,0 +1,73 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/shm.h>
#include <sys/syscall.h>
#include <unistd.h>
void* shmat(int id, const void* address, int flags) {
#if __i386__
// See the kernel's ipc/syscall.c for the other side of this dance.
void* result = nullptr;
if (syscall(SYS_ipc, SHMAT, id, flags, &result, address, 0) == -1) {
return reinterpret_cast<void*>(-1);
}
return result;
#else
return reinterpret_cast<void*>(syscall(SYS_shmat, id, address, flags));
#endif
}
int shmctl(int id, int cmd, struct shmid_ds* buf) {
#if !defined(__LP64__)
// Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
cmd |= IPC_64;
#endif
#if __i386__
return syscall(SYS_ipc, SHMCTL, id, cmd, 0, buf, 0);
#else
return syscall(SYS_shmctl, id, cmd, buf);
#endif
}
int shmdt(const void* address) {
#if __i386__
return syscall(SYS_ipc, SHMDT, 0, 0, 0, address, 0);
#else
return syscall(SYS_shmdt, address);
#endif
}
int shmget(key_t key, size_t size, int flags) {
#if __i386__
return syscall(SYS_ipc, SHMGET, key, size, flags, 0, 0);
#else
return syscall(SYS_shmget, key, size, flags);
#endif
}

View file

@ -33,6 +33,8 @@
#include <sys/types.h>
#include <linux/ipc.h>
#define ipc_perm ipc64_perm
__BEGIN_DECLS
key_t ftok(const char* path, int id);

View file

@ -30,6 +30,22 @@
#define _SYS_MSG_H_
#include <sys/cdefs.h>
#include <sys/ipc.h>
#include <linux/msg.h>
#define msqid_ds msqid64_ds
__BEGIN_DECLS
typedef __kernel_ulong_t msgqnum_t;
typedef __kernel_ulong_t msglen_t;
int msgctl(int, int, struct msqid_ds*) __INTRODUCED_IN_FUTURE;
int msgget(key_t, int) __INTRODUCED_IN_FUTURE;
ssize_t msgrcv(int, void*, size_t, long, int) __INTRODUCED_IN_FUTURE;
int msgsnd(int, const void*, size_t, int) __INTRODUCED_IN_FUTURE;
__END_DECLS
#endif /* _SYS_MSG_H_ */

View file

@ -30,6 +30,27 @@
#define _SYS_SEM_H_
#include <sys/cdefs.h>
#include <sys/ipc.h>
#include <sys/types.h>
#if defined(__USE_GNU)
#include <bits/timespec.h>
#endif
#include <linux/sem.h>
#endif /* _SYS_SEM_H_ */
#define semid_ds semid64_ds
__BEGIN_DECLS
int semctl(int, int, int, ...) __INTRODUCED_IN_FUTURE;
int semget(key_t, int, int) __INTRODUCED_IN_FUTURE;
int semop(int, struct sembuf*, size_t) __INTRODUCED_IN_FUTURE;
#if defined(__USE_GNU)
int semtimedop(int, struct sembuf*, size_t, const struct timespec*) __INTRODUCED_IN_FUTURE;
#endif
__END_DECLS
#endif

52
libc/include/sys/shm.h Normal file
View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SYS_SHM_H_
#define _SYS_SHM_H_
#include <sys/cdefs.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <linux/shm.h>
#define shmid_ds shmid64_ds
#define SHMLBA 4096
__BEGIN_DECLS
typedef unsigned long shmatt_t;
void* shmat(int, const void*, int) __INTRODUCED_IN_FUTURE;
int shmctl(int, int, struct shmid_ds*) __INTRODUCED_IN_FUTURE;
int shmdt(const void*) __INTRODUCED_IN_FUTURE;
int shmget(key_t, size_t, int) __INTRODUCED_IN_FUTURE;
__END_DECLS
#endif

View file

@ -59,6 +59,11 @@ kernel_token_replacements = {
"ARG_MAX": "_KERNEL_ARG_MAX",
# The kernel usage of __unused for unused struct fields conflicts with the macro defined in <sys/cdefs.h>.
"__unused": "__linux_unused",
# The non-64 stuff is legacy; msqid64_ds/ipc64_perm is what userspace wants.
"msqid_ds": "__kernel_legacy_msqid_ds",
"semid_ds": "__kernel_legacy_semid_ds",
"shmid_ds": "__kernel_legacy_shmid_ds",
"ipc_perm": "__kernel_legacy_ipc_perm",
# The kernel's _NSIG/NSIG are one less than the userspace value, so we need to move them aside.
"_NSIG": "_KERNEL__NSIG",
"NSIG": "_KERNEL_NSIG",

View file

@ -21,7 +21,7 @@
#include <linux/types.h>
#define IPC_PRIVATE ((__kernel_key_t) 0)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
struct ipc_perm {
struct __kernel_legacy_ipc_perm {
__kernel_key_t key;
__kernel_uid_t uid;
__kernel_gid_t gid;

View file

@ -26,8 +26,8 @@
#define MSG_EXCEPT 020000
#define MSG_COPY 040000
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
struct msqid_ds {
struct ipc_perm msg_perm;
struct __kernel_legacy_msqid_ds {
struct __kernel_legacy_ipc_perm msg_perm;
struct msg * msg_first;
struct msg * msg_last;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */

View file

@ -32,8 +32,8 @@
#define SEM_STAT 18
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define SEM_INFO 19
struct semid_ds {
struct ipc_perm sem_perm;
struct __kernel_legacy_semid_ds {
struct __kernel_legacy_ipc_perm sem_perm;
__kernel_time_t sem_otime;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
__kernel_time_t sem_ctime;
@ -54,7 +54,7 @@ struct sembuf {
};
union semun {
int val;
struct semid_ds __user * buf;
struct __kernel_legacy_semid_ds __user * buf;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
unsigned short __user * array;
struct seminfo __user * __buf;

View file

@ -28,8 +28,8 @@
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define SHMALL (ULONG_MAX - (1UL << 24))
#define SHMSEG SHMMNI
struct shmid_ds {
struct ipc_perm shm_perm;
struct __kernel_legacy_shmid_ds {
struct __kernel_legacy_ipc_perm shm_perm;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
int shm_segsz;
__kernel_time_t shm_atime;

View file

@ -1284,11 +1284,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1206,11 +1206,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1309,11 +1309,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1268,11 +1268,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1206,11 +1206,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1266,11 +1266,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -1206,11 +1206,23 @@ LIBC_O {
hasmntopt; # future
lutimes; # future
mblen; # future
msgctl; # future
msgget; # future
msgrcv; # future
msgsnd; # future
pthread_getname_np; # future
quotactl; # future
semctl; # future
semget; # future
semop; # future
semtimedop; # future
setdomainname; # future
setgrent; # future
setpwent; # future
shmat; # future
shmctl; # future
shmdt; # future
shmget; # future
sighold; # future
sigignore; # future
sigpause; # future

View file

@ -107,6 +107,7 @@ cc_test_library {
"sstream_test.cpp",
"sys_epoll_test.cpp",
"sys_mman_test.cpp",
"sys_msg_test.cpp",
"sys_personality_test.cpp",
"sys_prctl_test.cpp",
"sys_procfs_test.cpp",
@ -114,7 +115,9 @@ cc_test_library {
"sys_quota_test.cpp",
"sys_resource_test.cpp",
"sys_select_test.cpp",
"sys_sem_test.cpp",
"sys_sendfile_test.cpp",
"sys_shm_test.cpp",
"sys_socket_test.cpp",
"sys_stat_test.cpp",
"sys_statvfs_test.cpp",

94
tests/sys_msg_test.cpp Normal file
View file

@ -0,0 +1,94 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <sys/msg.h>
#include "TemporaryFile.h"
TEST(sys_msg, smoke) {
// Create a queue.
TemporaryDir dir;
key_t key = ftok(dir.dirname, 1);
int id = msgget(key, IPC_CREAT|0666);
ASSERT_NE(id, -1);
// Queue should be empty.
msqid_ds ds;
memset(&ds, 0, sizeof(ds));
ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
ASSERT_EQ(0U, ds.msg_qnum);
ASSERT_EQ(0U, ds.msg_cbytes);
// Send a message.
struct {
long type;
char data[32];
} msg = { 1, "hello world" };
ASSERT_EQ(0, msgsnd(id, &msg, sizeof(msg), 0));
// Queue should be non-empty.
ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
ASSERT_EQ(1U, ds.msg_qnum);
ASSERT_EQ(sizeof(msg), ds.msg_cbytes);
// Read the message.
memset(&msg, 0, sizeof(msg));
ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), msgrcv(id, &msg, sizeof(msg), 0, 0));
ASSERT_EQ(1, msg.type);
ASSERT_STREQ("hello world", msg.data);
// Destroy the queue.
ASSERT_EQ(0, msgctl(id, IPC_RMID, 0));
}
TEST(sys_msg, msgctl_failure) {
errno = 0;
ASSERT_EQ(-1, msgctl(-1, IPC_STAT, nullptr));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_msg, msgget_failure) {
errno = 0;
ASSERT_EQ(-1, msgget(-1, 0));
ASSERT_EQ(ENOENT, errno);
}
TEST(sys_msg, msgrcv_failure) {
errno = 0;
ASSERT_EQ(-1, msgrcv(-1, nullptr, 0, 0, 0));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_msg, msgsnd_failure) {
errno = 0;
ASSERT_EQ(-1, msgsnd(-1, "", 0, 0));
ASSERT_EQ(EINVAL, errno);
}

95
tests/sys_sem_test.cpp Normal file
View file

@ -0,0 +1,95 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <sys/sem.h>
#include "TemporaryFile.h"
TEST(sys_sem, smoke) {
// Create a semaphore.
TemporaryDir dir;
key_t key = ftok(dir.dirname, 1);
int id = semget(key, 1, IPC_CREAT|0666);
ASSERT_NE(id, -1);
// Check semaphore info.
semid_ds ds;
memset(&ds, 0, sizeof(ds));
ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
ASSERT_EQ(1U, ds.sem_nsems);
ASSERT_EQ(0, semctl(id, 0, GETVAL));
// Increment.
sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
ASSERT_EQ(0, semop(id, ops, 1));
ASSERT_EQ(1, semctl(id, 0, GETVAL));
// Test timeouts.
timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
errno = 0;
ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
ASSERT_EQ(EAGAIN, errno);
ASSERT_EQ(1, semctl(id, 0, GETVAL));
// Decrement.
ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
ASSERT_EQ(0, semop(id, ops, 1));
ASSERT_EQ(0, semctl(id, 0, GETVAL));
// Destroy the semaphore.
ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
}
TEST(sys_sem, semget_failure) {
errno = 0;
ASSERT_EQ(-1, semget(-1, -1, 0));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_sem, semctl_failure) {
errno = 0;
ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_sem, semop_failure) {
errno = 0;
ASSERT_EQ(-1, semop(-1, nullptr, 0));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_sem, semtimedop_failure) {
errno = 0;
ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
ASSERT_EQ(EINVAL, errno);
}

82
tests/sys_shm_test.cpp Normal file
View file

@ -0,0 +1,82 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <sys/shm.h>
#include "TemporaryFile.h"
TEST(sys_shm, smoke) {
// Create a segment.
TemporaryDir dir;
key_t key = ftok(dir.dirname, 1);
int id = shmget(key, 1234, IPC_CREAT|0666);
ASSERT_NE(id, -1);
// Check segment info.
shmid_ds ds;
memset(&ds, 0, sizeof(ds));
ASSERT_EQ(0, shmctl(id, IPC_STAT, &ds));
ASSERT_EQ(1234U, ds.shm_segsz);
// Attach.
void* p = shmat(id, 0, SHM_RDONLY);
ASSERT_NE(p, nullptr);
// Detach.
ASSERT_EQ(0, shmdt(p));
// Destroy the segment.
ASSERT_EQ(0, shmctl(id, IPC_RMID, 0));
}
TEST(sys_shm, shmat_failure) {
errno = 0;
ASSERT_EQ(reinterpret_cast<void*>(-1), shmat(-1, 0, SHM_RDONLY));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_shm, shmctl_failure) {
errno = 0;
ASSERT_EQ(-1, shmctl(-1, IPC_STAT, nullptr));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_shm, shmdt_failure) {
errno = 0;
ASSERT_EQ(-1, shmdt(nullptr));
ASSERT_EQ(EINVAL, errno);
}
TEST(sys_shm, shmget_failure) {
errno = 0;
ASSERT_EQ(-1, shmget(-1, 1234, 0));
ASSERT_EQ(ENOENT, errno);
}