blob: 713d699d12afe021c52d0e4a36c278b4280c2f1d [file] [log] [blame]
subrata_modak9a0748e2008-05-20 10:15:07 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
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
13 * the 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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak9a0748e2008-05-20 10:15:07 +000018 */
19
20/*
21 * NAME
22 * libtestsuite.c
23 *
24 * DESCRIPTION
25 * file containing generic routines which are used by some of the LTP
26 * testsuite tests. Currently, the following routines are present in
27 * this library:
28 *
29 * my_getpwnam(), do_file_setup()
30 *
31 * HISTORY
32 * 11/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
33 * - Add the following functions to synchronise father and sons processes
34 * sync_pipe_create(), sync_pipe_wait(), sync_pipe_notify()
35 */
36#include <unistd.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <pwd.h>
40#include <errno.h>
subrata_modak9a0748e2008-05-20 10:15:07 +000041
subrata_modak17d385a2009-01-22 09:44:16 +000042#include "libtestsuite.h"
subrata_modak9a0748e2008-05-20 10:15:07 +000043#include "test.h"
44#include "usctest.h"
45
Wanlong Gao354ebb42012-12-07 10:10:04 +080046struct passwd *my_getpwnam(char *name)
subrata_modak9a0748e2008-05-20 10:15:07 +000047{
48 struct passwd *saved_pwent;
49 struct passwd *pwent;
50
51 if ((pwent = getpwnam(name)) == NULL) {
52 perror("getpwnam");
53 tst_brkm(TBROK, NULL, "getpwnam() failed");
54 }
55 saved_pwent = (struct passwd *)malloc(sizeof(struct passwd));
56
57 *saved_pwent = *pwent;
58
Wanlong Gao354ebb42012-12-07 10:10:04 +080059 return (saved_pwent);
subrata_modak9a0748e2008-05-20 10:15:07 +000060}
61
Wanlong Gao354ebb42012-12-07 10:10:04 +080062void do_file_setup(char *fname)
subrata_modak9a0748e2008-05-20 10:15:07 +000063{
64 int fd;
65
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
subrata_modak9a0748e2008-05-20 10:15:07 +000067 tst_resm(TBROK, "open(%s, O_RDWR|O_CREAT,0700) Failed, "
68 "errno=%d : %s", fname, errno, strerror(errno));
69 }
70
71 if (close(fd) == -1) {
72 tst_resm(TWARN, "close(%s) Failed on file create, errno=%d : "
73 "%s", fname, errno, strerror(errno));
74 }
75}
76
subrata_modak17d385a2009-01-22 09:44:16 +000077static char pipe_name[256];
78
subrata_modak8d4fee72009-01-27 14:48:38 +000079void generate_pipe_name(const char *name)
subrata_modak9a0748e2008-05-20 10:15:07 +000080{
subrata_modak17d385a2009-01-22 09:44:16 +000081 char *p;
82
vapiera0604922009-07-20 10:58:09 +000083 p = strrchr(name, '/');
subrata_modak17d385a2009-01-22 09:44:16 +000084 if (p == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 p = (char *)name;
subrata_modak17d385a2009-01-22 09:44:16 +000086 else
87 p++;
88 snprintf(pipe_name, 255, "%s/ltp_fifo_%s", P_tmpdir, p);
subrata_modak9a0748e2008-05-20 10:15:07 +000089}
90
subrata_modak17d385a2009-01-22 09:44:16 +000091int sync_pipe_create(int fd[], const char *name)
92{
93 int ret;
94
95 if (name != NULL) {
96 generate_pipe_name(name);
97
98 ret = open(pipe_name, O_RDWR);
99 if (ret != -1) {
100 fd[0] = ret;
101 fd[1] = open(pipe_name, O_RDWR);
102 } else {
103 ret = mkfifo(pipe_name, S_IRWXU);
104 if (!ret) {
105 fd[0] = open(pipe_name, O_RDWR);
106 fd[1] = open(pipe_name, O_RDWR);
107 }
108 }
109 return ret;
110 } else
111 return pipe(fd);
112}
113
114int sync_pipe_close(int fd[], const char *name)
subrata_modak9a0748e2008-05-20 10:15:07 +0000115{
subrata_modak4f766032008-09-04 10:04:27 +0000116 int r = 0;
subrata_modak44fc4c12008-06-03 13:54:37 +0000117
118 if (fd[0] != -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 r = close(fd[0]);
subrata_modak44fc4c12008-06-03 13:54:37 +0000120 if (fd[1] != -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 r |= close(fd[1]);
subrata_modak17d385a2009-01-22 09:44:16 +0000122
123 if (name != NULL) {
124 generate_pipe_name(name);
125 unlink(pipe_name);
126 }
subrata_modak44fc4c12008-06-03 13:54:37 +0000127 return r;
subrata_modak9a0748e2008-05-20 10:15:07 +0000128}
129
130int sync_pipe_wait(int fd[])
131{
132 char buf;
133 int r;
subrata_modak44fc4c12008-06-03 13:54:37 +0000134
135 if (fd[1] != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 close(fd[1]);
subrata_modak44fc4c12008-06-03 13:54:37 +0000137 fd[1] = -1;
138 }
vapier45a8ba02009-07-20 10:59:32 +0000139
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 r = read(fd[0], &buf, 1);
vapier45a8ba02009-07-20 10:59:32 +0000141
subrata_modak9a0748e2008-05-20 10:15:07 +0000142 if ((r != 1) || (buf != 'A'))
143 return -1;
144 return 0;
145}
146
147int sync_pipe_notify(int fd[])
148{
149 char buf = 'A';
150 int r;
151
subrata_modak44fc4c12008-06-03 13:54:37 +0000152 if (fd[0] != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 close(fd[0]);
subrata_modak44fc4c12008-06-03 13:54:37 +0000154 fd[0] = -1;
155 }
156
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 r = write(fd[1], &buf, 1);
subrata_modak9a0748e2008-05-20 10:15:07 +0000158
159 if (r != 1)
160 return -1;
161 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700162}