Skip to content

Commit

Permalink
utils: add code to get the machine-id on windows machines. (#7970)
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan authored Oct 5, 2023
1 parent d59715e commit 2aa8cdc
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ int flb_utils_set_daemon(struct flb_config *config)
pid_t pid;

if ((pid = fork()) < 0){
flb_error("Failed creating to switch to daemon mode (fork failed)");
flb_error("Failed creating to switch to daemon mode (fork failed)");
exit(EXIT_FAILURE);
}
}

if (pid > 0) { /* parent */
exit(EXIT_SUCCESS);
Expand All @@ -185,7 +185,7 @@ int flb_utils_set_daemon(struct flb_config *config)
if (chdir("/") < 0) { /* make sure we can unmount the inherited filesystem */
flb_error("Unable to unmount the inherited filesystem");
exit(EXIT_FAILURE);
}
}

/* Our last STDOUT messages */
flb_info("switching to background mode (PID=%ld)", (long) getpid());
Expand Down Expand Up @@ -1388,6 +1388,36 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
*out_size = bytes;
return 0;
}
#elif defined(FLB_SYSTEM_WINDOWS)
LSTATUS status;
HKEY hKey = 0;
DWORD dwType = REG_SZ;
char buf[255] = {0};
DWORD dwBufSize = sizeof(buf)-1;

status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography"),
0,
KEY_QUERY_VALUE,
&hKey);

if (status != ERROR_SUCCESS) {
return -1;
}

status = RegQueryValueEx(hKey, TEXT("MachineGuid"), 0, &dwType, (LPBYTE)buf, &dwBufSize );
RegCloseKey(hKey);

if (status == ERROR_SUCCESS) {
*out_id = flb_calloc(1, dwBufSize+1);

if (*out_id == NULL) {
return -1;
}

*out_size = dwBufSize;
return 0;
}
#endif

/* generate a random uuid */
Expand Down

0 comments on commit 2aa8cdc

Please sign in to comment.