common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / dmapi / src / suite1 / cmd / test_assumption.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include <lib/hsm.h>
27
28 /*
29         Test that the new session ID is correctly returned to the caller on
30         dm_create_session when the oldsid parameter is not DM_NO_SESSION.
31
32         Test to make sure that DM_NO_TOKEN does not work if the session ID
33         is invalid.
34
35         Both are needed so that session assumption works correctly.  The
36         test creates a session, queries the session, assumes the session,
37         then attempts to check the event list of the old session ID while
38         using DM_NO_TOKEN.
39
40         The only parameter is the pathname of a DMAPI filesystem.  If it is
41         working correctly, you should get the error message:
42
43                 SUCCESS!
44
45         Any message containing the word FAILURE indicates a problem.
46 */
47
48 char            *Progname;
49 dm_sessid_t     sid = DM_NO_SESSION;    /* session ID of original session */
50 dm_sessid_t     newsid = DM_NO_SESSION; /* session ID after session resumed */
51 dm_eventset_t   eventlist;
52
53 int
54 main(
55         int     argc, 
56         char    **argv)
57 {
58         char            buffer[DM_SESSION_INFO_LEN];
59         void            *hanp;
60         size_t          hlen;
61         u_int           nelem;
62         size_t           rlen;
63
64         Progname = argv[0];
65
66         if (argc != 2) {
67                 fprintf(stderr, "usage:\t%s filesystem\n", Progname);
68                 exit(1);
69         }
70
71         /* Initialize the DMAPI interface and obtain a session ID, then verify
72            that the filesystem supports DMAPI.
73         */
74
75         if (setup_dmapi(&sid))
76                 exit(1);
77
78         if (dm_path_to_handle(argv[1], &hanp, &hlen)) {
79                 perror("FAILURE: can't get handle for filesystem");
80                 exit(1);
81         }
82
83         /* Query the session just to make sure things are working okay. */
84
85         if (dm_query_session(sid, sizeof(buffer), buffer, &rlen)) {
86                 errno_msg("FAILURE: can't query the original session ID %d",
87                         sid);
88                 exit(1);
89         }
90         fprintf(stdout, "Initial session ID: %d\n", sid);
91         fprintf(stdout, "Initial session message length: '%zd'\n", rlen);
92         if (rlen > 0) {
93                 fprintf(stdout, "Initial session message: '%s'\n", buffer);
94         }
95
96         /* Now try to assume the session. */
97
98         if (dm_create_session(sid, "this is a new message", &newsid))  {
99                 fprintf(stderr, "FAILURE: can't assume session %d\n", sid);
100                 exit(1);
101         }
102
103         /* Now query the new session. */
104
105         if (dm_query_session(newsid, sizeof(buffer), buffer, &rlen)) {
106                 errno_msg("FAILURE: can't query the assumed session ID %d",
107                         newsid);
108                 exit(1);
109         }
110         fprintf(stdout, "Assumed session ID: %d\n", newsid);
111         fprintf(stdout, "Assumed session message length: '%zd'\n", rlen);
112         if (rlen > 0) {
113                 fprintf(stdout, "Assumed session message: '%s'\n", buffer);
114         }
115
116         /* Get rid of the new session as we are done with it. */
117
118         if (dm_destroy_session(newsid))  {
119                 fprintf(stderr, "FAILURE: Can't shut down assumed session %d\n",
120                         newsid);
121                 exit(1);
122         }
123
124         /* Now verify that DM_NO_TOKEN will not work with the old session ID,
125            which is now invalid.
126         */
127
128         DMEV_ZERO(eventlist);
129
130         if (dm_get_eventlist(sid, hanp, hlen, DM_NO_TOKEN, DM_EVENT_MAX,
131                         &eventlist, &nelem) == 0) {
132                 fprintf(stderr, "FAILURE: dm_get_eventlist() worked when it "
133                         "should have failed!\n");
134         }
135 #ifdef  VERITAS_21
136         if (errno != ESRCH) {
137 #else
138         if (errno != EINVAL) {
139 #endif
140                 errno_msg("FAILURE: unexpected errno");
141                 exit(1);
142         }
143         fprintf(stdout, "SUCCESS!\n");
144         exit(0);
145 }