t_linkat.c revision 272343
17836SJohn.Forte@Sun.COM/*	$NetBSD: t_linkat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $ */
27836SJohn.Forte@Sun.COM
37836SJohn.Forte@Sun.COM/*-
47836SJohn.Forte@Sun.COM * Copyright (c) 2012 The NetBSD Foundation, Inc.
57836SJohn.Forte@Sun.COM * All rights reserved.
67836SJohn.Forte@Sun.COM *
77836SJohn.Forte@Sun.COM * This code is derived from software contributed to The NetBSD Foundation
87836SJohn.Forte@Sun.COM * by Emmanuel Dreyfus.
97836SJohn.Forte@Sun.COM *
107836SJohn.Forte@Sun.COM * Redistribution and use in source and binary forms, with or without
117836SJohn.Forte@Sun.COM * modification, are permitted provided that the following conditions
127836SJohn.Forte@Sun.COM * are met:
137836SJohn.Forte@Sun.COM * 1. Redistributions of source code must retain the above copyright
147836SJohn.Forte@Sun.COM *    notice, this list of conditions and the following disclaimer.
157836SJohn.Forte@Sun.COM * 2. Redistributions in binary form must reproduce the above copyright
167836SJohn.Forte@Sun.COM *    notice, this list of conditions and the following disclaimer in the
177836SJohn.Forte@Sun.COM *    documentation and/or other materials provided with the distribution.
187836SJohn.Forte@Sun.COM *
197836SJohn.Forte@Sun.COM * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207836SJohn.Forte@Sun.COM * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217836SJohn.Forte@Sun.COM * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227836SJohn.Forte@Sun.COM * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237836SJohn.Forte@Sun.COM * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247836SJohn.Forte@Sun.COM * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257836SJohn.Forte@Sun.COM * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267836SJohn.Forte@Sun.COM * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277836SJohn.Forte@Sun.COM * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287836SJohn.Forte@Sun.COM * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297836SJohn.Forte@Sun.COM * POSSIBILITY OF SUCH DAMAGE.
307836SJohn.Forte@Sun.COM */
317836SJohn.Forte@Sun.COM#include <sys/cdefs.h>
327836SJohn.Forte@Sun.COM__RCSID("$NetBSD: t_linkat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $");
337836SJohn.Forte@Sun.COM
347836SJohn.Forte@Sun.COM#include <atf-c.h>
357836SJohn.Forte@Sun.COM#include <errno.h>
367836SJohn.Forte@Sun.COM#include <fcntl.h>
377836SJohn.Forte@Sun.COM#include <limits.h>
387836SJohn.Forte@Sun.COM#include <paths.h>
397836SJohn.Forte@Sun.COM#include <stdio.h>
407836SJohn.Forte@Sun.COM#include <string.h>
417836SJohn.Forte@Sun.COM#include <unistd.h>
427836SJohn.Forte@Sun.COM#include <sys/param.h>
437836SJohn.Forte@Sun.COM#include <sys/stat.h>
447836SJohn.Forte@Sun.COM
457836SJohn.Forte@Sun.COM#define ODIR "olddir"
467836SJohn.Forte@Sun.COM#define NDIR "newdir"
477836SJohn.Forte@Sun.COM#define FILE "olddir/old"
487836SJohn.Forte@Sun.COM#define BASEFILE "old"
497836SJohn.Forte@Sun.COM#define RELFILE "../olddir/old"
507836SJohn.Forte@Sun.COM#define TARGET "newdir/new"
517836SJohn.Forte@Sun.COM#define BASETARGET "new"
527836SJohn.Forte@Sun.COM#define LINK "olddir/symlink"
537836SJohn.Forte@Sun.COM#define BASELINK "symlink"
547836SJohn.Forte@Sun.COM#define FILEERR "olddir/olderr"
557836SJohn.Forte@Sun.COM
567836SJohn.Forte@Sun.COMATF_TC(linkat_fd);
577836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fd, tc)
587836SJohn.Forte@Sun.COM{
597836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr", "See that linkat works with fd");
607836SJohn.Forte@Sun.COM}
617836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fd, tc)
627836SJohn.Forte@Sun.COM{
637836SJohn.Forte@Sun.COM	int ofd, nfd, fd;
647836SJohn.Forte@Sun.COM	struct stat ost, nst;
657836SJohn.Forte@Sun.COM
667836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
677836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
687836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
697836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
707836SJohn.Forte@Sun.COM
717836SJohn.Forte@Sun.COM	ATF_REQUIRE((ofd = open(ODIR, O_RDONLY, 0)) != -1);
727836SJohn.Forte@Sun.COM	ATF_REQUIRE((nfd = open(NDIR, O_RDONLY, 0)) != -1);
737836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(ofd, BASEFILE, nfd, BASETARGET, 0) == 0);
747836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(ofd) == 0);
757836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(nfd) == 0);
767836SJohn.Forte@Sun.COM
777836SJohn.Forte@Sun.COM	ATF_REQUIRE(stat(FILE, &ost) == 0);
787836SJohn.Forte@Sun.COM	ATF_REQUIRE(stat(TARGET, &nst) == 0);
797836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino == nst.st_ino);
807836SJohn.Forte@Sun.COM}
817836SJohn.Forte@Sun.COM
827836SJohn.Forte@Sun.COMATF_TC(linkat_fdcwd);
837836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fdcwd, tc)
847836SJohn.Forte@Sun.COM{
857836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr",
867836SJohn.Forte@Sun.COM			  "See that linkat works with fd as AT_FDCWD");
877836SJohn.Forte@Sun.COM}
887836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fdcwd, tc)
897836SJohn.Forte@Sun.COM{
907836SJohn.Forte@Sun.COM	int fd;
917836SJohn.Forte@Sun.COM	struct stat ost, nst;
927836SJohn.Forte@Sun.COM
937836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
947836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
957836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
967836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
977836SJohn.Forte@Sun.COM
987836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(AT_FDCWD, FILE, AT_FDCWD, TARGET, 0) == 0);
997836SJohn.Forte@Sun.COM
1007836SJohn.Forte@Sun.COM	ATF_REQUIRE(stat(FILE, &ost) == 0);
1017836SJohn.Forte@Sun.COM	ATF_REQUIRE(stat(TARGET, &nst) == 0);
1027836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino == nst.st_ino);
1037836SJohn.Forte@Sun.COM}
1047836SJohn.Forte@Sun.COM
1057836SJohn.Forte@Sun.COMATF_TC(linkat_fdcwderr);
1067836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fdcwderr, tc)
1077836SJohn.Forte@Sun.COM{
1087836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr",
1097836SJohn.Forte@Sun.COM		  "See that linkat fails with fd as AT_FDCWD and bad path");
1107836SJohn.Forte@Sun.COM}
1117836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fdcwderr, tc)
1127836SJohn.Forte@Sun.COM{
1137836SJohn.Forte@Sun.COM	int fd;
1147836SJohn.Forte@Sun.COM
1157836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
1167836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
1177836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
1187836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
1197836SJohn.Forte@Sun.COM
1207836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(AT_FDCWD, FILEERR, AT_FDCWD, TARGET, 0) == -1);
1217836SJohn.Forte@Sun.COM}
1227836SJohn.Forte@Sun.COM
1237836SJohn.Forte@Sun.COMATF_TC(linkat_fderr);
1247836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fderr, tc)
1257836SJohn.Forte@Sun.COM{
1267836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr", "See that linkat fails with fd as -1");
1277836SJohn.Forte@Sun.COM}
1287836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fderr, tc)
1297836SJohn.Forte@Sun.COM{
1307836SJohn.Forte@Sun.COM	int fd;
1317836SJohn.Forte@Sun.COM
1327836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
1337836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
1347836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
1357836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
1367836SJohn.Forte@Sun.COM
1377836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(-1, FILE, AT_FDCWD, TARGET, 0) == -1);
1387836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(AT_FDCWD, FILE, -1, TARGET, 0) == -1);
1397836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(-1, FILE, -1, TARGET, 0) == -1);
1407836SJohn.Forte@Sun.COM}
1417836SJohn.Forte@Sun.COM
1427836SJohn.Forte@Sun.COMATF_TC(linkat_fdlink1);
1437836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fdlink1, tc)
1447836SJohn.Forte@Sun.COM{
1457836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr", "See that linkat works on symlink target");
1467836SJohn.Forte@Sun.COM}
1477836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fdlink1, tc)
1487836SJohn.Forte@Sun.COM{
1497836SJohn.Forte@Sun.COM	int ofd, nfd, fd;
1507836SJohn.Forte@Sun.COM	struct stat ost, nst;
1517836SJohn.Forte@Sun.COM
1527836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
1537836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
1547836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
1557836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
1567836SJohn.Forte@Sun.COM	ATF_REQUIRE(symlink(RELFILE, LINK) == 0);
1577836SJohn.Forte@Sun.COM
1587836SJohn.Forte@Sun.COM	ATF_REQUIRE((ofd = open(ODIR, O_RDONLY, 0)) != -1);
1597836SJohn.Forte@Sun.COM	ATF_REQUIRE((nfd = open(NDIR, O_RDONLY, 0)) != -1);
1607836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(ofd, BASELINK, nfd, BASETARGET,
1617836SJohn.Forte@Sun.COM	    AT_SYMLINK_FOLLOW) == 0);
1627836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(ofd) == 0);
1637836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(nfd) == 0);
1647836SJohn.Forte@Sun.COM
1657836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(LINK, &ost) == 0);
1667836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(TARGET, &nst) == 0);
1677836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino != nst.st_ino);
1687836SJohn.Forte@Sun.COM
1697836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(FILE, &ost) == 0);
1707836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(TARGET, &nst) == 0);
1717836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino == nst.st_ino);
1727836SJohn.Forte@Sun.COM}
1737836SJohn.Forte@Sun.COM
1747836SJohn.Forte@Sun.COM
1757836SJohn.Forte@Sun.COMATF_TC(linkat_fdlink2);
1767836SJohn.Forte@Sun.COMATF_TC_HEAD(linkat_fdlink2, tc)
1777836SJohn.Forte@Sun.COM{
1787836SJohn.Forte@Sun.COM	atf_tc_set_md_var(tc, "descr", "See that linkat works on symlink source");
1797836SJohn.Forte@Sun.COM}
1807836SJohn.Forte@Sun.COMATF_TC_BODY(linkat_fdlink2, tc)
1817836SJohn.Forte@Sun.COM{
1827836SJohn.Forte@Sun.COM	int ofd, nfd, fd;
1837836SJohn.Forte@Sun.COM	struct stat ost, nst;
1847836SJohn.Forte@Sun.COM
1857836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(ODIR, 0755) == 0);
1867836SJohn.Forte@Sun.COM	ATF_REQUIRE(mkdir(NDIR, 0755) == 0);
1877836SJohn.Forte@Sun.COM	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
1887836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(fd) != -1);
1897836SJohn.Forte@Sun.COM	ATF_REQUIRE(symlink(RELFILE, LINK) == 0);
1907836SJohn.Forte@Sun.COM
1917836SJohn.Forte@Sun.COM	ATF_REQUIRE((ofd = open(ODIR, O_RDONLY, 0)) != -1);
1927836SJohn.Forte@Sun.COM	ATF_REQUIRE((nfd = open(NDIR, O_RDONLY, 0)) != -1);
1937836SJohn.Forte@Sun.COM	ATF_REQUIRE(linkat(ofd, BASELINK, nfd, BASETARGET, 0) == 0);
1947836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(ofd) == 0);
1957836SJohn.Forte@Sun.COM	ATF_REQUIRE(close(nfd) == 0);
1967836SJohn.Forte@Sun.COM
1977836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(LINK, &ost) == 0);
1987836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(TARGET, &nst) == 0);
1997836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino == nst.st_ino);
2007836SJohn.Forte@Sun.COM
2017836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(FILE, &ost) == 0);
2027836SJohn.Forte@Sun.COM	ATF_REQUIRE(lstat(TARGET, &nst) == 0);
2037836SJohn.Forte@Sun.COM	ATF_REQUIRE(ost.st_ino != nst.st_ino);
2047836SJohn.Forte@Sun.COM}
2057836SJohn.Forte@Sun.COM
2067836SJohn.Forte@Sun.COMATF_TP_ADD_TCS(tp)
2077836SJohn.Forte@Sun.COM{
2087836SJohn.Forte@Sun.COM
2097836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fd);
2107836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fdcwd);
2117836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fdcwderr);
2127836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fderr);
2137836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fdlink1);
2147836SJohn.Forte@Sun.COM	ATF_TP_ADD_TC(tp, linkat_fdlink2);
2157836SJohn.Forte@Sun.COM
2167836SJohn.Forte@Sun.COM	return atf_no_error();
2177836SJohn.Forte@Sun.COM}
2187836SJohn.Forte@Sun.COM