subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 41 | |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 42 | #include "libtestsuite.h" |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 43 | #include "test.h" |
| 44 | #include "usctest.h" |
| 45 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 46 | struct passwd *my_getpwnam(char *name) |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 47 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 59 | return (saved_pwent); |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | void do_file_setup(char *fname) |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 63 | { |
| 64 | int fd; |
| 65 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) { |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 67 | 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_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 77 | static char pipe_name[256]; |
| 78 | |
subrata_modak | 8d4fee7 | 2009-01-27 14:48:38 +0000 | [diff] [blame] | 79 | void generate_pipe_name(const char *name) |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 80 | { |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 81 | char *p; |
| 82 | |
vapier | a060492 | 2009-07-20 10:58:09 +0000 | [diff] [blame] | 83 | p = strrchr(name, '/'); |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 84 | if (p == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | p = (char *)name; |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 86 | else |
| 87 | p++; |
| 88 | snprintf(pipe_name, 255, "%s/ltp_fifo_%s", P_tmpdir, p); |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 91 | int 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 | |
| 114 | int sync_pipe_close(int fd[], const char *name) |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 115 | { |
subrata_modak | 4f76603 | 2008-09-04 10:04:27 +0000 | [diff] [blame] | 116 | int r = 0; |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 117 | |
| 118 | if (fd[0] != -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | r = close(fd[0]); |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 120 | if (fd[1] != -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | r |= close(fd[1]); |
subrata_modak | 17d385a | 2009-01-22 09:44:16 +0000 | [diff] [blame] | 122 | |
| 123 | if (name != NULL) { |
| 124 | generate_pipe_name(name); |
| 125 | unlink(pipe_name); |
| 126 | } |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 127 | return r; |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int sync_pipe_wait(int fd[]) |
| 131 | { |
| 132 | char buf; |
| 133 | int r; |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 134 | |
| 135 | if (fd[1] != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | close(fd[1]); |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 137 | fd[1] = -1; |
| 138 | } |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 139 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | r = read(fd[0], &buf, 1); |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 141 | |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 142 | if ((r != 1) || (buf != 'A')) |
| 143 | return -1; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int sync_pipe_notify(int fd[]) |
| 148 | { |
| 149 | char buf = 'A'; |
| 150 | int r; |
| 151 | |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 152 | if (fd[0] != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | close(fd[0]); |
subrata_modak | 44fc4c1 | 2008-06-03 13:54:37 +0000 | [diff] [blame] | 154 | fd[0] = -1; |
| 155 | } |
| 156 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | r = write(fd[1], &buf, 1); |
subrata_modak | 9a0748e | 2008-05-20 10:15:07 +0000 | [diff] [blame] | 158 | |
| 159 | if (r != 1) |
| 160 | return -1; |
| 161 | return 0; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 162 | } |