3 * Copyright (C) 2019 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
21 * Program test_linkat.c:
22 * Program to test use of linkat and O_TMPFILE and other combinations
24 * Author: Simone Piccardi
27 * Usage: testlinkat -h give all info's
29 ****************************************************************/
31 * Include needed headers
34 #include <errno.h> /* error definitions and routines */
35 #include <stdlib.h> /* C standard library */
36 #include <unistd.h> /* unix standard library */
37 #include <stdio.h> /* standard I/O library */
38 #include <string.h> /* C strings library */
40 #include <libgen.h> /* needed for dirname e basename */
42 #include <sys/types.h>
45 /* Help printing routine */
48 int main(int argc, char *argv[])
51 * Variables definition
57 * Input section: decode command line parameters
60 opterr = 0; /* don't want writing to stderr */
61 while ( (i = getopt(argc, argv, "hw:f:")) != -1) {
66 case 'h': /* help option */
67 printf("Wrong -h option use\n");
71 case 'w': /* time to wait in s */
72 wait = strtol(optarg, NULL, 10); /* convert input */
74 case 'f': /* using a source file */
77 case '?': /* unrecognized options */
78 printf("Unrecognized options -%c\n",optopt);
80 default: /* should not reached */
84 /* ***********************************************************
86 * Options processing completed
90 * ***********************************************************/
91 /* There must be 1 remaing parameters */
92 if ( (argc-optind) != 1 ) {
93 printf("From %d arguments, removed %d options\n", argc, optind);
96 char *path, *dir, *file;
97 char pattern[] = "prova prova prova\n";
98 path = strdup(argv[optind]);
99 file = basename(argv[optind]);
100 dir = dirname(argv[optind]);
101 printf("Working on dir %s file: %s\nwith path: %s\n",
105 fd = open(dir, O_TMPFILE|O_RDWR,S_IRUSR|S_IWUSR);
107 perror("cannot open TMPFILE");
108 if ( (i = write(fd, pattern, sizeof(pattern))) < 0 )
109 perror("cannot write on TMPFILE");
111 printf("saved %d chars on fd %d\n", i, fd);
113 printf("source is %s\n", srcfile);
114 if ( (fd = open(srcfile, O_RDONLY)) < 0 ) {
115 perror("cannot open source");
119 newfd = open(dir, O_PATH|O_RDWR);
121 err = linkat(fd, "", newfd, file, AT_EMPTY_PATH);
123 perror("error on creating TMPFILE");
124 printf("cannot link fd %d on fd %d, %s with AT_EMPTY_PATH\n",
126 char fdpath[PATH_MAX];
127 snprintf(fdpath, PATH_MAX, "/proc/self/fd/%d", fd);
128 printf("fd path %s\n", fdpath);
129 err = linkat(AT_FDCWD, fdpath, newfd, file, AT_SYMLINK_FOLLOW);
131 perror("still error creating TMPFILE");
135 if ( fchmodat(newfd, file,S_IRUSR|S_IWUSR,0) < 0 )
136 perror("Cannot change permission to new file");
142 * routine to print usage info and exit
145 printf("Program testlinkat : test linkat for a file \n");
147 printf(" testlinkat [-h] file mode \n");
148 printf(" -h print this help\n");
149 printf(" -w [N] wait N seconds\n");
150 printf(" -f [file] use file as source\n");