11 void dangling_slink(int sub_test
, char const slink_to
[PATH_MAX
]);
13 void dangling_slink(int sub_test
, char const slink_to
[PATH_MAX
])
20 if (child
== (pid_t
) -1) {
23 } else if (child
== (pid_t
) 0) {
24 /* I'm the child. Create a dangling symlink with an absolute path */
29 /* We don't want to actually write to '/', so instead we pretend */
30 if (chroot(".") != 0) e(2);
32 /* Create file 'slink_to' with contents "bar" */
33 if ((fd
= open(slink_to
, O_CREAT
|O_WRONLY
)) == -1) e(3);
34 if (write(fd
, "bar", strlen("bar")) != strlen("bar")) e(4);
37 if (symlink(slink_to
, "a") == -1) e(5); /* Create the symlink */
38 if (rename(slink_to
, "c") == -1) e(6); /* Make it a dangling symlink */
40 /* Write "foo" to symlink; this should recreate file 'slink_to' with
42 if ((fd
= open("a", O_CREAT
|O_WRONLY
)) == -1) e(7);
43 if (write(fd
, "foo", strlen("foo")) != strlen("foo")) e(8);
46 /* Verify 'a' and 'slink_to' contain "foo" */
47 memset(buf
, '\0', sizeof(buf
));
48 if ((fd
= open("a", O_RDONLY
)) == -1) e(9);
49 if (read(fd
, buf
, 3) != 3) e(10);
50 if (strncmp(buf
, "foo", strlen("foo"))) e(11);
52 memset(buf
, '\0', sizeof(buf
));
53 if ((fd
= open(slink_to
, O_RDONLY
)) == -1) e(12);
54 if (read(fd
, buf
, 3) != 3) e(13);
55 if (strncmp(buf
, "foo", strlen("foo"))) e(14);
58 /* Verify 'c' contains 'bar' */
59 memset(buf
, '\0', sizeof(buf
));
60 if ((fd
= open("c", O_RDONLY
)) == -1) e(15);
61 if (read(fd
, buf
, 3) != 3) e(16);
62 if (strncmp(buf
, "bar", strlen("bar"))) e(17);
65 /* Cleanup created files */
66 if (unlink(slink_to
) == -1) e(18);
67 if (unlink("a") == -1) e(19);
68 if (unlink("c") == -1) e(20);
73 if (wait(&status
) == -1) e(7);
79 int main(int argc
, char *argv
[])
82 dangling_slink(1, "/abs"); /* Create dangling symlink with absolute path */
83 dangling_slink(2, "rel"); /* Create dangling symlink with relative path */
85 return(-1); /* Unreachable */