blob: eb3d0e7510acd7f9b227153ae7d442669b6676de [file] [log] [blame]
subrata_modakfd49ff62009-06-15 18:26:34 +00001/*
2 * eventfd-sem by Davide Libenzi (Simple test for eventfd sempahore)
3 * Copyright (C) 2009 Davide Libenzi
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 * Reference: http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=bcd0b235bf3808dec5115c381cd55568f63b85f0
21 * Reference: http://www.xmailserver.org/eventfd-sem.c
22 * eventfd: testing improved support for semaphore-like behavior in linux-2.6.30
23 *
24 */
25
26#include <sys/types.h>
27#include <sys/syscall.h>
28#include <sys/stat.h>
yaberauneyae1900272009-07-22 05:16:11 +000029#include <sys/wait.h>
subrata_modakfd49ff62009-06-15 18:26:34 +000030#include <fcntl.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <unistd.h>
35#include <errno.h>
yaberauneyad3fe8f62009-07-22 07:54:04 +000036#include <inttypes.h>
subrata_modakfd49ff62009-06-15 18:26:34 +000037
subrata_modak6333e192009-07-06 14:49:17 +000038#include "test.h"
39#include "linux_syscall_numbers.h"
40
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020041char *TCID = "eventfd2_03";
42int TST_TOTAL = 1;
subrata_modakfd49ff62009-06-15 18:26:34 +000043
44#ifndef EFD_SEMLIKE
45#define EFD_SEMLIKE (1 << 0)
46#endif
47
yaberauneyae1900272009-07-22 05:16:11 +000048/* Dummy function as syscall from linux_syscall_numbers.h uses cleanup(). */
Mike Frysingerc57fba52014-04-09 18:56:30 -040049void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080050{
51}
subrata_modakfd49ff62009-06-15 18:26:34 +000052
Wanlong Gao354ebb42012-12-07 10:10:04 +080053static int eventfd2(int count, int flags)
54{
Jan Stancek359980f2013-02-15 10:16:05 +010055 return ltp_syscall(__NR_eventfd2, count, flags);
subrata_modakfd49ff62009-06-15 18:26:34 +000056}
57
Wanlong Gao354ebb42012-12-07 10:10:04 +080058static void xsem_wait(int fd)
59{
subrata_modakfd49ff62009-06-15 18:26:34 +000060 u_int64_t cntr;
61
62 if (read(fd, &cntr, sizeof(cntr)) != sizeof(cntr)) {
63 perror("reading eventfd");
64 exit(1);
65 }
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 fprintf(stdout, "[%u] wait completed on %d: count=%" PRIu64 "\n",
subrata_modakfd49ff62009-06-15 18:26:34 +000067 getpid(), fd, cntr);
68}
69
Wanlong Gao354ebb42012-12-07 10:10:04 +080070static void xsem_post(int fd, int count)
71{
subrata_modakfd49ff62009-06-15 18:26:34 +000072 u_int64_t cntr = count;
73
74 if (write(fd, &cntr, sizeof(cntr)) != sizeof(cntr)) {
75 perror("writing eventfd");
76 exit(1);
77 }
78}
79
Wanlong Gao354ebb42012-12-07 10:10:04 +080080static void sem_player(int fd1, int fd2)
81{
subrata_modakfd49ff62009-06-15 18:26:34 +000082 fprintf(stdout, "[%u] posting 1 on %d\n", getpid(), fd1);
83 xsem_post(fd1, 1);
84
85 fprintf(stdout, "[%u] waiting on %d\n", getpid(), fd2);
86 xsem_wait(fd2);
87
88 fprintf(stdout, "[%u] posting 1 on %d\n", getpid(), fd1);
89 xsem_post(fd1, 1);
90
91 fprintf(stdout, "[%u] waiting on %d\n", getpid(), fd2);
92 xsem_wait(fd2);
93
94 fprintf(stdout, "[%u] posting 5 on %d\n", getpid(), fd1);
95 xsem_post(fd1, 5);
96
97 fprintf(stdout, "[%u] waiting 5 times on %d\n", getpid(), fd2);
98 xsem_wait(fd2);
99 xsem_wait(fd2);
100 xsem_wait(fd2);
101 xsem_wait(fd2);
102 xsem_wait(fd2);
103}
104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105static void usage(char const *prg)
106{
subrata_modakfd49ff62009-06-15 18:26:34 +0000107 fprintf(stderr, "use: %s [-h]\n", prg);
108}
109
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110int main(int argc, char **argv)
111{
subrata_modakfd49ff62009-06-15 18:26:34 +0000112 int c, fd1, fd2, status;
113 pid_t cpid_poster, cpid_waiter;
114
115 while ((c = getopt(argc, argv, "h")) != -1) {
116 switch (c) {
117 default:
118 usage(argv[0]);
119 return 1;
120 }
121 }
subrata_modak6333e192009-07-06 14:49:17 +0000122 if ((tst_kvercmp(2, 6, 27)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100123 tst_brkm(TCONF,
124 NULL,
subrata_modak6333e192009-07-06 14:49:17 +0000125 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak6333e192009-07-06 14:49:17 +0000126 }
subrata_modakfd49ff62009-06-15 18:26:34 +0000127 if ((fd1 = eventfd2(0, EFD_SEMLIKE)) == -1 ||
128 (fd2 = eventfd2(0, EFD_SEMLIKE)) == -1) {
129 perror("eventfd2");
130 return 1;
131 }
132 if ((cpid_poster = fork()) == 0) {
133 sem_player(fd1, fd2);
134 exit(0);
135 }
136 if ((cpid_waiter = fork()) == 0) {
137 sem_player(fd2, fd1);
138 exit(0);
139 }
140 waitpid(cpid_poster, &status, 0);
141 waitpid(cpid_waiter, &status, 0);
142
Garrett Cooper2c282152010-12-16 00:55:50 -0800143 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700144}