Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
Signed-off-by: leleliu008 <[email protected]>
  • Loading branch information
leleliu008 committed Apr 24, 2024
1 parent 6ad5e7c commit 3ecfc2c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
96 changes: 53 additions & 43 deletions utils/sysinfo/src/lib/sysinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,27 @@ int sysinfo_libc() {
}
}

size_t muslDynamicLoaderPathLength = osArchLength + 19U;
char muslDynamicLoaderPath[muslDynamicLoaderPathLength];
snprintf(muslDynamicLoaderPath, muslDynamicLoaderPathLength, "/lib/ld-musl-%s.so.1", uts.machine);
size_t muslDynamicLoaderPathCapacity = osArchLength + 19U;
char muslDynamicLoaderPath[muslDynamicLoaderPathCapacity];

int ret = snprintf(muslDynamicLoaderPath, muslDynamicLoaderPathCapacity, "/lib/ld-musl-%s.so.1", uts.machine);

if (ret < 0) {
return -1;
}

if ((stat(muslDynamicLoaderPath, &sb) == 0) && (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))) {
return 2;
}

size_t dynamicLoaderPathLength = osArchLength + 22U;
char dynamicLoaderPath[dynamicLoaderPathLength];
snprintf(dynamicLoaderPath, dynamicLoaderPathLength, "/lib64/ld-linux-%s.so.2", uts.machine);
size_t dynamicLoaderPathCapacity = osArchLength + 22U;
char dynamicLoaderPath[dynamicLoaderPathCapacity];

ret = snprintf(dynamicLoaderPath, dynamicLoaderPathCapacity, "/lib64/ld-linux-%s.so.2", uts.machine);

if (ret < 0) {
return -1;
}

if ((stat(dynamicLoaderPath, &sb) == 0) && (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))) {
return 1;
Expand Down Expand Up @@ -616,47 +626,47 @@ int sysinfo_make(SysInfo * sysinfo) {
sysinfo->arch = strdup(osArch);

if (sysinfo->arch == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}

sysinfo->kind = strdup(osKind);

if (sysinfo->kind == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}

sysinfo->type = strdup(osType);

if (sysinfo->type == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}

sysinfo->code = strdup(osCode);

if (sysinfo->code == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}

sysinfo->name = strdup(osName);

if (sysinfo->name == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}

sysinfo->vers = strdup(osVers);

if (sysinfo->vers == NULL) {
sysinfo_free(*sysinfo);
sysinfo_free(sysinfo);
errno = ENOMEM;
return -1;
}
Expand All @@ -670,53 +680,53 @@ int sysinfo_make(SysInfo * sysinfo) {
return 0;
}

void sysinfo_dump(SysInfo sysinfo) {
printf("sysinfo.ncpu: %u\n", sysinfo.ncpu);
printf("sysinfo.arch: %s\n", (sysinfo.arch == NULL) ? "" : sysinfo.arch);
printf("sysinfo.kind: %s\n", (sysinfo.kind == NULL) ? "" : sysinfo.kind);
printf("sysinfo.type: %s\n", (sysinfo.type == NULL) ? "" : sysinfo.type);
printf("sysinfo.code: %s\n", (sysinfo.code == NULL) ? "" : sysinfo.code);
printf("sysinfo.name: %s\n", (sysinfo.name == NULL) ? "" : sysinfo.name);
printf("sysinfo.vers: %s\n", (sysinfo.vers == NULL) ? "" : sysinfo.vers);
printf("sysinfo.euid: %u\n", sysinfo.euid);
printf("sysinfo.egid: %u\n", sysinfo.egid);

switch(sysinfo.libc) {
void sysinfo_dump(SysInfo * sysinfo) {
printf("sysinfo.ncpu: %u\n", sysinfo->ncpu);
printf("sysinfo.arch: %s\n", (sysinfo->arch == NULL) ? "" : sysinfo->arch);
printf("sysinfo.kind: %s\n", (sysinfo->kind == NULL) ? "" : sysinfo->kind);
printf("sysinfo.type: %s\n", (sysinfo->type == NULL) ? "" : sysinfo->type);
printf("sysinfo.code: %s\n", (sysinfo->code == NULL) ? "" : sysinfo->code);
printf("sysinfo.name: %s\n", (sysinfo->name == NULL) ? "" : sysinfo->name);
printf("sysinfo.vers: %s\n", (sysinfo->vers == NULL) ? "" : sysinfo->vers);
printf("sysinfo.euid: %u\n", sysinfo->euid);
printf("sysinfo.egid: %u\n", sysinfo->egid);

switch(sysinfo->libc) {
case 1: printf("sysinfo.libc: glibc\n"); break;
case 2: printf("sysinfo.libc: musl\n"); break;
case 3: printf("sysinfo.libc: bonic\n"); break;
default: printf("sysinfo.libc: \n");
}
}

void sysinfo_free(SysInfo sysinfo) {
if (sysinfo.arch != NULL) {
free(sysinfo.arch);
sysinfo.arch = NULL;
void sysinfo_free(SysInfo * sysinfo) {
if (sysinfo->arch != NULL) {
free(sysinfo->arch);
sysinfo->arch = NULL;
}

if (sysinfo.kind != NULL) {
free(sysinfo.kind);
sysinfo.kind = NULL;
if (sysinfo->kind != NULL) {
free(sysinfo->kind);
sysinfo->kind = NULL;
}

if (sysinfo.type != NULL) {
free(sysinfo.type);
sysinfo.type = NULL;
if (sysinfo->type != NULL) {
free(sysinfo->type);
sysinfo->type = NULL;
}

if (sysinfo.code != NULL) {
free(sysinfo.code);
sysinfo.code = NULL;
if (sysinfo->code != NULL) {
free(sysinfo->code);
sysinfo->code = NULL;
}

if (sysinfo.name != NULL) {
free(sysinfo.name);
sysinfo.name = NULL;
if (sysinfo->name != NULL) {
free(sysinfo->name);
sysinfo->name = NULL;
}

if (sysinfo.vers != NULL) {
free(sysinfo.vers);
sysinfo.vers = NULL;
if (sysinfo->vers != NULL) {
free(sysinfo->vers);
sysinfo->vers = NULL;
}
}
4 changes: 2 additions & 2 deletions utils/sysinfo/src/lib/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int sysinfo_libc();
int sysinfo_ncpu();

int sysinfo_make(SysInfo * sysinfo);
void sysinfo_dump(SysInfo sysinfo);
void sysinfo_free(SysInfo sysinfo);
void sysinfo_dump(SysInfo * sysinfo);
void sysinfo_free(SysInfo * sysinfo);

#endif
4 changes: 2 additions & 2 deletions utils/sysinfo/src/main/sysinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ int main(int argc, char *argv[]) {
return ret;
}

sysinfo_dump(sysinfo);
sysinfo_free(sysinfo);
sysinfo_dump(&sysinfo);
sysinfo_free(&sysinfo);

return 0;
}
Expand Down

0 comments on commit 3ecfc2c

Please sign in to comment.