4 #include <sys/syslimits.h>
13 void dangling_slink(int sub_test
, char const slink_to
[PATH_MAX
]);
15 void dangling_slink(int sub_test
, char const slink_to
[PATH_MAX
])
22 if (child
== (pid_t
) -1) {
25 } else if (child
== (pid_t
) 0) {
26 /* I'm the child. Create a dangling symlink with an absolute path */
31 /* We don't want to actually write to '/', so instead we pretend */
32 if (chroot(".") != 0) e(2);
34 /* Create file 'slink_to' with contents "bar" */
35 if ((fd
= open(slink_to
, O_CREAT
|O_WRONLY
)) == -1) e(3);
36 if (write(fd
, "bar", strlen("bar")) != strlen("bar")) e(4);
39 if (symlink(slink_to
, "a") == -1) e(5); /* Create the symlink */
40 if (rename(slink_to
, "c") == -1) e(6); /* Make it a dangling symlink */
42 /* Write "foo" to symlink; this should recreate file 'slink_to' with
44 if ((fd
= open("a", O_CREAT
|O_WRONLY
)) == -1) e(7);
45 if (write(fd
, "foo", strlen("foo")) != strlen("foo")) e(8);
48 /* Verify 'a' and 'slink_to' contain "foo" */
49 memset(buf
, '\0', sizeof(buf
));
50 if ((fd
= open("a", O_RDONLY
)) == -1) e(9);
51 if (read(fd
, buf
, 3) != 3) e(10);
52 if (strncmp(buf
, "foo", strlen("foo"))) e(11);
54 memset(buf
, '\0', sizeof(buf
));
55 if ((fd
= open(slink_to
, O_RDONLY
)) == -1) e(12);
56 if (read(fd
, buf
, 3) != 3) e(13);
57 if (strncmp(buf
, "foo", strlen("foo"))) e(14);
60 /* Verify 'c' contains 'bar' */
61 memset(buf
, '\0', sizeof(buf
));
62 if ((fd
= open("c", O_RDONLY
)) == -1) e(15);
63 if (read(fd
, buf
, 3) != 3) e(16);
64 if (strncmp(buf
, "bar", strlen("bar"))) e(17);
67 /* Cleanup created files */
68 if (unlink(slink_to
) == -1) e(18);
69 if (unlink("c") == -1) e(19);
71 /* Use the dangling symlink to test mknod(2) following symlinks */
72 if (mknod("a", S_IFCHR
| 0777, makedev(1, 1)) != -1) e(20);
73 if (errno
!= EEXIST
) e(21);
74 if (unlink("a") == -1) e(22);
79 if (wait(&status
) == -1) e(7);
85 int main(int argc
, char *argv
[])
88 dangling_slink(1, "/abs"); /* Create dangling symlink with absolute path */
89 dangling_slink(2, "rel"); /* Create dangling symlink with relative path */
91 return(-1); /* Unreachable */