added tsmread.c
[tridge/junkcode.git] / tsmread.c
1 /*
2   example program to read a file in the same way that Samba does. This
3   demonstrates the problem with GPFS share modes and leases and TSM
4   migrated files
5
6   Andrew Tridgell January 2008
7
8   compile with:
9
10      gcc -Wall -o tsmread{,.c} -lgpfs_gpl
11
12   then to run you must symlink tsmread to smbd
13
14      ln -s tsmread smbd
15
16   and run like this:
17
18     ./smbd /gpfs/data/tsmtest/test.dat 
19     ./smbd /gpfs/data/tsmtest/test.dat -S
20     ./smbd /gpfs/data/tsmtest/test.dat -L
21     ./smbd /gpfs/data/tsmtest/test.dat -L -S
22
23  */
24
25 #define _XOPEN_SOURCE 500
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <signal.h>
36 #include <stdbool.h>
37 #include "gpfs_gpl.h"
38
39
40 static void sigio_handler(int sig)
41 {
42         printf("Got SIGIO\n");
43 }
44
45 static int read_file(const char *fname, bool use_lease, bool use_sharemode)
46 {
47         int fd;
48         char c;
49
50         printf("Reading '%s' with use_lease=%s use_sharemode=%s\n",
51                fname, use_lease?"yes":"no", use_sharemode?"yes":"no");
52
53         signal(SIGIO, sigio_handler);
54
55         fd = open(fname, O_RDONLY);
56         if (fd == -1) {
57                 perror(fname);
58                 return -1;
59         }
60
61         if (use_lease && gpfs_set_lease(fd, GPFS_LEASE_READ) != 0) {
62                 perror("gpfs_set_lease");
63                 close(fd);
64                 return -1;
65         }
66
67         if (use_sharemode && gpfs_set_share(fd, 1, 2) != 0) {
68                 perror("gpfs_set_share");
69                 close(fd);
70                 return -1;
71         }
72
73         if (pread(fd, &c, 1, 0) != 1) {
74                 perror("pread");
75                 close(fd);
76                 return -1;              
77         }
78
79         printf("read OK\n");
80         
81         close(fd);
82         return 0;       
83 }
84
85 static void usage(void)
86 {
87         printf("Usage: (note, must run as 'smbd')\n");
88         printf("ln -sf tesmread smbd\n");
89         printf("./smbd [options] <files>\n");
90         printf("Options:\n");
91         printf("  -L    use gpfs leases\n");
92         printf("  -S    use gpfs sharemodes\n");
93         exit(0);
94 }
95
96 int main(int argc, char * const argv[])
97 {
98         int opt, i;
99         bool use_lease = false, use_sharemode = false;
100         
101         /* parse command-line options */
102         while ((opt = getopt(argc, argv, "LSh")) != -1) {
103                 switch (opt){
104                 case 'L':
105                         use_lease = true;
106                         break;
107                 case 'S':
108                         use_sharemode = true;
109                         break;
110                 default:
111                         usage();
112                         break;
113                 }
114         }
115
116         argv += optind;
117         argc -= optind;
118
119         if (argc == 0) {
120                 usage();
121         }
122
123         for (i=0;i<argc;i++) {
124                 const char *fname = argv[i];
125                 if (read_file(fname, use_lease, use_sharemode) != 0) {
126                         printf("Failed to read '%s'\n", fname);
127                 }
128         }
129
130         return 0;
131 }