Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * a race in pid generation that causes pids to be reused immediately |
| 3 | * |
| 4 | * From the mainline commit 5fdee8c4a5e1800489ce61963208f8cc55e42ea1: |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 5 | * |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 6 | * A program that repeatedly forks and waits is susceptible to having |
| 7 | * the same pid repeated, especially when it competes with another |
| 8 | * instance of the same program. This is really bad for bash |
| 9 | * implementation. Furthermore, many shell scripts assume that pid |
| 10 | * numbers will not be used for some length of time. |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 11 | * |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 12 | * Race Description: |
| 13 | * |
| 14 | * A B |
| 15 | * |
| 16 | * // pid == offset == n // pid == offset == n + 1 |
| 17 | * test_and_set_bit(offset, map->page) |
| 18 | * test_and_set_bit(offset, map->page); |
| 19 | * pid_ns->last_pid = pid; |
| 20 | * pid_ns->last_pid = pid; |
| 21 | * // pid == n + 1 is freed (wait()) |
| 22 | * |
| 23 | * // Next fork()... |
| 24 | * last = pid_ns->last_pid; // == n |
| 25 | * pid = last + 1; |
| 26 | * |
| 27 | * Copyright (C) 2010 Red Hat, Inc. |
| 28 | * This program is free software; you can redistribute it and/or |
| 29 | * modify it under the terms of version 2 of the GNU General Public |
| 30 | * License as published by the Free Software Foundation. |
| 31 | * |
| 32 | * This program is distributed in the hope that it would be useful, |
| 33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 35 | * |
| 36 | * Further, this software is distributed without any warranty that it |
| 37 | * is free of the rightful claim of any third person regarding |
| 38 | * infringement or the like. Any license provided herein, whether |
| 39 | * implied or otherwise, applies only to this software file. Patent |
| 40 | * licenses, if any, provided herein do not apply to combinations of |
| 41 | * this program with other software, or any other product whatsoever. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License |
| 44 | * along with this program; if not, write the Free Software |
| 45 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 46 | * 02110-1301, USA. |
| 47 | */ |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 48 | |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
| 51 | #include <sys/wait.h> |
| 52 | #include <fcntl.h> |
| 53 | #include <errno.h> |
| 54 | #include <unistd.h> |
| 55 | #include <stdio.h> |
| 56 | #include <stdlib.h> |
| 57 | #include "test.h" |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 58 | |
| 59 | char *TCID = "fork13"; |
| 60 | int TST_TOTAL = 1; |
| 61 | |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 62 | static unsigned long pid_max; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 63 | |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 64 | #define PID_MAX_PATH "/proc/sys/kernel/pid_max" |
| 65 | #define PID_MAX 32768 |
| 66 | #define RETURN 256 |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 67 | |
| 68 | static void setup(void); |
| 69 | static int pid_distance(pid_t first, pid_t second); |
| 70 | static void cleanup(void); |
| 71 | static void check(void); |
| 72 | |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 73 | int main(int argc, char *argv[]) |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 74 | { |
| 75 | /* message returned from parse_opts */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 76 | const char *msg; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 77 | |
| 78 | msg = parse_opts(argc, argv, NULL, NULL); |
| 79 | if (msg != NULL) |
| 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 81 | setup(); |
| 82 | check(); |
| 83 | cleanup(); |
| 84 | tst_exit(); |
| 85 | } |
| 86 | |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 87 | static void check(void) |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 88 | { |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 89 | long lc; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 90 | pid_t last_pid = 0; |
| 91 | pid_t pid; |
| 92 | int child_exit_code, distance, reaped, status; |
| 93 | |
| 94 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 96 | child_exit_code = lc % RETURN; |
| 97 | switch (pid = fork()) { |
| 98 | case -1: |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 99 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 100 | case 0: |
| 101 | exit(child_exit_code); |
| 102 | default: |
| 103 | if (lc > 0) { |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 104 | distance = pid_distance(last_pid, pid); |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 105 | if (distance == 0) { |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 106 | tst_resm(TFAIL, |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 107 | "Unexpected pid sequence: " |
| 108 | "previous fork: pid=%d, " |
| 109 | "current fork: pid=%d for " |
| 110 | "iteration=%ld.", last_pid, |
| 111 | pid, lc); |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 114 | } |
| 115 | last_pid = pid; |
| 116 | |
tangchen | 4cb537a | 2011-06-10 13:46:50 -0400 | [diff] [blame] | 117 | reaped = waitpid(pid, &status, 0); |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 118 | if (reaped != pid) { |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 119 | tst_resm(TFAIL, |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 120 | "Wait return value: expected pid=%d, " |
| 121 | "got %d, iteration %ld.", pid, reaped, |
| 122 | lc); |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 123 | return; |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 124 | } else if (WEXITSTATUS(status) != child_exit_code) { |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 125 | tst_resm(TFAIL, "Unexpected exit status %x, " |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 126 | "iteration %ld.", WEXITSTATUS(status), |
| 127 | lc); |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 132 | tst_resm(TPASS, "%ld pids forked, all passed", lc); |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 135 | static void setup(void) |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 136 | { |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 137 | tst_require_root(NULL); |
| 138 | |
| 139 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 140 | TEST_PAUSE; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 141 | |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 142 | /* Backup pid_max value. */ |
| 143 | SAFE_FILE_SCANF(NULL, PID_MAX_PATH, "%lu", &pid_max); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 145 | SAFE_FILE_PRINTF(NULL, PID_MAX_PATH, "%d", PID_MAX); |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 148 | static void cleanup(void) |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 149 | { |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 150 | /* Restore pid_max value. */ |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 151 | FILE_PRINTF(PID_MAX_PATH, "%lu", pid_max); |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Garrett Cooper | 3f0688d | 2011-03-04 01:30:50 -0800 | [diff] [blame] | 154 | /* The distance mod PIDMAX between two pids, where the first pid is |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 155 | expected to be smaller than the second. */ |
Wanlong Gao | 8b71829 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 156 | static int pid_distance(pid_t first, pid_t second) |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 157 | { |
Cyril Hrubis | 2664913 | 2012-11-27 19:42:50 +0100 | [diff] [blame] | 158 | return (second + PID_MAX - first) % PID_MAX; |
Garrett Cooper | 205be29 | 2011-01-19 01:01:38 -0800 | [diff] [blame] | 159 | } |