blob: 2260ac7bacb6317ee432121da74c870fa8691d96 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis3e5f7902012-12-12 17:28:29 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis3e5f7902012-12-12 17:28:29 +01005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010015 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010021 * Testcase to check creat(2) sets ETXTBSY correctly.
plars865695b2001-08-27 22:15:12 +000022 */
23
Garrett Cooperc585a272010-12-20 03:04:07 -080024#ifndef _GNU_SOURCE
25#define _GNU_SOURCE
26#endif
plars74948ad2002-11-14 16:16:14 +000027#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000028#include <sys/stat.h>
plars4d1ce602001-09-12 16:23:42 +000029#include <sys/wait.h>
Garrett Cooperc585a272010-12-20 03:04:07 -080030#include <errno.h>
31#include <fcntl.h>
32#include <libgen.h>
33#include <stdio.h>
plars865695b2001-08-27 22:15:12 +000034#include "test.h"
plars865695b2001-08-27 22:15:12 +000035
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010036#define TEST_APP "creat07_child"
Garrett Cooper87bc45c2010-12-17 01:56:08 -080037
plars865695b2001-08-27 22:15:12 +000038char *TCID = "creat07";
39int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000040
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010041static void setup(char *);
42static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000043
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010044static struct tst_checkpoint checkpoint;
plars865695b2001-08-27 22:15:12 +000045
subrata_modak56207ce2009-03-23 13:35:39 +000046int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000047{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020048 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020049 const char *msg;
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010050 pid_t pid;
plars865695b2001-08-27 22:15:12 +000051
Garrett Cooper87bc45c2010-12-17 01:56:08 -080052 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080053 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000054
Garrett Cooper87bc45c2010-12-17 01:56:08 -080055 setup(av[0]);
plars865695b2001-08-27 22:15:12 +000056
plars865695b2001-08-27 22:15:12 +000057 for (lc = 0; TEST_LOOPING(lc); lc++) {
58
Caspar Zhangd59a6592013-03-07 14:59:12 +080059 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000060
Garrett Cooper87bc45c2010-12-17 01:56:08 -080061 if ((pid = FORK_OR_VFORK()) == -1)
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010062 tst_brkm(TBROK|TERRNO, cleanup, "fork #1 failed");
plars865695b2001-08-27 22:15:12 +000063
Garrett Cooper87bc45c2010-12-17 01:56:08 -080064 if (pid == 0) {
Salvatore CRO76c4c802012-02-01 15:38:34 +010065 char *av[2];
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010066 av[0] = TEST_APP;
Salvatore CRO76c4c802012-02-01 15:38:34 +010067 av[1] = NULL;
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010068 (void)execve(TEST_APP, av, NULL);
Garrett Cooper87bc45c2010-12-17 01:56:08 -080069 perror("execve failed");
plars865695b2001-08-27 22:15:12 +000070 exit(1);
71 }
72
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010073 TST_CHECKPOINT_PARENT_WAIT(NULL, &checkpoint);
plars865695b2001-08-27 22:15:12 +000074
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010075 TEST(creat(TEST_APP, O_WRONLY));
plars865695b2001-08-27 22:15:12 +000076
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010077 if (TEST_RETURN != -1) {
78 tst_resm(TFAIL, "creat succeeded");
79 } else {
80 if (TEST_ERRNO == ETXTBSY)
81 tst_resm(TPASS, "creat received EXTBSY");
82 else
83 tst_resm(TFAIL | TTERRNO, "creat failed with "
84 "unexpected error");
plars865695b2001-08-27 22:15:12 +000085 }
plars865695b2001-08-27 22:15:12 +000086
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010087 if (kill(pid, SIGKILL) == -1)
88 tst_resm(TINFO | TERRNO, "kill failed");
89
90 if (wait(NULL) == -1)
91 tst_brkm(TBROK|TERRNO, cleanup, "wait failed");
92 }
93
94 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -080095 tst_exit();
plars865695b2001-08-27 22:15:12 +000096}
97
Cyril Hrubis3e5f7902012-12-12 17:28:29 +010098static void setup(char *app)
plars865695b2001-08-27 22:15:12 +000099{
Garrett Cooperfb89b0a2010-12-20 01:58:23 -0800100 tst_sig(FORK, DEF_HANDLER, cleanup);
101
subrata_modak4ce69a52008-11-13 11:28:28 +0000102 tst_tmpdir();
103
Cyril Hrubis3e5f7902012-12-12 17:28:29 +0100104 TST_RESOURCE_COPY(cleanup, TEST_APP, NULL);
105
Jan Stancek4caa6312014-10-06 14:57:56 +0200106 TST_CHECKPOINT_CREATE(&checkpoint);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107
plars865695b2001-08-27 22:15:12 +0000108 TEST_PAUSE;
109}
110
Cyril Hrubis3e5f7902012-12-12 17:28:29 +0100111static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000112{
subrata_modak4ce69a52008-11-13 11:28:28 +0000113 tst_rmdir();
Garrett Cooperfb89b0a2010-12-20 01:58:23 -0800114}