X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Ftest_linkat.c;fp=sources%2Ftest_linkat.c;h=fbc06dc0881963d11835ddc10bbf1e560eaa2795;hp=6931cd7c3ef2d40265e69e24e6ba0a178d6563fc;hb=4be39dd3718d708ccff65b01a8f634e05b56f3c9;hpb=2535dbace6167c4bcff7dd9c98f4d397bb699afd diff --git a/sources/test_linkat.c b/sources/test_linkat.c index 6931cd7..fbc06dc 100644 --- a/sources/test_linkat.c +++ b/sources/test_linkat.c @@ -19,7 +19,9 @@ /**************************************************************** * * Program test_linkat.c: - * Program to test use of linkat and O_TMPFILE and other combinations + * Program to test use of linkat with an O_TMPFILE or linking another file + * using AT_EMPTY_PATH or AT_SYMLINK_FOLLOW on /proc/self/fd/N + * giving the option to wait to remove source file * * Author: Simone Piccardi * Oct. 2018 @@ -42,6 +44,7 @@ #include #include + /* Help printing routine */ void usage(void); @@ -95,13 +98,15 @@ int main(int argc, char *argv[]) } char *path, *dir, *file; char pattern[] = "prova prova prova\n"; + char outbuff[2048]; path = strdup(argv[optind]); file = basename(argv[optind]); dir = dirname(argv[optind]); - printf("Working on dir %s file: %s\nwith path: %s\n", + printf("Destination on dir %s file: %s\n with path: %s\n", dir, file, path); int fd, newfd; if ( ! srcfile ) { + // temp file with O_TMPFILE fd = open(dir, O_TMPFILE|O_RDWR,S_IRUSR|S_IWUSR); if ( fd <= 0 ) perror("cannot open TMPFILE"); @@ -110,28 +115,32 @@ int main(int argc, char *argv[]) else printf("saved %d chars on fd %d\n", i, fd); } else { - printf("source is %s\n", srcfile); + // user provided source + printf("source file is %s\n", srcfile); if ( (fd = open(srcfile, O_RDONLY)) < 0 ) { perror("cannot open source"); exit(1); } + printf("waiting %d second to remove the file\n", wait); + sleep(wait); } newfd = open(dir, O_PATH|O_RDWR); int err; err = linkat(fd, "", newfd, file, AT_EMPTY_PATH); if ( err < 0 ) { - perror("error on creating TMPFILE"); - printf("cannot link fd %d on fd %d, %s with AT_EMPTY_PATH\n", + perror("error on creating TMPFILE with AT_EMPTY_PATH"); + printf("cannot link fd %d on fd %d/%s with AT_EMPTY_PATH\n", fd, newfd, file); char fdpath[PATH_MAX]; snprintf(fdpath, PATH_MAX, "/proc/self/fd/%d", fd); - printf("fd path %s\n", fdpath); + printf("Attempt to use AT_SYMLINK_FOLLOW with path %s\n", fdpath); err = linkat(AT_FDCWD, fdpath, newfd, file, AT_SYMLINK_FOLLOW); if ( err < 0 ) { perror("still error creating TMPFILE"); - sleep(wait); exit(1); - } + } else { + printf("Success, fd %d on fd %d/%s created\n", fd, newfd, file); + } if ( fchmodat(newfd, file,S_IRUSR|S_IWUSR,0) < 0 ) perror("Cannot change permission to new file"); } @@ -143,8 +152,9 @@ int main(int argc, char *argv[]) */ void usage(void) { printf("Program testlinkat : test linkat for a file \n"); + printf("Create a link to a tempfile or a link to -f indicated file"); printf("Usage:\n"); - printf(" testlinkat [-h] file mode \n"); + printf(" testlinkat [-h] destfilepathname \n"); printf(" -h print this help\n"); printf(" -w [N] wait N seconds\n"); printf(" -f [file] use file as source\n");