subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Bull S.A.S. 2008 |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 10 | * the GNU General Public License for more details. |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 13 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | * File: pidns31.c |
| 17 | * |
| 18 | * Description: |
| 19 | * This testcase checks if the si_pid is correctly set when a process |
| 20 | * that has registered for notification on a posix mqueue is in an |
| 21 | * ancestor namespace wrt the process that sends a message to that posix |
| 22 | * mqueue. |
| 23 | * |
| 24 | * Test Assertion & Strategy: |
| 25 | * Parent Child |
| 26 | * -------------------------------------------------------------------------- |
| 27 | * Create a POSIX mqueue. |
| 28 | * Create a PID namespace container. |
| 29 | * Register for notification when a |
| 30 | * message arrives in that mqueue |
| 31 | * Install a handler for SIGUSR1. |
| 32 | * Open that mqueue for writing |
| 33 | * Write something to the mqueue. |
| 34 | * Inside the handler, check that |
| 35 | * si_pid is set to the child's pid |
| 36 | * |
| 37 | * Usage: <for command-line> |
| 38 | * pidns31 |
| 39 | * |
| 40 | * History: |
| 41 | * DATE NAME DESCRIPTION |
| 42 | * 04/12/08 Nadia Derbey Creation of this test. |
| 43 | * <Nadia.Derbey@bull.net> |
| 44 | * |
| 45 | ******************************************************************************/ |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 46 | #ifndef _GNU_SOURCE |
| 47 | #define _GNU_SOURCE |
| 48 | #endif |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 49 | #include <sys/wait.h> |
| 50 | #include <sys/types.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <unistd.h> |
| 54 | #include <stdio.h> |
| 55 | #include <mqueue.h> |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 56 | #include "test.h" |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 57 | #include "linux_syscall_numbers.h" |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 58 | #include "libclone.h" |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 59 | #include "pidns_helper.h" |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 60 | |
| 61 | char *TCID = "pidns31"; |
| 62 | int TST_TOTAL = 1; |
| 63 | |
Garrett Cooper | cb2dde7 | 2010-11-11 00:38:43 -0800 | [diff] [blame] | 64 | char *mqname = "mq1"; |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 65 | int result = TFAIL; |
| 66 | |
| 67 | int errno; |
| 68 | int father_to_child[2]; |
| 69 | |
| 70 | #define CHILD_PID 1 |
| 71 | #define PARENT_PID 0 |
| 72 | |
| 73 | #define MSG "HOW ARE YOU" |
| 74 | #define MSG_PRIO 1 |
| 75 | |
| 76 | #define NO_STEP -1 |
| 77 | #define F_STEP_0 0x00 |
| 78 | #define F_STEP_1 0x01 |
| 79 | #define F_STEP_2 0x02 |
| 80 | #define F_STEP_3 0x03 |
| 81 | #define C_STEP_0 0x10 |
| 82 | #define C_STEP_1 0x11 |
| 83 | |
| 84 | struct notify_info { |
| 85 | mqd_t mqd; |
| 86 | pid_t pid; |
| 87 | }; |
| 88 | |
| 89 | static void remove_pipe(int *fd) |
| 90 | { |
| 91 | close(fd[0]); |
| 92 | close(fd[1]); |
| 93 | } |
| 94 | |
| 95 | static void remove_mqueue(mqd_t mqd) |
| 96 | { |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 97 | mq_close(mqd); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 98 | ltp_syscall(__NR_mq_unlink, mqname); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * steps F_STEP_XX : called from main |
| 103 | * steps C_STEP_XX : called from child_fn |
| 104 | */ |
| 105 | static void cleanup_resources(int step, mqd_t mqd) |
| 106 | { |
| 107 | switch (step) { |
| 108 | case C_STEP_1: |
| 109 | close(father_to_child[0]); |
| 110 | /* fall through */ |
| 111 | case C_STEP_0: |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 112 | mq_close(mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case F_STEP_3: |
| 116 | remove_mqueue(mqd); |
| 117 | close(father_to_child[1]); |
| 118 | break; |
| 119 | |
| 120 | case F_STEP_2: |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 121 | ltp_syscall(__NR_mq_notify, mqd, NULL); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 122 | /* fall through */ |
| 123 | case F_STEP_1: |
| 124 | remove_mqueue(mqd); |
| 125 | /* fall through */ |
| 126 | case F_STEP_0: |
| 127 | remove_pipe(father_to_child); |
| 128 | break; |
| 129 | default: |
| 130 | tst_resm(TWARN, "Unknown code - no resource removed."); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 136 | * cleanup_mqueue() - performs all ONE TIME cleanup for this test at |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 137 | * completion or premature exit. |
| 138 | * step == -1 means no local resource to remove. |
| 139 | */ |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 140 | void cleanup_mqueue(int result, int step, mqd_t mqd) |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 141 | { |
| 142 | if (step != NO_STEP) |
| 143 | cleanup_resources(step, mqd); |
| 144 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 145 | tst_exit(); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * child_fn() - Inside container |
| 150 | */ |
| 151 | int child_fn(void *arg) |
| 152 | { |
| 153 | pid_t pid, ppid; |
| 154 | mqd_t mqd; |
| 155 | char buf[5]; |
| 156 | |
| 157 | /* Set process id and parent pid */ |
| 158 | pid = getpid(); |
| 159 | ppid = getppid(); |
| 160 | |
| 161 | if (pid != CHILD_PID || ppid != PARENT_PID) { |
| 162 | tst_resm(TBROK, "cinit: pidns is not created"); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 163 | cleanup_mqueue(TBROK, NO_STEP, 0); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Close the appropriate end of pipe */ |
| 167 | close(father_to_child[1]); |
| 168 | |
| 169 | /* Is parent ready to receive a message? */ |
| 170 | read(father_to_child[0], buf, 5); |
| 171 | if (strcmp(buf, "f:ok")) { |
| 172 | tst_resm(TBROK, "cinit: parent did not send the message!"); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 173 | cleanup_mqueue(TBROK, NO_STEP, 0); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 174 | } |
| 175 | tst_resm(TINFO, "cinit: my father is ready to receive a message"); |
| 176 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 177 | mqd = ltp_syscall(__NR_mq_open, mqname, O_WRONLY, 0, NULL); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | if (mqd == (mqd_t) - 1) { |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 179 | tst_resm(TBROK, "cinit: mq_open() failed (%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 181 | cleanup_mqueue(TBROK, NO_STEP, 0); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 182 | } |
| 183 | tst_resm(TINFO, "cinit: mq_open succeeded"); |
| 184 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | if (mq_send(mqd, MSG, strlen(MSG), MSG_PRIO) == (mqd_t) - 1) { |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 186 | tst_resm(TBROK, "cinit: mq_send() failed (%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 188 | cleanup_mqueue(TBROK, C_STEP_0, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 189 | } |
| 190 | tst_resm(TINFO, "cinit: mq_send() succeeded"); |
| 191 | |
| 192 | /* Cleanup and exit */ |
| 193 | cleanup_resources(C_STEP_1, mqd); |
| 194 | exit(0); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * father_signal_handler() |
| 199 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 200 | static void father_signal_handler(int sig, siginfo_t * si, void *unused) |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 201 | { |
| 202 | char buf[256]; |
| 203 | struct mq_attr attr; |
| 204 | struct notify_info *info; |
| 205 | |
| 206 | if (si->si_signo != SIGUSR1) { |
| 207 | tst_resm(TBROK, "father: received %s unexpectedly", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 208 | strsignal(si->si_signo)); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (si->si_code != SI_MESGQ) { |
| 213 | tst_resm(TBROK, "father: expected signal code SI_MESGQ - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 214 | "Got %d", si->si_code); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (!si->si_ptr) { |
| 219 | tst_resm(TBROK, "father: expected si_ptr - Got NULL"); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | info = (struct notify_info *)si->si_ptr; |
| 224 | |
| 225 | if (si->si_pid != info->pid) { |
| 226 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 227 | "father: expected signal originator PID = %d - Got %d", |
| 228 | info->pid, si->si_pid); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
| 232 | tst_resm(TPASS, "father: signal originator PID = %d", si->si_pid); |
| 233 | result = TPASS; |
| 234 | |
| 235 | /* |
| 236 | * Now read the message - Be silent on errors since this is not the |
| 237 | * test purpose. |
| 238 | */ |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 239 | if (!mq_getattr(info->mqd, &attr)) |
| 240 | mq_receive(info->mqd, buf, attr.mq_msgsize, NULL); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 243 | static void setup(void) |
| 244 | { |
| 245 | tst_require_root(NULL); |
| 246 | check_newpid(); |
| 247 | } |
| 248 | |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 249 | /*********************************************************************** |
| 250 | * M A I N |
| 251 | ***********************************************************************/ |
| 252 | |
| 253 | int main(int argc, char *argv[]) |
| 254 | { |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 255 | pid_t cpid; |
| 256 | mqd_t mqd; |
| 257 | struct sigevent notif; |
| 258 | struct sigaction sa; |
| 259 | int status; |
| 260 | struct notify_info info; |
| 261 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 262 | setup(); |
| 263 | |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 264 | if (pipe(father_to_child) == -1) { |
| 265 | tst_resm(TBROK, "parent: pipe() failed. aborting!"); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 266 | cleanup_mqueue(TBROK, NO_STEP, 0); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 269 | ltp_syscall(__NR_mq_unlink, mqname); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 270 | mqd = |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 271 | ltp_syscall(__NR_mq_open, mqname, O_RDWR | O_CREAT | O_EXCL, 0777, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 272 | NULL); |
| 273 | if (mqd == (mqd_t) - 1) { |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 274 | tst_resm(TBROK, "parent: mq_open() failed (%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 275 | strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 276 | cleanup_mqueue(TBROK, F_STEP_0, 0); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 277 | } |
| 278 | tst_resm(TINFO, "parent: successfully created posix mqueue"); |
| 279 | |
| 280 | /* container creation on PID namespace */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 281 | cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 282 | if (cpid < 0) { |
| 283 | tst_resm(TBROK, "parent: clone() failed(%s)", strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 284 | cleanup_mqueue(TBROK, F_STEP_1, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 285 | } |
| 286 | tst_resm(TINFO, "parent: successfully created child (pid = %d)", cpid); |
| 287 | |
| 288 | /* Register for notification on message arrival */ |
| 289 | notif.sigev_notify = SIGEV_SIGNAL; |
| 290 | notif.sigev_signo = SIGUSR1; |
| 291 | info.mqd = mqd; |
| 292 | info.pid = cpid; |
| 293 | notif.sigev_value.sival_ptr = &info; |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 294 | if (ltp_syscall(__NR_mq_notify, mqd, ¬if) == (mqd_t) -1) { |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 295 | tst_resm(TBROK, "parent: mq_notify() failed (%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 296 | strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 297 | cleanup_mqueue(TBROK, F_STEP_1, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 298 | } |
| 299 | tst_resm(TINFO, "parent: successfully registered for notification"); |
| 300 | |
| 301 | /* Define handler for SIGUSR1 */ |
| 302 | sa.sa_flags = SA_SIGINFO; |
| 303 | sigemptyset(&sa.sa_mask); |
| 304 | sa.sa_sigaction = father_signal_handler; |
| 305 | if (sigaction(SIGUSR1, &sa, NULL) == -1) { |
| 306 | tst_resm(TBROK, "parent: sigaction() failed(%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 307 | strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 308 | cleanup_mqueue(TBROK, F_STEP_2, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 309 | } |
| 310 | tst_resm(TINFO, "parent: successfully registered handler for SIGUSR1"); |
| 311 | |
| 312 | /* Close the appropriate end of pipe */ |
| 313 | close(father_to_child[0]); |
| 314 | |
| 315 | /* Tell the child a message can be sent */ |
| 316 | if (write(father_to_child[1], "f:ok", 5) != 5) { |
| 317 | tst_resm(TBROK, "parent: pipe is broken(%s)", strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 318 | cleanup_mqueue(TBROK, F_STEP_2, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | sleep(3); |
| 322 | |
| 323 | /* Wait for child to finish */ |
| 324 | if (wait(&status) == -1) { |
| 325 | tst_resm(TBROK, "parent: wait() failed(%s)", strerror(errno)); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 326 | cleanup_mqueue(TBROK, F_STEP_1, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 327 | } |
| 328 | |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 329 | cleanup_mqueue(result, F_STEP_3, mqd); |
subrata_modak | 3a14aff | 2008-12-12 13:17:07 +0000 | [diff] [blame] | 330 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 331 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 332 | } |