2 a test implementation of a HSM daemon
4 Andrew Tridgell August 2008
13 } dmapi_event_strings[] = {
14 #define EVENT_STRING(x) { x, #x }
15 EVENT_STRING(DM_EVENT_INVALID),
16 EVENT_STRING(DM_EVENT_CLOSE),
17 EVENT_STRING(DM_EVENT_MOUNT),
18 EVENT_STRING(DM_EVENT_PREUNMOUNT),
19 EVENT_STRING(DM_EVENT_UNMOUNT),
20 EVENT_STRING(DM_EVENT_NOSPACE),
21 EVENT_STRING(DM_EVENT_DEBUT),
22 EVENT_STRING(DM_EVENT_CREATE),
23 EVENT_STRING(DM_EVENT_POSTCREATE),
24 EVENT_STRING(DM_EVENT_REMOVE),
25 EVENT_STRING(DM_EVENT_POSTREMOVE),
26 EVENT_STRING(DM_EVENT_RENAME),
27 EVENT_STRING(DM_EVENT_POSTRENAME),
28 EVENT_STRING(DM_EVENT_SYMLINK),
29 EVENT_STRING(DM_EVENT_POSTSYMLINK),
30 EVENT_STRING(DM_EVENT_LINK),
31 EVENT_STRING(DM_EVENT_POSTLINK),
32 EVENT_STRING(DM_EVENT_READ),
33 EVENT_STRING(DM_EVENT_WRITE),
34 EVENT_STRING(DM_EVENT_TRUNCATE),
35 EVENT_STRING(DM_EVENT_ATTRIBUTE),
36 EVENT_STRING(DM_EVENT_CANCEL),
37 EVENT_STRING(DM_EVENT_DESTROY),
38 EVENT_STRING(DM_EVENT_USER),
39 EVENT_STRING(DM_EVENT_PREPERMCHANGE),
40 EVENT_STRING(DM_EVENT_POSTPERMCHANGE),
41 EVENT_STRING(DM_EVENT_MAX),
44 const char *dmapi_event_string(dm_eventtype_t ev)
47 for (i=0;i<sizeof(dmapi_event_strings)/sizeof(dmapi_event_strings[0]);i++) {
48 if (dmapi_event_strings[i].ev == ev) {
49 return dmapi_event_strings[i].name;
55 void hsm_recover_session(const char *name, dm_sessid_t *sid)
59 dm_sessid_t *sess = NULL;
60 dm_sessid_t oldsid = DM_NO_SESSION;
62 ret = dm_getall_sessions(0, NULL, &n);
67 printf("Bad error code %s from dm_getall_sessions\n", strerror(errno));
71 sess = (dm_sessid_t *)calloc(sizeof(dm_sessid_t), n);
73 printf("No memory for %u sessions\n", n);
77 ret = dm_getall_sessions(n, sess, &n);
79 printf("dm_getall_sessions failed\n");
84 char buf[DM_SESSION_INFO_LEN+1] = "";
87 ret = dm_query_session(sess[i], DM_SESSION_INFO_LEN, buf, &len);
92 if (strcmp(buf, name) == 0) {
93 printf("Recovered existing session\n");
101 ret = dm_create_session(oldsid, discard_const(name), sid);
103 printf("Failed to create session\n");
109 int hsm_store_open(dev_t device, ino_t inode, int flags)
112 asprintf(&fname, HSM_STORE "/0x%llx:0x%llx",
113 (unsigned long long)device, (unsigned long long)inode);
114 int fd = open(fname, flags, 0600);
119 int hsm_store_unlink(dev_t device, ino_t inode)
123 asprintf(&fname, HSM_STORE "/0x%llx:0x%llx",
124 (unsigned long long)device, (unsigned long long)inode);
137 select(0,NULL,NULL, NULL, &tval);
140 void hsm_cleanup_tokens(dm_sessid_t sid, dm_response_t response, int retcode)
142 dm_token_t *tok = NULL;
148 ret = dm_getall_tokens(sid, n, tok, &n2);
149 if (ret == -1 && errno == E2BIG) {
151 tok = realloc(tok, sizeof(dm_token_t)*n);
155 printf("dm_getall_tokens - %s\n", strerror(errno));
158 if (ret == 0 && n2 == 0) {
161 printf("Cleaning up %u tokens\n", n2);
163 dm_respond_event(sid, tok[i],
164 response, retcode, 0, NULL);
171 const char *timestring(void)
173 time_t t = time(NULL);
174 struct tm *tm = localtime(&t);
175 static char TimeBuf[100];
177 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);