Home | History | Annotate | Download | only in fcntl
      1 /*
      2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
      3  * Copyright (c) 2017 Cyril Hrubis <chrubis (at) suse.cz>
      4  *
      5  * This program is free software; you can redistribute it and/or modify it
      6  * under the terms of version 2 of the GNU General Public License as
      7  * published by the Free Software Foundation.
      8  *
      9  * This program is distributed in the hope that it would be useful, but
     10  * WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12  *
     13  * Further, this software is distributed without any warranty that it is
     14  * free of the rightful claim of any third person regarding infringement
     15  * or the like.  Any license provided herein, whether implied or
     16  * otherwise, applies only to this software file.  Patent licenses, if
     17  * any, provided herein do not apply to combinations of this program with
     18  * other software, or any other product whatsoever.
     19  *
     20  * You should have received a copy of the GNU General Public License along
     21  * with this program; if not, write the Free Software Foundation, Inc.,
     22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     23  *
     24  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
     25  * Mountain View, CA  94043, or:
     26  *
     27  * http://www.sgi.com
     28  *
     29  * For further information regarding this notice, see:
     30  *
     31  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
     32  *
     33  */
     34  /*
     35   * Basic test for fcntl(2) using F_DUPFD argument.
     36   */
     37 
     38 #include <sys/types.h>
     39 #include <sys/stat.h>
     40 #include <fcntl.h>
     41 #include <unistd.h>
     42 #include <errno.h>
     43 #include <stdio.h>
     44 
     45 #include "tst_test.h"
     46 
     47 static int fd;
     48 static char fname[256];
     49 
     50 static const int min_fds[] = {0, 1, 2, 3, 10, 100};
     51 
     52 static void verify_fcntl(unsigned int n)
     53 {
     54 	int min_fd = min_fds[n];
     55 
     56 	TEST(fcntl(fd, F_DUPFD, min_fd));
     57 
     58 	if (TEST_RETURN == -1) {
     59 		tst_res(TFAIL | TTERRNO, "fcntl(%s, F_DUPFD, %i) failed",
     60 			fname, min_fd);
     61 		return;
     62 	}
     63 
     64 	if (TEST_RETURN < min_fd) {
     65 		tst_res(TFAIL, "fcntl(%s, F_DUPFD, %i) returned %ld < %i",
     66 			fname, min_fd, TEST_RETURN, min_fd);
     67 	}
     68 
     69 	tst_res(TPASS, "fcntl(%s, F_DUPFD, %i) returned %ld",
     70 		fname, min_fd, TEST_RETURN);
     71 
     72 	SAFE_CLOSE(TEST_RETURN);
     73 }
     74 
     75 static void setup(void)
     76 {
     77 	sprintf(fname, "fcntl02_%d", getpid());
     78 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
     79 }
     80 
     81 static void cleanup(void)
     82 {
     83 	if (fd > 0)
     84 		SAFE_CLOSE(fd);
     85 }
     86 
     87 static struct tst_test test = {
     88 	.needs_tmpdir = 1,
     89 	.test = verify_fcntl,
     90 	.tcnt = ARRAY_SIZE(min_fds),
     91 	.setup = setup,
     92 	.cleanup = cleanup,
     93 };
     94