void *CDiskManage::HotPlugSdCheckThreadFunc(void *arg) { struct sockaddr_nl client; int hotplug_sock = -1; int buffersize = 1024; int ret; tIPC_HOTPLUG_INFO info; CDiskManage *pThisDiskManage = (CDiskManage *)arg; hotplug_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT); if (hotplug_sock == -1) { LOG_DEBUG_MSG_ERROR("", "create sd hotplug socket error !\n"); return NULL; } memset(&client, 0, sizeof(client)); client.nl_family = AF_NETLINK ; client.nl_pid = getpid() ; client.nl_groups = 1 ; //receive broadcast message setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize)); ret = bind(hotplug_sock, (struct sockaddr *)&client, sizeof(client)); if (ret < 0) { LOG_DEBUG_MSG_ERROR("", "bind hotplug socket error !\n"); close(hotplug_sock); return NULL; } while (1) { char buf[UEVENT_BUFFER_SIZE] = { 0 }; struct timeval tv; fd_set fds; int rcvlen, ret; FD_ZERO(&fds); FD_SET(hotplug_sock, &fds); tv.tv_sec = 0; tv.tv_usec = 100 * 1000; ret = select(hotplug_sock + 1, &fds, NULL, NULL, &tv); if(ret < 0) continue; if(!(ret > 0 && FD_ISSET(hotplug_sock, &fds))) continue; //receive data char *end = NULL, *start = NULL; rcvlen = recv(hotplug_sock, &buf, (UEVENT_BUFFER_SIZE - 1), 0); if (rcvlen > 0) { buf[rcvlen] = '\0'; //LOG_DEBUG_MSG_ERROR("", "IPC HotPlug Thread, Original Recv message=%s\n", buf); end = buf + rcvlen; start = buf ; while(start < end) { if (*start == '\0') *start = '#'; start++; } //LOG_DEBUG_MSG_ERROR("", "IPC HotPlug Thread, Recv message=%s\n", buf); pThisDiskManage->DiskManage_AnayseHotplugEventContent(buf, strlen(buf), &info); if (info.plug_event_type != OTHER_PLUG_EVENT) pThisDiskManage->DiskManage_DealIpcSdHotPlug(&info); } usleep(100000); } close( hotplug_sock ); pthread_exit(NULL); } void CDiskManage::DiskManage_AnayseHotplugEventContent(char *buf, int len, tIPC_HOTPLUG_INFO *pHotplutInfo) { char *p = NULL; bool plug_in = false; bool plug_out = false; char szDevName[8]; if (len == 0 || buf[0] == 0x00) { pHotplutInfo->plug_event_type = OTHER_PLUG_EVENT; return; } pHotplutInfo->plug_event_type = OTHER_PLUG_EVENT; if (strncmp(buf, "add", 3) == 0) plug_in = true; //插入 else if (strncmp(buf, "remove", 6) == 0) plug_out = true; //拔出 p = strstr(buf, "#DEVTYPE=disk"); if (p != NULL && strstr(buf, "mmc_host")) { char *q = strstr(buf, "#DEVNAME="); if (q != NULL) { q += strlen("#DEVNAME="); memset(szDevName, 0x0, sizeof(szDevName)); strncpy(szDevName, q, p-q); //printf("---------plug_in=%d, plug_out=%d, szDevName=%s\n", plug_in, plug_out, szDevName); if (plug_in) pHotplutInfo->plug_event_type = SD_PLUG_IN_EVENT; else if(plug_out) pHotplutInfo->plug_event_type = SD_PLUG_OUT_EVENT; strcpy(pHotplutInfo->dev_name, szDevName); return; } } else { pHotplutInfo->plug_event_type = OTHER_PLUG_EVENT; return; } } |
|
来自: 海漩涡 > 《serverStorage》