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