| | |
| | | #include <paths.h> |
| | | #include <sys/stat.h> |
| | | #include <stdio.h> |
| | | #include <cerrno> |
| | | #include <cstring> |
| | | #include <string> |
| | | #include "../os.h" |
| | | #include "../../base/logger.h" |
| | | |
| | | #include <mntent.h> |
| | | #include <dirent.h> |
| | | #include <sys/utsname.h> |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | #include <valgrind/memcheck.h> |
| | | #endif |
| | | |
| | |
| | | free(hexuuid); |
| | | } |
| | | |
| | | #define MAX_UNITS 20 |
| | | #define MAX_UNITS 40 |
| | | FUNCTION_RETURN getDiskInfos(DiskInfo *diskInfos, size_t *disk_info_size) { |
| | | struct stat mount_stat, sym_stat; |
| | | /*static char discard[1024]; |
| | |
| | | } |
| | | |
| | | currentDrive = 0; |
| | | while (NULL != (ent = getmntent(aFile))) { |
| | | while (NULL != (ent = getmntent(aFile)) && currentDrive < maxDrives) { |
| | | if ((strncmp(ent->mnt_type, "ext", 3) == 0 || strncmp(ent->mnt_type, "xfs", 3) == 0 || |
| | | strncmp(ent->mnt_type, "vfat", 4) == 0 || strncmp(ent->mnt_type, "ntfs", 4) == 0) && |
| | | strncmp(ent->mnt_type, "vfat", 4) == 0 || strncmp(ent->mnt_type, "ntfs", 4) == 0 || |
| | | strncmp(ent->mnt_type, "btr", 3) == 0) && |
| | | ent->mnt_fsname != NULL && strncmp(ent->mnt_fsname, "/dev/", 5) == 0) { |
| | | if (stat(ent->mnt_fsname, &mount_stat) == 0) { |
| | | drive_found = -1; |
| | |
| | | } |
| | | } |
| | | if (drive_found == -1) { |
| | | LOG_DEBUG("mntent: %s %s %d\n", ent->mnt_fsname, ent->mnt_dir, |
| | | LOG_DEBUG("mntent fs:[%s],dir:[%s],inode:[%d]\n", ent->mnt_fsname, ent->mnt_dir, |
| | | (unsigned long int)mount_stat.st_ino); |
| | | strncpy(tmpDrives[currentDrive].device, ent->mnt_fsname, 255 - 1); |
| | | statDrives[currentDrive] = mount_stat.st_ino; |
| | |
| | | } else { |
| | | tmpDrives[drive_found].preferred = 0; |
| | | } |
| | | } else { |
| | | LOG_DEBUG("Error %s during stat of %s \n", std::strerror(errno), ent->mnt_fsname); |
| | | } |
| | | } |
| | | } |
| | |
| | | result = (currentDrive > 0) ? FUNC_RET_OK : FUNC_RET_NOT_AVAIL; |
| | | } else if (*disk_info_size >= currentDrive) { |
| | | disk_by_uuid_dir = opendir("/dev/disk/by-uuid"); |
| | | if (disk_by_uuid_dir == NULL) { |
| | | if (disk_by_uuid_dir == nullptr) { |
| | | LOG_WARN("Open /dev/disk/by-uuid fail"); |
| | | free(statDrives); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | result = FUNC_RET_OK; |
| | | *disk_info_size = currentDrive; |
| | | while ((dir = readdir(disk_by_uuid_dir)) != NULL) { |
| | | strcpy(cur_dir, "/dev/disk/by-uuid/"); |
| | | strncat(cur_dir, dir->d_name, 200); |
| | | if (stat(cur_dir, &sym_stat) == 0) { |
| | | |
| | | while ((dir = readdir(disk_by_uuid_dir)) != nullptr) { |
| | | std::string cur_dir("/dev/disk/by-uuid/"); |
| | | cur_dir += dir->d_name; |
| | | |
| | | bool found = false; |
| | | if (stat(cur_dir.c_str(), &sym_stat) == 0) { |
| | | for (i = 0; i < currentDrive; i++) { |
| | | if (sym_stat.st_ino == statDrives[i]) { |
| | | found = true; |
| | | parseUUID(dir->d_name, tmpDrives[i].disk_sn, sizeof(tmpDrives[i].disk_sn)); |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | VALGRIND_CHECK_VALUE_IS_DEFINED(tmpDrives[i].device); |
| | | |
| | | LOG_DEBUG("uuid %d %s %02x%02x%02x%02x\n", i, tmpDrives[i].device, tmpDrives[i].disk_sn[0], |
| | | LOG_DEBUG("uuid %d %s %02x%02x%02x%02x", i, tmpDrives[i].device, tmpDrives[i].disk_sn[0], |
| | | tmpDrives[i].disk_sn[1], tmpDrives[i].disk_sn[2], tmpDrives[i].disk_sn[3]); |
| | | #endif |
| | | } |
| | | } |
| | | if (!found) { |
| | | LOG_DEBUG("Drive [%s], num [%d] inode [%d] did not match any existing drive", cur_dir.c_str(), |
| | | (unsigned long int)sym_stat.st_ino); |
| | | } |
| | | } else { |
| | | LOG_DEBUG("Error %s during stat of %s", std::strerror(errno), cur_dir.c_str()); |
| | | } |
| | | } |
| | | closedir(disk_by_uuid_dir); |
| | | |
| | | disk_by_label = opendir("/dev/disk/by-label"); |
| | | if (disk_by_label != NULL) { |
| | | while ((dir = readdir(disk_by_label)) != NULL) { |
| | | if (disk_by_label != nullptr) { |
| | | while ((dir = readdir(disk_by_label)) != nullptr) { |
| | | strcpy(cur_dir, "/dev/disk/by-label/"); |
| | | strcat(cur_dir, dir->d_name); |
| | | if (stat(cur_dir, &sym_stat) == 0) { |
| | | for (i = 0; i < currentDrive; i++) { |
| | | if (sym_stat.st_ino == statDrives[i]) { |
| | | strncpy(tmpDrives[i].label, dir->d_name, 255 - 1); |
| | | printf("label %d %s %s\n", i, tmpDrives[i].label, tmpDrives[i].device); |
| | | LOG_DEBUG("label %d %s %s", i, tmpDrives[i].label, tmpDrives[i].device); |
| | | } |
| | | } |
| | | } |
| | |
| | | free(statDrives); |
| | | return result; |
| | | } |
| | | |
| | | static void _getCpuid(unsigned int *p, unsigned int ax) { |
| | | __asm __volatile( |
| | | "movl %%ebx, %%esi\n\t" |
| | | "cpuid\n\t" |
| | | "xchgl %%ebx, %%esi" |
| | | : "=a"(p[0]), "=S"(p[1]), "=c"(p[2]), "=d"(p[3]) |
| | | : "0"(ax)); |
| | | } |
| | | |
| | | FUNCTION_RETURN getCpuId(unsigned char identifier[6]) { |
| | | unsigned int i; |
| | | unsigned int cpuinfo[4] = {0, 0, 0, 0}; |
| | | _getCpuid(cpuinfo, 0); |
| | | for (i = 0; i < 3; i++) { |
| | | identifier[i * 2] = cpuinfo[i] & 0xFF; |
| | | identifier[i * 2 + 1] = (cpuinfo[i] & 0xFF00) >> 8; |
| | | } |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | |
| | | |
| | | FUNCTION_RETURN getMachineName(unsigned char identifier[6]) { |
| | | static struct utsname u; |