blob: 4a41765feceb33b408dbc223f299cae2af4f042b [file] [log] [blame]
Garrett Cooper205be292011-01-19 01:01:38 -08001/*
2 * a race in pid generation that causes pids to be reused immediately
3 *
4 * From the mainline commit 5fdee8c4a5e1800489ce61963208f8cc55e42ea1:
Wanlong Gao8b718292012-09-18 17:14:01 +08005 *
Garrett Cooper205be292011-01-19 01:01:38 -08006 * 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 Gao8b718292012-09-18 17:14:01 +080011 *
Garrett Cooper205be292011-01-19 01:01:38 -080012 * 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 Gao8b718292012-09-18 17:14:01 +080048
Garrett Cooper205be292011-01-19 01:01:38 -080049#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 Cooper205be292011-01-19 01:01:38 -080058
59char *TCID = "fork13";
60int TST_TOTAL = 1;
61
Cyril Hrubis26649132012-11-27 19:42:50 +010062static unsigned long pid_max;
Garrett Cooper205be292011-01-19 01:01:38 -080063
Cyril Hrubis26649132012-11-27 19:42:50 +010064#define PID_MAX_PATH "/proc/sys/kernel/pid_max"
65#define PID_MAX 32768
66#define RETURN 256
Garrett Cooper205be292011-01-19 01:01:38 -080067
68static void setup(void);
69static int pid_distance(pid_t first, pid_t second);
70static void cleanup(void);
71static void check(void);
72
Wanlong Gao8b718292012-09-18 17:14:01 +080073int main(int argc, char *argv[])
Garrett Cooper205be292011-01-19 01:01:38 -080074{
75 /* message returned from parse_opts */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
Garrett Cooper205be292011-01-19 01:01:38 -080077
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 Gao8b718292012-09-18 17:14:01 +080087static void check(void)
Garrett Cooper205be292011-01-19 01:01:38 -080088{
Garrett Cooper3f0688d2011-03-04 01:30:50 -080089 long lc;
Garrett Cooper205be292011-01-19 01:01:38 -080090 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 Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
Garrett Cooper205be292011-01-19 01:01:38 -080096 child_exit_code = lc % RETURN;
97 switch (pid = fork()) {
98 case -1:
Wanlong Gao8b718292012-09-18 17:14:01 +080099 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Garrett Cooper205be292011-01-19 01:01:38 -0800100 case 0:
101 exit(child_exit_code);
102 default:
103 if (lc > 0) {
Garrett Cooper205be292011-01-19 01:01:38 -0800104 distance = pid_distance(last_pid, pid);
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800105 if (distance == 0) {
Garrett Cooper205be292011-01-19 01:01:38 -0800106 tst_resm(TFAIL,
Wanlong Gao8b718292012-09-18 17:14:01 +0800107 "Unexpected pid sequence: "
108 "previous fork: pid=%d, "
109 "current fork: pid=%d for "
110 "iteration=%ld.", last_pid,
111 pid, lc);
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800112 return;
113 }
Garrett Cooper205be292011-01-19 01:01:38 -0800114 }
115 last_pid = pid;
116
tangchen4cb537a2011-06-10 13:46:50 -0400117 reaped = waitpid(pid, &status, 0);
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800118 if (reaped != pid) {
Garrett Cooper205be292011-01-19 01:01:38 -0800119 tst_resm(TFAIL,
Wanlong Gao8b718292012-09-18 17:14:01 +0800120 "Wait return value: expected pid=%d, "
121 "got %d, iteration %ld.", pid, reaped,
122 lc);
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800123 return;
Wanlong Gao8b718292012-09-18 17:14:01 +0800124 } else if (WEXITSTATUS(status) != child_exit_code) {
Garrett Cooper205be292011-01-19 01:01:38 -0800125 tst_resm(TFAIL, "Unexpected exit status %x, "
Wanlong Gao8b718292012-09-18 17:14:01 +0800126 "iteration %ld.", WEXITSTATUS(status),
127 lc);
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800128 return;
129 }
Garrett Cooper205be292011-01-19 01:01:38 -0800130 }
131 }
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800132 tst_resm(TPASS, "%ld pids forked, all passed", lc);
Garrett Cooper205be292011-01-19 01:01:38 -0800133}
134
Wanlong Gao8b718292012-09-18 17:14:01 +0800135static void setup(void)
Garrett Cooper205be292011-01-19 01:01:38 -0800136{
Garrett Cooper205be292011-01-19 01:01:38 -0800137 tst_require_root(NULL);
138
139 tst_sig(FORK, DEF_HANDLER, cleanup);
140 TEST_PAUSE;
Garrett Cooper205be292011-01-19 01:01:38 -0800141
Cyril Hrubis26649132012-11-27 19:42:50 +0100142 /* Backup pid_max value. */
143 SAFE_FILE_SCANF(NULL, PID_MAX_PATH, "%lu", &pid_max);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144
Cyril Hrubis26649132012-11-27 19:42:50 +0100145 SAFE_FILE_PRINTF(NULL, PID_MAX_PATH, "%d", PID_MAX);
Garrett Cooper205be292011-01-19 01:01:38 -0800146}
147
Wanlong Gao8b718292012-09-18 17:14:01 +0800148static void cleanup(void)
Garrett Cooper205be292011-01-19 01:01:38 -0800149{
Cyril Hrubis26649132012-11-27 19:42:50 +0100150 /* Restore pid_max value. */
Li Wang8bfd7c12014-12-03 08:11:36 -0500151 FILE_PRINTF(PID_MAX_PATH, "%lu", pid_max);
Garrett Cooper205be292011-01-19 01:01:38 -0800152}
153
Garrett Cooper3f0688d2011-03-04 01:30:50 -0800154/* The distance mod PIDMAX between two pids, where the first pid is
Garrett Cooper205be292011-01-19 01:01:38 -0800155 expected to be smaller than the second. */
Wanlong Gao8b718292012-09-18 17:14:01 +0800156static int pid_distance(pid_t first, pid_t second)
Garrett Cooper205be292011-01-19 01:01:38 -0800157{
Cyril Hrubis26649132012-11-27 19:42:50 +0100158 return (second + PID_MAX - first) % PID_MAX;
Garrett Cooper205be292011-01-19 01:01:38 -0800159}