1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/types.h> 5 #include <sys/stat.h> 6 #include <sys/xattr.h> 7 #include <fcntl.h> 8 #include <errno.h> 9 #include <string.h> 10 #include <time.h> 11 12 /* backward compat in case it's not defined */ 13 #ifndef O_TMPFILE 14 #define O_TMPFILE (020000000|O_DIRECTORY) 15 #endif 16 17 /* 18 * DESCRIPTION: 19 * Verify we can create tmpfile. 20 * 21 * STRATEGY: 22 * 1. open(2) with O_TMPFILE. 23 * 2. write(2) random data to it, then read(2) and compare. 24 * 3. fsetxattr(2) random data, then fgetxattr(2) and compare. 25 * 4. Verify the above operations run successfully. 26 * 27 */ 28 29 #define BSZ 64 30 31 static void 32 fill_random(char *buf, int len) 33 { 34 int i; 35 srand(time(NULL)); 36 for (i = 0; i < len; i++) { 37 buf[i] = (char)rand(); 38 } 39 } 40 41 int 42 main(int argc, char *argv[]) 43 { 44 int i, fd; 45 char buf1[BSZ], buf2[BSZ] = {}; 46 char *penv[] = {"TESTDIR"}; 47 48 (void) fprintf(stdout, "Verify O_TMPFILE is working properly.\n"); 49 50 /* 51 * Get the environment variable values. 52 */ 53 for (i = 0; i < sizeof (penv) / sizeof (char *); i++) { 54 if ((penv[i] = getenv(penv[i])) == NULL) { 55 (void) fprintf(stderr, "getenv(penv[%d])\n", i); 56 exit(1); 57 } 58 } 59 60 fill_random(buf1, BSZ); 61 62 fd = open(penv[0], O_RDWR|O_TMPFILE, 0666); 63 if (fd < 0) { 64 perror("open"); 65 exit(2); 66 } 67 68 if (write(fd, buf1, BSZ) < 0) { 69 perror("write"); 70 close(fd); 71 exit(3); 72 } 73 74 if (pread(fd, buf2, BSZ, 0) < 0) { 75 perror("pread"); 76 close(fd); 77 exit(4); 78 } 79 80 if (memcmp(buf1, buf2, BSZ) != 0) { 81 fprintf(stderr, "data corrupted\n"); 82 close(fd); 83 exit(5); 84 } 85 86 memset(buf2, 0, BSZ); 87 88 if (fsetxattr(fd, "user.test", buf1, BSZ, 0) < 0) { 89 perror("fsetxattr"); 90 close(fd); 91 exit(6); 92 } 93 94 if (fgetxattr(fd, "user.test", buf2, BSZ) < 0) { 95 perror("fgetxattr"); 96 close(fd); 97 exit(7); 98 } 99 100 if (memcmp(buf1, buf2, BSZ) != 0) { 101 fprintf(stderr, "xattr corrupted\n"); 102 close(fd); 103 exit(8); 104 } 105 106 close(fd); 107 108 return (0); 109 } 110