void *Thread(void *parm) { int rc; size_t dmMsgBufLen; dm_eventmsg_t *dmMsg; int bMounted = DM_FALSE; dm_eventtype_t type; dm_token_t token; dm_eventset_t events; dm_response_t response; do { /* Loop until message received (wait could be interrupted) */ do { DMLOG_PRINT(DMLVL_DEBUG, "Waiting for event...\n"); dmMsgBufLen = 0; rc = dm_get_events(sid, 1, DM_EV_WAIT, sizeof(dmMsgBuf), dmMsgBuf, &dmMsgBufLen); DMLOG_PRINT(DMLVL_DEBUG, "... dm_get_events returned %d (errno %d)\n", rc, errno); } while ((rc == -1) && (errno == EINTR) && (dmMsgBufLen == 0)); if (rc) { DMLOG_PRINT(DMLVL_ERR, "dm_get_events failed with rc = %d, errno = %d\n", rc, errno); dm_destroy_session(sid); DM_EXIT(); } else { dmMsg = (dm_eventmsg_t *)dmMsgBuf; token = dmMsg->ev_token; type = dmMsg->ev_type; DMLOG_PRINT(DMLVL_DEBUG, "Received message %d\n", type); } if (type == DM_EVENT_MOUNT) { /* SPECIAL CASE: need to set disposition, events and response */ dm_mount_event_t *me = DM_GET_VALUE(dmMsg, ev_data, dm_mount_event_t *); void *lhanp = DM_GET_VALUE(me, me_handle1, void *); size_t lhlen = DM_GET_LEN(me, me_handle1); DMLOG_PRINT(DMLVL_DEBUG, "Message is DM_EVENT_MOUNT\n"); DMLOG_PRINT(DMLVL_DEBUG, " Mode: %x\n", me->me_mode); DMLOG_PRINT(DMLVL_DEBUG, " File system handle: %p\n", lhanp); DMLOG_PRINT(DMLVL_DEBUG, " File system handle length: %d\n", lhlen); DMLOG_PRINT(DMLVL_DEBUG, " Mountpoint handle: %p\n", DM_GET_VALUE(me, me_handle2, void *)); DMLOG_PRINT(DMLVL_DEBUG, " Mountpoint handle length: %d\n", DM_GET_LEN(me, me_handle2)); DMLOG_PRINT(DMLVL_DEBUG, " Mountpoint path: %s\n", DM_GET_VALUE(me, me_name1, char *)); DMLOG_PRINT(DMLVL_DEBUG, " Media designator: %s\n", DM_GET_VALUE(me, me_name2, char *)); DMLOG_PRINT(DMLVL_DEBUG, " Root handle: %p\n", DM_GET_VALUE(me, me_roothandle, void *)); DMLOG_PRINT(DMLVL_DEBUG, " Root handle length: %d\n", DM_GET_LEN(me, me_roothandle)); bMounted = dm_handle_is_valid(lhanp, lhlen); rc = dm_request_right(sid, lhanp, lhlen, token, DM_RR_WAIT, DM_RIGHT_EXCL); if (rc == -1) { DMLOG_PRINT(DMLVL_ERR, "dm_request_right failed! (rc = %d, errno = %d)\n", rc, errno); dm_destroy_session(sid); DM_EXIT(); } DMEV_ZERO(events); DMEV_SET(DM_EVENT_PREUNMOUNT, events); DMEV_SET(DM_EVENT_UNMOUNT, events); DMEV_SET(DM_EVENT_READ, events); DMEV_SET(DM_EVENT_WRITE, events); rc = dm_set_disp(sid, lhanp, lhlen, token, &events, DM_EVENT_MAX); if (rc == -1) { DMLOG_PRINT(DMLVL_ERR, "dm_set_disp failed! (rc = %d, errno = %d)\n", rc, errno); dm_destroy_session(sid); DM_EXIT(); } DMEV_ZERO(events); DMEV_SET(DM_EVENT_PREUNMOUNT, events); DMEV_SET(DM_EVENT_UNMOUNT, events); rc = dm_set_eventlist(sid, lhanp, lhlen, token, &events, DM_EVENT_MAX); if (rc == -1) { DMLOG_PRINT(DMLVL_ERR, "dm_set_eventlist failed! (rc = %d, errno = %d)\n", rc, errno); dm_destroy_session(sid); DM_EXIT(); } rc = dm_release_right(sid, lhanp, lhlen, token); if (rc == -1) { DMLOG_PRINT(DMLVL_ERR, "dm_request_right failed! (rc = %d, errno = %d)\n", rc, errno); dm_destroy_session(sid); DM_EXIT(); } response = DM_RESP_CONTINUE; } else if (type == DM_EVENT_UNMOUNT) { dm_namesp_event_t *nse = DM_GET_VALUE(dmMsg, ev_data, dm_namesp_event_t *); if (nse->ne_retcode == 0) { bMounted = DM_FALSE; } response = DM_RESP_CONTINUE; } else {
int main(int argc, char **argv) { char *szSessionInfo = "dm_test session info"; char *szFuncName; char *varstr; int i; int rc; DMOPT_PARSE(argc, argv); DMLOG_START(); /* CANNOT DO ANYTHING WITHOUT SUCCESSFUL INITIALIZATION!!! */ if ((rc = dm_init_service(&varstr)) != 0) { DMLOG_PRINT(DMLVL_ERR, "dm_init_service failed! (rc = %d, errno = %d)\n", rc, errno); DM_EXIT(); } else { int nexist; rc = dm_getall_sessions(0, NULL, &nexist); if (rc == -1 && errno == E2BIG) { dm_sessid_t *psid; DMLOG_PRINT(DMLVL_DEBUG, "%d sessions already exist\n", nexist); if ((psid = malloc(nexist * sizeof(dm_sessid_t))) != NULL) { if ((rc = dm_getall_sessions(nexist, psid, &nexist)) == 0) { for (rc = 0, i = 0; i < nexist; i++) { DMLOG_PRINT(DMLVL_DEBUG, "destroying session %d\n", psid[i]); rc |= dm_destroy_session(psid[i]); } if (rc == -1) { DMLOG_PRINT(DMLVL_ERR, "dm_destroy_session failed, unable to destroy existing sessions\n"); DM_EXIT(); } } else { DMLOG_PRINT(DMLVL_ERR, "dm_getall_sessions failed, unable to destroy existing sessions\n"); DM_EXIT(); } free(psid); } else { DMLOG_PRINT(DMLVL_ERR, "malloc failed, unable to destroy existing sessions\n"); DM_EXIT(); } } } DMLOG_PRINT(DMLVL_DEBUG, "Starting DMAPI session tests\n") ; szFuncName = "dm_create_session"; /* * TEST : dm_create_session - invalid oldsid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 1)) { dm_sessid_t newsid; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid oldsid)\n", szFuncName); rc = dm_create_session(INVALID_ADDR, szSessionInfo, &newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } /* * TEST : dm_create_session - NULL sessinfop * EXPECTED: rc = 0 */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 2)) { dm_sessid_t newsid; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL sessinfop)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, NULL, &newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ } /* * TEST : dm_create_session - invalid sessinfop * EXPECTED: rc = -1, errno = EFAULT * * This variation uncovered XFS BUG #2 (0 return code from strnlen_user * ignored, which indicated fault) */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 3)) { dm_sessid_t newsid; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid sessinfop)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, (char *)INVALID_ADDR, &newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ } /* * TEST : dm_create_session - NULL newsidp * EXPECTED: rc = -1, errno = EFAULT */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 4)) { /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL newsidp)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szSessionInfo, NULL); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ } /* * TEST : dm_create_session - invalid newsidp * EXPECTED: rc = -1, errno = EFAULT */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 5)) { /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid newsidp)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szSessionInfo, (dm_sessid_t *)INVALID_ADDR); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ } /* * TEST : dm_create_session - DM_NO_SESSION oldsid * EXPECTED: rc = 0 */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 6)) { dm_sessid_t newsid; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(DM_NO_SESSION oldsid)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "newsid = %d\n", newsid); } DMVAR_ENDPASSEXP(szFuncName, 0, rc); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } /* * TEST : dm_create_session - valid oldsid * EXPECTED: rc = 0 */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 7)) { dm_sessid_t newsid, oldsid; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { oldsid = newsid; /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(valid oldsid)\n", szFuncName); rc = dm_create_session(oldsid, szSessionInfo, &newsid); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "newsid = %d\n", newsid); } DMVAR_ENDPASSEXP(szFuncName, 0, rc); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_create_session - invalidated oldsid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 8)) { dm_sessid_t newsid, oldsid, delsid; /* Variation set up */ if ((rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid)) == -1) { /* No clean up */ } else if ((rc = dm_create_session(oldsid = newsid, szSessionInfo, &newsid)) == -1) { dm_destroy_session(oldsid); } if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { delsid = newsid; /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalidated oldsid)\n", szFuncName); rc = dm_create_session(oldsid, szSessionInfo, &newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ rc = dm_destroy_session(delsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_create_session - maximum sessinfo * EXPECTED: rc = 0 */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 9)) { dm_sessid_t newsid; char *szBig = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(max sessionfo)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szBig, &newsid); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "newsid = %d\n", newsid); } DMVAR_ENDPASSEXP(szFuncName, 0, rc); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } /* * TEST : dm_create_session - sessinfo too big * EXPECTED: rc = -1, errno = E2BIG * * This variation uncovered XFS BUG #1 (sessinfo simply truncated, API * passed) */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 10)) { dm_sessid_t newsid; char *szTooBig = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(sessinfo too big)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szTooBig, &newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, E2BIG); /* Variation clean up */ } /* * TEST : dm_create_session - multiple sessions with same sessinfo * EXPECTED: rc = 0 */ if (DMVAR_EXEC(CREATE_SESSION_BASE + 11)) { dm_sessid_t newsid1, newsid2; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid1); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(same sessinfo)\n", szFuncName); rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid2); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "1st newsid = %d, 2nd newsid = %d\n", newsid1, newsid2); if (newsid1 != newsid2) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but session IDs same\n", szFuncName, 0); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid1); rc |= dm_destroy_session(newsid2); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } szFuncName = "dm_destroy_session"; /* * TEST : dm_destroy_session - DM_NO_SESSION sid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(DESTROY_SESSION_BASE + 1)) { /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(DM_NO_SESSION sid)\n", szFuncName); rc = dm_destroy_session(DM_NO_SESSION); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } /* * TEST : dm_destroy_session - invalid sid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(DESTROY_SESSION_BASE + 2)) { /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid sid)\n", szFuncName); rc = dm_destroy_session(INVALID_ADDR); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } /* * TEST : dm_destroy_session - invalidated sid * EXPECTED: rc = 0 */ if (DMVAR_EXEC(DESTROY_SESSION_BASE + 3)) { dm_sessid_t newsid; /* Variation set up */ if ((rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid)) != -1) { rc = dm_destroy_session(newsid); } if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalidated sid)\n", szFuncName); rc = dm_destroy_session(newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } } /* * TEST : dm_destroy_session - valid sid * EXPECTED: rc = 0 */ if (DMVAR_EXEC(DESTROY_SESSION_BASE + 4)) { dm_sessid_t newsid; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(valid sid)\n", szFuncName); rc = dm_destroy_session(newsid); DMVAR_ENDPASSEXP(szFuncName, 0, rc); /* Variation clean up */ } } /* * TEST : dm_destroy_session - sid with oustanding events * EXPECTED: rc = -1, erno = EBUSY */ if (DMVAR_EXEC(DESTROY_SESSION_BASE + 5)) { dm_sessid_t newsid; char buf[MSG_DATALEN]; size_t rlen; /* Variation set up */ memcpy(buf, MSG_DATA, MSG_DATALEN); rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); rc |= dm_send_msg(newsid, DM_MSGTYPE_ASYNC, MSG_DATALEN, buf); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(valid sid)\n", szFuncName); rc = dm_destroy_session(newsid); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EBUSY); /* Variation clean up */ rc = dm_get_events(newsid, 1, 0, sizeof(dmMsgBuf), dmMsgBuf, &rlen); rc |= dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } szFuncName = "dm_getall_sessions"; /* * TEST : dm_getall_sessions - NULL sidbufp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 1)) { dm_sessid_t newsid; int nelem; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL sidbufp)\n", szFuncName); rc = dm_getall_sessions(1, NULL, &nelem); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - invalid sidbufp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 2)) { dm_sessid_t newsid; int nelem; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid sidbufp)\n", szFuncName); rc = dm_getall_sessions(1, (dm_sessid_t *)INVALID_ADDR, &nelem); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - NULL nelemp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 3)) { dm_sessid_t newsid, sidArray[1]; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL nelemp)\n", szFuncName); rc = dm_getall_sessions(1, sidArray, NULL); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - invalid nelemp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 4)) { dm_sessid_t newsid, sidArray[1]; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid nelemp)\n", szFuncName); rc = dm_getall_sessions(1, sidArray, (u_int *)INVALID_ADDR); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - zero nelem, zero sessions * EXPECTED: rc = 0 */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 5)) { dm_sessid_t sidArray[1]; int nelem; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(zero nelem, zero sessions)\n", szFuncName); rc = dm_getall_sessions(0, sidArray, &nelem); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "nelem = %d\n", nelem); if (nelem == 0) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected nelem (%d vs %d)\n", szFuncName, 0, nelem, 0); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ } /* * TEST : dm_getall_sessions - zero nelem, one session * EXPECTED: rc = -1, errno = E2BIG */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 6)) { dm_sessid_t newsid, sidArray[1]; int nelem; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(zero nelem, one session)\n", szFuncName); rc = dm_getall_sessions(0, sidArray, &nelem); if (rc == -1) { if (errno == E2BIG) { DMLOG_PRINT(DMLVL_DEBUG, "nelem = %d\n", nelem); if (nelem == 1) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d and expected errno = %d\n", szFuncName, -1, E2BIG); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and expected errno = %d but unexpected nelem (%d vs %d)\n", szFuncName, -1, E2BIG, nelem, 1); DMVAR_PASS(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected errno = %d\n", szFuncName, -1, errno); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d\n", szFuncName, rc); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - one nelem, one session * EXPECTED: rc = 0 */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 7)) { dm_sessid_t newsid, sidArray[1]; int nelem; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(one nelem, one session)\n", szFuncName); rc = dm_getall_sessions(1, sidArray, &nelem); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "nelem = %d\n", nelem); if (nelem == 1) { LogSessions(sidArray, nelem); if (newsid == sidArray[0]) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and nelem = %d but unexpected session ID (%d vs %d)\n", szFuncName, 0, nelem, newsid, sidArray[0]); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected nelem (%d vs %d)\n", szFuncName, 0, nelem, 1); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - two nelem, one session * EXPECTED: rc = 0 */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 8)) { dm_sessid_t newsid, sidArray[2]; int nelem; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(two nelem, one session)\n", szFuncName); rc = dm_getall_sessions(2, sidArray, &nelem); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "nelem = %d\n", nelem); if (nelem == 1) { LogSessions(sidArray, nelem); if (newsid == sidArray[0]) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and nelem = %d but unexpected session ID (%d vs %d)\n", szFuncName, 0, nelem, newsid, sidArray[0]); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected nelem (%d vs %d)\n", szFuncName, 0, nelem, 1); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_getall_sessions - ten nelem, eight sessions * EXPECTED: rc = 0 */ if (DMVAR_EXEC(GETALL_SESSIONS_BASE + 9)) { dm_sessid_t sidExpected[NUM_SESSIONS], sidArray[10]; int nelem; /* Variation set up */ for (i = 0, rc = 0; i < NUM_SESSIONS && rc == 0; i++) { rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &sidExpected[i]); } if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); for (i--; i >= 0; i--) { dm_destroy_session(sidExpected[i]); } DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(%d nelem, %d sessions)\n", szFuncName, sizeof(sidArray)/sizeof(dm_sessid_t), NUM_SESSIONS); rc = dm_getall_sessions(sizeof(sidArray)/sizeof(dm_sessid_t), sidArray, &nelem); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "nelem = %d\n", nelem); if (nelem == NUM_SESSIONS) { LogSessions(sidArray, nelem); if (memcmp(sidArray, sidExpected, NUM_SESSIONS * sizeof(dm_sessid_t)) == 0) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and nelem = %d but unexpected session ID(s)\n", szFuncName, 0, nelem); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected nelem (%d vs %d)\n", szFuncName, 0, nelem, NUM_SESSIONS); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ for (i = 0, rc = 0; i < NUM_SESSIONS; i++) { rc |= dm_destroy_session(sidExpected[i]); } if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } szFuncName = "dm_query_session"; /* * TEST : dm_query_session - DM_NO_SESSION sid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 1)) { char buf[64]; size_t rlen; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(DM_NO_SESSION sid)\n", szFuncName); rc = dm_query_session(DM_NO_SESSION, sizeof(buf), buf, &rlen); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } /* * TEST : dm_query_session - invalid sid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 2)) { char buf[64]; size_t rlen; /* Variation set up */ /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid sid)\n", szFuncName); rc = dm_query_session(INVALID_ADDR, sizeof(buf), buf, &rlen); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } /* * TEST : dm_query_session - invalidated sid * EXPECTED: rc = -1, errno = EINVAL */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 3)) { dm_sessid_t newsid; char buf[64]; size_t rlen; /* Variation set up */ if ((rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid)) != -1) { rc = dm_destroy_session(newsid); } if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalidated sid)\n", szFuncName); rc = dm_query_session(newsid, sizeof(buf), buf, &rlen); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EINVAL); /* Variation clean up */ } } /* * TEST : dm_query_session - NULL bufp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 4)) { dm_sessid_t newsid; char buf[64]; size_t rlen; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL bufp)\n", szFuncName); rc = dm_query_session(newsid, sizeof(buf), NULL, &rlen); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - invalid bufp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 5)) { dm_sessid_t newsid; char buf[64]; size_t rlen; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid bufp)\n", szFuncName); rc = dm_query_session(newsid, sizeof(buf), (void *)INVALID_ADDR, &rlen); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - NULL rlenp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 6)) { dm_sessid_t newsid; char buf[64]; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(NULL rlenp)\n", szFuncName); rc = dm_query_session(newsid, sizeof(buf), buf, NULL); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - invalid rlenp * EXPECTED: rc = -1, errno EFAULT */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 7)) { dm_sessid_t newsid; char buf[64]; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(invalid rlenp)\n", szFuncName); rc = dm_query_session(newsid, sizeof(buf), buf, (size_t *)INVALID_ADDR); DMVAR_ENDFAILEXP(szFuncName, -1, rc, EFAULT); /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - zero buflen * EXPECTED: rc = -1, errno = E2BIG */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 8)) { dm_sessid_t newsid; char buf[64]; size_t rlen; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(buflen zero)\n", szFuncName); rc = dm_query_session(newsid, 0, buf, &rlen); if (rc == -1) { if (errno == E2BIG) { DMLOG_PRINT(DMLVL_DEBUG, "rlen = %d\n", rlen); if (rlen == strlen(szSessionInfo)+1) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d and expected errno = %d\n", szFuncName, -1, E2BIG); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and expected errno = %d but unexpected rlen (%d vs %d)\n", szFuncName, -1, E2BIG, rlen, strlen(szSessionInfo)+1); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected errno = %d\n", szFuncName, -1, errno); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d\n", szFuncName, rc); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - valid * EXPECTED: rc = 0 */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 9)) { dm_sessid_t newsid; char buf[64]; size_t rlen; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szSessionInfo, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(valid)\n", szFuncName, sizeof(buf)); rc = dm_query_session(newsid, sizeof(buf), buf, &rlen); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "rlen = %d\n", rlen); if (rlen == strlen(szSessionInfo)+1) { DMLOG_PRINT(DMLVL_DEBUG, "buf = \"%s\"\n", buf); if (strcmp(buf, szSessionInfo) == 0) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and rlen = %d but unexpected session info (\"%s\" vs \"%s\")\n", szFuncName, 0, rlen, buf, szSessionInfo); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected rlen (%d vs %d)\n", szFuncName, 0, rlen, strlen(szSessionInfo)+1); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } /* * TEST : dm_query_session - maximum sessionfo * sessioninfo * EXPECTED: rc = 0 */ if (DMVAR_EXEC(QUERY_SESSION_BASE + 10)) { dm_sessid_t newsid; char buf[512]; size_t rlen; char *szBig = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"; /* Variation set up */ rc = dm_create_session(DM_NO_SESSION, szBig, &newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to set up variation! (errno = %d)\n", errno); DMVAR_SKIP(); } else { /* Variation */ DMLOG_PRINT(DMLVL_DEBUG, "%s(max sessinfo)\n", szFuncName, sizeof(buf)); rc = dm_query_session(newsid, sizeof(buf), buf, &rlen); if (rc == 0) { DMLOG_PRINT(DMLVL_DEBUG, "rlen = %d\n", rlen); if (rlen == DM_SESSION_INFO_LEN) { DMLOG_PRINT(DMLVL_DEBUG, "buf = \"%s\"\n", buf); if (strncmp(buf, szBig, DM_SESSION_INFO_LEN-1) == 0) { DMLOG_PRINT(DMLVL_DEBUG, "%s passed with expected rc = %d\n", szFuncName, 0); DMVAR_PASS(); } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d and rlen = %d but unexpected session info (\"%s\" vs \"%s\")\n", szFuncName, 0, rlen, buf, szSessionInfo); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with expected rc = %d but unexpected rlen (%d vs %d)\n", szFuncName, 0, rlen, DM_SESSION_INFO_LEN); DMVAR_FAIL(); } } else { DMLOG_PRINT(DMLVL_ERR, "%s failed with unexpected rc = %d (errno = %d)\n", szFuncName, rc, errno); DMVAR_FAIL(); } /* Variation clean up */ rc = dm_destroy_session(newsid); if (rc == -1) { DMLOG_PRINT(DMLVL_DEBUG, "Unable to clean up variation! (errno = %d)\n", errno); } } } DMLOG_STOP(); return 0; }
static int dmapi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { sys_dmapi_args_t kargs; sys_dmapi_args_t *uap = &kargs; int error = 0; int rvp = -ENOSYS; int use_rvp = 0; if (!capable(CAP_MKNOD)) return(-EPERM); if( copy_from_user( &kargs, (sys_dmapi_args_t*)arg, sizeof(sys_dmapi_args_t) ) ) return -EFAULT; switch (cmd) { case DM_CLEAR_INHERIT: error = dm_clear_inherit( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrname_t *) uap->arg5); /* attrnamep */ break; case DM_CREATE_BY_HANDLE: error = dm_create_by_handle( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* dirhanp */ (size_t) uap->arg3, /* dirhlen */ (dm_token_t) uap->arg4, /* token */ (void *) uap->arg5, /* hanp */ (size_t) uap->arg6, /* hlen */ (char *) uap->arg7); /* cname */ break; case DM_CREATE_SESSION: error = dm_create_session( (dm_sessid_t) uap->arg1, /* oldsid */ (char *) uap->arg2, /* sessinfop */ (dm_sessid_t *) uap->arg3); /* newsidp */ break; case DM_CREATE_USEREVENT: error = dm_create_userevent( (dm_sessid_t) uap->arg1, /* sid */ (size_t) uap->arg2, /* msglen */ (void *) uap->arg3, /* msgdatap */ (dm_token_t *) uap->arg4); /* tokenp */ break; case DM_DESTROY_SESSION: error = dm_destroy_session( (dm_sessid_t) uap->arg1); /* sid */ break; case DM_DOWNGRADE_RIGHT: error = dm_downgrade_right( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4); /* token */ break; case DM_FD_TO_HANDLE: error = dm_fd_to_hdl( (int) uap->arg1, /* fd */ (void *) uap->arg2, /* hanp */ (size_t *) uap->arg3); /* hlenp */ break; case DM_FIND_EVENTMSG: error = dm_find_eventmsg( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (size_t) uap->arg3, /* buflen */ (void *) uap->arg4, /* bufp */ (size_t *) uap->arg5); /* rlenp */ break; case DM_GET_ALLOCINFO: use_rvp = 1; error = dm_get_allocinfo_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_off_t *) uap->arg5, /* offp */ (u_int) uap->arg6, /* nelem */ (dm_extent_t *) uap->arg7, /* extentp */ (u_int *) uap->arg8, /* nelemp */ &rvp); break; case DM_GET_BULKALL: use_rvp = 1; error = dm_get_bulkall_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* mask */ (dm_attrname_t *) uap->arg6, /* attrnamep */ (dm_attrloc_t *) uap->arg7, /* locp */ (size_t) uap->arg8, /* buflen */ (void *) uap->arg9, /* bufp */ (size_t *) uap->arg10, /* rlenp */ &rvp); break; case DM_GET_BULKATTR: use_rvp = 1; error = dm_get_bulkattr_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* mask */ (dm_attrloc_t *)uap->arg6, /* locp */ (size_t) uap->arg7, /* buflen */ (void *) uap->arg8, /* bufp */ (size_t *) uap->arg9, /* rlenp */ &rvp); break; case DM_GET_CONFIG: error = dm_get_config( (void *) uap->arg1, /* hanp */ (size_t) uap->arg2, /* hlen */ (dm_config_t) uap->arg3, /* flagname */ (dm_size_t *) uap->arg4); /* retvalp */ break; case DM_GET_CONFIG_EVENTS: error = dm_get_config_events( (void *) uap->arg1, /* hanp */ (size_t) uap->arg2, /* hlen */ (u_int) uap->arg3, /* nelem */ (dm_eventset_t *) uap->arg4, /* eventsetp */ (u_int *) uap->arg5); /* nelemp */ break; case DM_GET_DIOINFO: error = dm_get_dioinfo( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_dioinfo_t *)uap->arg5); /* diop */ break; case DM_GET_DIRATTRS: use_rvp = 1; error = dm_get_dirattrs_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* mask */ (dm_attrloc_t *)uap->arg6, /* locp */ (size_t) uap->arg7, /* buflen */ (void *) uap->arg8, /* bufp */ (size_t *) uap->arg9, /* rlenp */ &rvp); break; case DM_GET_DMATTR: error = dm_get_dmattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrname_t *) uap->arg5, /* attrnamep */ (size_t) uap->arg6, /* buflen */ (void *) uap->arg7, /* bufp */ (size_t *) uap->arg8); /* rlenp */ break; case DM_GET_EVENTLIST: error = dm_get_eventlist( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* nelem */ (dm_eventset_t *) uap->arg6, /* eventsetp */ (u_int *) uap->arg7); /* nelemp */ break; case DM_GET_EVENTS: error = dm_get_events( (dm_sessid_t) uap->arg1, /* sid */ (u_int) uap->arg2, /* maxmsgs */ (u_int) uap->arg3, /* flags */ (size_t) uap->arg4, /* buflen */ (void *) uap->arg5, /* bufp */ (size_t *) uap->arg6); /* rlenp */ break; case DM_GET_FILEATTR: error = dm_get_fileattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* mask */ (dm_stat_t *) uap->arg6); /* statp */ break; case DM_GET_MOUNTINFO: error = dm_get_mountinfo( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (size_t) uap->arg5, /* buflen */ (void *) uap->arg6, /* bufp */ (size_t *) uap->arg7); /* rlenp */ break; case DM_GET_REGION: error = dm_get_region( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* nelem */ (dm_region_t *) uap->arg6, /* regbufp */ (u_int *) uap->arg7); /* nelemp */ break; case DM_GETALL_DISP: error = dm_getall_disp( (dm_sessid_t) uap->arg1, /* sid */ (size_t) uap->arg2, /* buflen */ (void *) uap->arg3, /* bufp */ (size_t *) uap->arg4); /* rlenp */ break; case DM_GETALL_DMATTR: error = dm_getall_dmattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (size_t) uap->arg5, /* buflen */ (void *) uap->arg6, /* bufp */ (size_t *) uap->arg7); /* rlenp */ break; case DM_GETALL_INHERIT: error = dm_getall_inherit( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* nelem */ (dm_inherit_t *)uap->arg6, /* inheritbufp*/ (u_int *) uap->arg7); /* nelemp */ break; case DM_GETALL_SESSIONS: error = dm_getall_sessions( (u_int) uap->arg1, /* nelem */ (dm_sessid_t *) uap->arg2, /* sidbufp */ (u_int *) uap->arg3); /* nelemp */ break; case DM_GETALL_TOKENS: error = dm_getall_tokens( (dm_sessid_t) uap->arg1, /* sid */ (u_int) uap->arg2, /* nelem */ (dm_token_t *) uap->arg3, /* tokenbufp */ (u_int *) uap->arg4); /* nelemp */ break; case DM_INIT_ATTRLOC: error = dm_init_attrloc( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrloc_t *) uap->arg5); /* locp */ break; case DM_MKDIR_BY_HANDLE: error = dm_mkdir_by_handle( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* dirhanp */ (size_t) uap->arg3, /* dirhlen */ (dm_token_t) uap->arg4, /* token */ (void *) uap->arg5, /* hanp */ (size_t) uap->arg6, /* hlen */ (char *) uap->arg7); /* cname */ break; case DM_MOVE_EVENT: error = dm_move_event( (dm_sessid_t) uap->arg1, /* srcsid */ (dm_token_t) uap->arg2, /* token */ (dm_sessid_t) uap->arg3, /* targetsid */ (dm_token_t *) uap->arg4); /* rtokenp */ break; case DM_OBJ_REF_HOLD: error = dm_obj_ref_hold( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (void *) uap->arg3, /* hanp */ (size_t) uap->arg4); /* hlen */ break; case DM_OBJ_REF_QUERY: use_rvp = 1; error = dm_obj_ref_query_rvp( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (void *) uap->arg3, /* hanp */ (size_t) uap->arg4, /* hlen */ &rvp); break; case DM_OBJ_REF_RELE: error = dm_obj_ref_rele( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (void *) uap->arg3, /* hanp */ (size_t) uap->arg4); /* hlen */ break; case DM_PATH_TO_FSHANDLE: error = dm_path_to_fshdl( (char *) uap->arg1, /* path */ (void *) uap->arg2, /* hanp */ (size_t *) uap->arg3); /* hlenp */ break; case DM_PATH_TO_HANDLE: error = dm_path_to_hdl( (char *) uap->arg1, /* path */ (void *) uap->arg2, /* hanp */ (size_t *) uap->arg3); /* hlenp */ break; case DM_PENDING: error = dm_pending( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (dm_timestruct_t *) uap->arg3); /* delay */ break; case DM_PROBE_HOLE: error = dm_probe_hole( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_off_t) uap->arg5, /* off */ (dm_size_t) uap->arg6, /* len */ (dm_off_t *) uap->arg7, /* roffp */ (dm_size_t *) uap->arg8); /* rlenp */ break; case DM_PUNCH_HOLE: error = dm_punch_hole( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_off_t) uap->arg5, /* off */ (dm_size_t) uap->arg6); /* len */ break; case DM_QUERY_RIGHT: error = dm_query_right( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_right_t *) uap->arg5); /* rightp */ break; case DM_QUERY_SESSION: error = dm_query_session( (dm_sessid_t) uap->arg1, /* sid */ (size_t) uap->arg2, /* buflen */ (void *) uap->arg3, /* bufp */ (size_t *) uap->arg4); /* rlenp */ break; case DM_READ_INVIS: use_rvp = 1; error = dm_read_invis_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_off_t) uap->arg5, /* off */ (dm_size_t) uap->arg6, /* len */ (void *) uap->arg7, /* bufp */ &rvp); break; case DM_RELEASE_RIGHT: error = dm_release_right( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4); /* token */ break; case DM_REMOVE_DMATTR: error = dm_remove_dmattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (int) uap->arg5, /* setdtime */ (dm_attrname_t *) uap->arg6); /* attrnamep */ break; case DM_REQUEST_RIGHT: error = dm_request_right( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* flags */ (dm_right_t) uap->arg6); /* right */ break; case DM_RESPOND_EVENT: error = dm_respond_event( (dm_sessid_t) uap->arg1, /* sid */ (dm_token_t) uap->arg2, /* token */ (dm_response_t) uap->arg3, /* response */ (int) uap->arg4, /* reterror */ (size_t) uap->arg5, /* buflen */ (void *) uap->arg6); /* respbufp */ break; case DM_SEND_MSG: error = dm_send_msg( (dm_sessid_t) uap->arg1, /* targetsid */ (dm_msgtype_t) uap->arg2, /* msgtype */ (size_t) uap->arg3, /* buflen */ (void *) uap->arg4); /* bufp */ break; case DM_SET_DISP: error = dm_set_disp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_eventset_t *) uap->arg5, /* eventsetp */ (u_int) uap->arg6); /* maxevent */ break; case DM_SET_DMATTR: error = dm_set_dmattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrname_t *) uap->arg5, /* attrnamep */ (int) uap->arg6, /* setdtime */ (size_t) uap->arg7, /* buflen */ (void *) uap->arg8); /* bufp */ break; case DM_SET_EVENTLIST: error = dm_set_eventlist( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_eventset_t *) uap->arg5, /* eventsetp */ (u_int) uap->arg6); /* maxevent */ break; case DM_SET_FILEATTR: error = dm_set_fileattr( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* mask */ (dm_fileattr_t *)uap->arg6); /* attrp */ break; case DM_SET_INHERIT: error = dm_set_inherit( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrname_t *)uap->arg5, /* attrnamep */ (mode_t) uap->arg6); /* mode */ break; case DM_SET_REGION: error = dm_set_region( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (u_int) uap->arg5, /* nelem */ (dm_region_t *) uap->arg6, /* regbufp */ (dm_boolean_t *) uap->arg7); /* exactflagp */ break; case DM_SET_RETURN_ON_DESTROY: error = dm_set_return_on_destroy( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (dm_attrname_t *) uap->arg5, /* attrnamep */ (dm_boolean_t) uap->arg6); /* enable */ break; case DM_SYMLINK_BY_HANDLE: error = dm_symlink_by_handle( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* dirhanp */ (size_t) uap->arg3, /* dirhlen */ (dm_token_t) uap->arg4, /* token */ (void *) uap->arg5, /* hanp */ (size_t) uap->arg6, /* hlen */ (char *) uap->arg7, /* cname */ (char *) uap->arg8); /* path */ break; case DM_SYNC_BY_HANDLE: error = dm_sync_by_handle( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4); /* token */ break; case DM_UPGRADE_RIGHT: error = dm_upgrade_right( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4); /* token */ break; case DM_WRITE_INVIS: use_rvp = 1; error = dm_write_invis_rvp( (dm_sessid_t) uap->arg1, /* sid */ (void *) uap->arg2, /* hanp */ (size_t) uap->arg3, /* hlen */ (dm_token_t) uap->arg4, /* token */ (int) uap->arg5, /* flags */ (dm_off_t) uap->arg6, /* off */ (dm_size_t) uap->arg7, /* len */ (void *) uap->arg8, /* bufp */ &rvp); break; default: error = ENOSYS; break; } /* If it was an *_rvp() function, then if error==0, return |rvp| */ if( use_rvp && (error == 0) ) return rvp; else return -error; }
static void event_loop( dm_sessid_t sid, int waitflag) { void *msgbuf; size_t bufsize; int error; dm_eventmsg_t *msg; int count; /* * We take a swag at a buffer size. If it's wrong, we can * always resize it */ bufsize = sizeof(dm_eventmsg_t) + sizeof(dm_data_event_t) + HANDLE_LEN; bufsize *= 50; msgbuf = (void *)malloc(bufsize); if (msgbuf == NULL) { err_msg("Can't allocate memory for buffer"); return; } for (;;) { error = dm_get_events(sid, ALL_AVAIL_MSGS, waitflag ? DM_EV_WAIT:0, bufsize, msgbuf, &bufsize); if (error) { if (errno == EAGAIN) { if (waitflag) continue; break; } if (errno == E2BIG) { free(msgbuf); msgbuf = (void *)malloc(bufsize); if (msgbuf == NULL) { err_msg("Can't resize msg buffer"); return; } continue; } errno_msg("Error getting events from DMAPI"); break; } /* * Walk through the message buffer, pull out each individual * message, and dispatch the messages to handle_message(), * which will respond to the events. */ count = 0; msg = (dm_eventmsg_t *)msgbuf; while (msg != NULL ) { count++; error = handle_message(sid, msg); if (error) { free(msgbuf); return; } printf("end_of_message\n"); msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *); } if (count != 1 && Verbose) { err_msg("Found %d events in one call to " "dm_get_events\n", count); } } if (msgbuf != NULL) free(msgbuf); }
int main( int argc, char **argv) { dm_sessid_t sid, oldsid, targetsid, *newsidp, *sidbufp; dm_token_t token, *tokenp, *rtokenp, *tokenbufp; dm_attrname_t *attrnamep; dm_off_t off, *offp, *roffp; dm_extent_t *extentp; dm_inherit_t *inheritbufp; dm_stat_t *statp; dm_size_t len, *dmrlenp, *retvalp; dm_attrloc_t *locp; dm_eventset_t *eventsetp; dm_config_t flagname; dm_region_t *regbufp; dm_response_t response; dm_right_t right, *rightp; dm_igen_t igen, *igenp; dm_msgtype_t msgtype; dm_fileattr_t *attrp; dm_boolean_t enable, *exactflagp; dm_timestruct_t *delay; mode_t mode; size_t hlen, dirhlen, hlen1, hlen2, targhlen, *fshlenp, *hlenp; size_t msglen, buflen, *rlenp; u_int nelem, mask, maxmsgs, uflags, *nelemp, maxevent; void *hanp, *dirhanp, *hanp1, *hanp2, *targhanp; void *msgdatap, *bufp, **hanpp, *respbufp, **fshanpp; dm_fsid_t fsid, *fsidp; dm_ino_t ino, *inop; char *cname, *sessinfop, *path, *pathbufp, **versionstrpp; int flags, fd, setdtime, reterror; u_int urc; int rc; dm_ssize_t ssrc; /* Definitions per the prototypes in dmport.h, in the same order. */ rc = dm_clear_inherit(sid, hanp, hlen, token, attrnamep); rc = dm_create_by_handle(sid, dirhanp, dirhlen, token, hanp, hlen, cname); rc = dm_create_session(oldsid, sessinfop, newsidp); rc = dm_create_userevent(sid, msglen, msgdatap, tokenp); rc = dm_destroy_session(sid); rc = dm_downgrade_right(sid, hanp, hlen, token); rc = dm_fd_to_handle(fd, hanpp, hlenp); rc = dm_find_eventmsg(sid, token, buflen, bufp, rlenp); rc = dm_get_allocinfo(sid, hanp, hlen, token, offp, nelem, extentp, nelemp); rc = dm_get_bulkall(sid, hanp, hlen, token, mask, attrnamep, locp, buflen, bufp, rlenp); rc = dm_get_bulkattr(sid, hanp, hlen, token, mask, locp, buflen, bufp, rlenp); rc = dm_get_config(hanp, hlen, flagname, retvalp); rc = dm_get_config_events(hanp, hlen, nelem, eventsetp, nelemp); rc = dm_get_dirattrs(sid, hanp, hlen, token, mask, locp, buflen, bufp, rlenp); rc = dm_get_dmattr(sid, hanp, hlen, token, attrnamep, buflen, bufp, rlenp); rc = dm_get_eventlist(sid, hanp, hlen, token, nelem, eventsetp, nelemp); rc = dm_get_events(sid, maxmsgs, flags, buflen, bufp, rlenp); rc = dm_get_fileattr(sid, hanp, hlen, token, mask, statp); rc = dm_get_mountinfo(sid, hanp, hlen, token, buflen, bufp, rlenp); rc = dm_get_region(sid, hanp, hlen, token, nelem, regbufp, nelemp); rc = dm_getall_disp(sid, buflen, bufp, rlenp); rc = dm_getall_dmattr(sid, hanp, hlen, token, buflen, bufp, rlenp); rc = dm_getall_inherit(sid, hanp, hlen, token, nelem, inheritbufp, nelemp); rc = dm_getall_sessions(nelem, sidbufp, nelemp); rc = dm_getall_tokens(sid, nelem, tokenbufp, nelemp); rc = dm_handle_cmp(hanp1, hlen1, hanp2, hlen2); dm_handle_free(hanp, hlen); urc = dm_handle_hash(hanp, hlen); rc = dm_handle_is_valid(hanp, hlen); rc = dm_handle_to_fshandle(hanp, hlen, fshanpp, fshlenp); rc = dm_handle_to_fsid(hanp, hlen, fsidp); rc = dm_handle_to_igen(hanp, hlen, igenp); rc = dm_handle_to_ino(hanp, hlen, inop); rc = dm_handle_to_path(dirhanp, dirhlen, targhanp, targhlen, buflen, pathbufp, rlenp); rc = dm_init_attrloc(sid, hanp, hlen, token, locp); rc = dm_init_service(versionstrpp); rc = dm_make_handle(&fsid, &ino, &igen, hanpp, hlenp); rc = dm_make_fshandle(&fsid, hanpp, hlenp); rc = dm_mkdir_by_handle(sid, dirhanp, dirhlen, token, hanp, hlen, cname); rc = dm_move_event(sid, token, targetsid, rtokenp); rc = dm_obj_ref_hold(sid, token, hanp, hlen); rc = dm_obj_ref_query(sid, token, hanp, hlen); rc = dm_obj_ref_rele(sid, token, hanp, hlen); rc = dm_path_to_fshandle(path, hanpp, hlenp); rc = dm_path_to_handle(path, hanpp, hlenp); rc = dm_pending(sid, token, delay); rc = dm_probe_hole(sid, hanp, hlen, token, off, len, roffp, dmrlenp); rc = dm_punch_hole(sid, hanp, hlen, token, off, len); rc = dm_query_right(sid, hanp, hlen, token, rightp); rc = dm_query_session(sid, buflen, bufp, rlenp); ssrc = dm_read_invis(sid, hanp, hlen, token, off, len, bufp); rc = dm_release_right(sid, hanp, hlen, token); rc = dm_remove_dmattr(sid, hanp, hlen, token, setdtime, attrnamep); rc = dm_request_right(sid, hanp, hlen, token, uflags, right); rc = dm_respond_event(sid, token, response, reterror, buflen, respbufp); rc = dm_send_msg(sid, msgtype, buflen, bufp); rc = dm_set_disp(sid, hanp, hlen, token, eventsetp, maxevent); rc = dm_set_dmattr(sid, hanp, hlen, token, attrnamep, setdtime, buflen, bufp); rc = dm_set_eventlist(sid, hanp, hlen, token, eventsetp, maxevent); rc = dm_set_fileattr(sid, hanp, hlen, token, mask, attrp); rc = dm_set_inherit(sid, hanp, hlen, token, attrnamep, mode); rc = dm_set_region(sid, hanp, hlen, token, nelem, regbufp, exactflagp); rc = dm_set_return_on_destroy(sid, hanp, hlen, token, attrnamep, enable); rc = dm_symlink_by_handle(sid, dirhanp, dirhlen, token, hanp, hlen, cname, path); rc = dm_sync_by_handle(sid, hanp, hlen, token); rc = dm_upgrade_right(sid, hanp, hlen, token); ssrc = dm_write_invis(sid, hanp, hlen, flags, token, off, len, bufp); exit(0); }
/* * Main event loop processing */ void event_loop( dm_sessid_t sid) { void *msgbuf; size_t bufsize, rlen; int error; dm_eventmsg_t *msg; /* * We take a swag at a buffer size. If it's wrong, we can * always resize it */ bufsize = sizeof(dm_eventmsg_t) + sizeof(dm_data_event_t) + HANDLE_LEN; bufsize *= 16; msgbuf = (void *)malloc(bufsize); if (msgbuf == NULL) { err_msg("Can't allocate memory for buffer"); goto out; } for (;;) { error = dm_get_events(sid, ALL_AVAIL_MSGS, DM_EV_WAIT, bufsize, msgbuf, &rlen); if (error == -1) { if (errno == E2BIG) { free(msgbuf); msgbuf = (void *)malloc(rlen); if (msgbuf == NULL) { err_msg("Can't resize msg buffer"); goto out; } continue; } errno_msg("Error getting events from DMAPI"); goto out; } /* * Walk thru the message buffer, pull out each individual * message, and dispatch the messages to child processes * with the sid, token, and data. The children will * respond to the events. */ msg = (dm_eventmsg_t *)msgbuf; while (msg != NULL ) { if (Verbose) { fprintf(stderr, "Received %s, token %d\n", (msg->ev_type == DM_EVENT_READ ? "read" : (msg->ev_type == DM_EVENT_WRITE ? "write" : "trunc")), msg->ev_token); } switch (msg->ev_type) { case DM_EVENT_READ: spawn_kid(sid, msg->ev_token, RESTORE_FILE); break; case DM_EVENT_WRITE: case DM_EVENT_TRUNCATE: spawn_kid(sid, msg->ev_token, INVAL_FILE); break; default: err_msg("Invalid msg type %d\n", msg->ev_type); break; } msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *); } } out: if (msgbuf != NULL) free(msgbuf); migin_exit(0); }
// main event loop void event_loop() { void *msgbuf; size_t bufsize, rlen; int error; dm_eventmsg_t *msg; pid_t process_id; void *hanp; size_t hlen; dm_data_event_t *msgev; int first_time=1; // define inizial message buffer size bufsize = sizeof(dm_eventmsg_t) + sizeof(dm_data_event_t) + HANDLE_LEN; bufsize *= 16; msgbuf = (void *)malloc(bufsize); if (msgbuf == NULL) { fprintf(stderr,"Can't allocate memory for buffer\n"); goto out; } // start infinite loop for (;;) { // cleanup all finished children while (child_proc_count) { process_id = waitpid((pid_t) -1, NULL, WNOHANG); if (process_id < 0) { // waitpid error fprintf(stderr, "cannot waitpid\n"); exit(1); } else if (process_id == 0) break; // no child to wait else { child_proc_count--; // cleaned up one child if(Verbose) { fprintf(stderr,"Cleanup child with pid %d\n",process_id); } } } // if initialization is not finished, try to finalize if(global_state==1) finalize_init(); // check if filesystem is mounted, otherwise exit if(global_state==2&&!filesystem_is_mounted()) exit_program(0); // sleep for MAIN_LOOP_SLEEP_TIME mus usleep(MAIN_LOOP_SLEEP_TIME); // check if maximum number of children has been reached if(child_proc_count>=MAXIMUM_ALLOWED_CHILDREN) { if(first_time) printf("Maximum number of children reached %d/%d\n",child_proc_count,MAXIMUM_ALLOWED_CHILDREN); first_time=0; continue; } if(!first_time) { printf("Number of children back to normality %d/%d\n",child_proc_count,MAXIMUM_ALLOWED_CHILDREN); first_time=1; } // get MAXIMUM_MESSAGES available event messages with unblocking call error = dm_get_events(sid, MAXIMUM_MESSAGES, 0, bufsize, msgbuf, &rlen); if (error == -1) { if (errno == E2BIG) { // message buffer was too small, enlarge it free(msgbuf); msgbuf = (void *)malloc(rlen); if (msgbuf == NULL) { fprintf(stderr,"Can't resize msg buffer\n"); goto out; } continue; } // we got an error while getting events, or simply there were no events // for the moment continue, but should improve error handling here continue; } // examine each message and dispatch children to manage events and respond to client msg = (dm_eventmsg_t *)msgbuf; while (msg != NULL ) { if (Verbose) { fprintf(stderr, "Received %s, sid %llx token %llx %llx\n", (msg->ev_type == DM_EVENT_READ ? "READ" : (msg->ev_type == DM_EVENT_WRITE ? "WRITE" : (msg->ev_type == DM_EVENT_TRUNCATE ? "TRUNCATE" : "MOUNT"))), sid, msg->ev_token.high, msg->ev_token.low); } // get file handle msgev = DM_GET_VALUE(msg, ev_data, dm_data_event_t *); hanp = DM_GET_VALUE(msgev, de_handle, void *); hlen = DM_GET_LEN(msgev, de_handle); switch (msg->ev_type) { case DM_EVENT_MOUNT: spawn_child(msg->ev_token, hanp, hlen, "MOUNT"); break; case DM_EVENT_READ: spawn_child(msg->ev_token, hanp, hlen, "READ"); break; case DM_EVENT_WRITE: spawn_child(msg->ev_token, hanp, hlen, "WRITE"); break; case DM_EVENT_TRUNCATE: spawn_child(msg->ev_token, hanp, hlen, "TRUNC"); break; default: fprintf(stderr,"Invalid msg type %d\n", msg->ev_type); break; } // go to next event msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *); } } out: if (msgbuf != NULL) free(msgbuf); exit_program(0); }