Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / lib / libtestsuite.c
blob54c1b31553cf20d103b762386fb491b3b4f8990d
1 /*
3 * Copyright (c) International Business Machines Corp., 2001
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.
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.
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
21 * NAME
22 * libtestsuite.c
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:
29 * my_getpwnam(), do_file_setup()
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()
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <pwd.h>
40 #include <errno.h>
42 #include "libtestsuite.h"
43 #include "test.h"
44 #include "usctest.h"
46 struct passwd *
47 my_getpwnam(char *name)
49 struct passwd *saved_pwent;
50 struct passwd *pwent;
52 if ((pwent = getpwnam(name)) == NULL) {
53 perror("getpwnam");
54 tst_brkm(TBROK, NULL, "getpwnam() failed");
56 saved_pwent = (struct passwd *)malloc(sizeof(struct passwd));
58 *saved_pwent = *pwent;
60 return(saved_pwent);
63 void
64 do_file_setup(char *fname)
66 int fd;
68 if ((fd = open(fname,O_RDWR|O_CREAT,0700)) == -1) {
69 tst_resm(TBROK, "open(%s, O_RDWR|O_CREAT,0700) Failed, "
70 "errno=%d : %s", fname, errno, strerror(errno));
73 if (close(fd) == -1) {
74 tst_resm(TWARN, "close(%s) Failed on file create, errno=%d : "
75 "%s", fname, errno, strerror(errno));
79 static char pipe_name[256];
81 void generate_pipe_name(const char *name)
83 char *p;
85 p = strrchr(name, '/');
86 if (p == NULL)
87 p = (char *) name;
88 else
89 p++;
90 snprintf(pipe_name, 255, "%s/ltp_fifo_%s", P_tmpdir, p);
93 int sync_pipe_create(int fd[], const char *name)
95 int ret;
97 if (name != NULL) {
98 generate_pipe_name(name);
100 ret = open(pipe_name, O_RDWR);
101 if (ret != -1) {
102 fd[0] = ret;
103 fd[1] = open(pipe_name, O_RDWR);
104 } else {
105 ret = mkfifo(pipe_name, S_IRWXU);
106 if (!ret) {
107 fd[0] = open(pipe_name, O_RDWR);
108 fd[1] = open(pipe_name, O_RDWR);
111 return ret;
112 } else
113 return pipe(fd);
116 int sync_pipe_close(int fd[], const char *name)
118 int r = 0;
120 if (fd[0] != -1)
121 r = close (fd[0]);
122 if (fd[1] != -1)
123 r |= close (fd[1]);
125 if (name != NULL) {
126 generate_pipe_name(name);
127 unlink(pipe_name);
129 return r;
132 int sync_pipe_wait(int fd[])
134 char buf;
135 int r;
137 if (fd[1] != -1) {
138 close (fd[1]);
139 fd[1] = -1;
142 r = read (fd[0], &buf, 1);
144 if ((r != 1) || (buf != 'A'))
145 return -1;
146 return 0;
149 int sync_pipe_notify(int fd[])
151 char buf = 'A';
152 int r;
154 if (fd[0] != -1) {
155 close (fd[0]);
156 fd[0] = -1;
159 r = write (fd[1], &buf, 1);
161 if (r != 1)
162 return -1;
163 return 0;