Home | History | Annotate | Download | only in open
      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
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * DESCRIPTION
     22  *	Testcase to check open(2) sets errno to ENXIO correctly.
     23  *
     24  * ALGORITHM
     25  *	Create a named pipe using mknod(2).  Attempt to
     26  *	open(2) the pipe for writing. The open(2) should
     27  *	fail with ENXIO.
     28  */
     29 #include <sys/types.h>
     30 #include <sys/stat.h>
     31 #include <fcntl.h>
     32 #include <errno.h>
     33 #include <unistd.h>
     34 #include "test.h"
     35 
     36 char *TCID = "open06";
     37 int TST_TOTAL = 1;
     38 
     39 static void setup(void);
     40 static void cleanup(void);
     41 
     42 static char fname[100] = "fifo";
     43 
     44 int main(int ac, char **av)
     45 {
     46 	int lc;
     47 
     48 	tst_parse_opts(ac, av, NULL, NULL);
     49 
     50 	setup();
     51 
     52 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     53 		tst_count = 0;
     54 
     55 		TEST(open(fname, O_NONBLOCK | O_WRONLY));
     56 		if (TEST_RETURN != -1) {
     57 			tst_resm(TFAIL, "open(2) succeeded unexpectedly");
     58 			continue;
     59 		}
     60 
     61 		if (TEST_ERRNO != ENXIO)
     62 			tst_resm(TFAIL, "Expected ENXIO got %d", TEST_ERRNO);
     63 		else
     64 			tst_resm(TPASS, "call returned expected ENXIO error");
     65 	}
     66 
     67 	cleanup();
     68 	tst_exit();
     69 }
     70 
     71 static void setup(void)
     72 {
     73 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
     74 
     75 	TEST_PAUSE;
     76 
     77 	tst_tmpdir();
     78 
     79 	sprintf(fname, "%s.%d", fname, getpid());
     80 
     81 	if (mknod(fname, S_IFIFO | 0644, 0) == -1)
     82 		tst_brkm(TBROK, cleanup, "mknod FAILED");
     83 }
     84 
     85 static void cleanup(void)
     86 {
     87 	unlink(fname);
     88 
     89 	tst_rmdir();
     90 }
     91