1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <lm.h>
#include <iphlpapi.h>
#include "../common/types.h"
#define DRAGENT_WIN_HAL_C_ONLY
#include "win_hal/win_hal_public.h"
#include "scap.h"
#include "scap-int.h"
#include "windows_hal.h"
typedef char* (*fwh_getlasterror)(wh_t* handle);
typedef wh_t* (*fwh_open)(char* error);
typedef void (*fwh_close)(wh_t* handle);
typedef wh_proclist (*fwh_wmi_get_procs)(wh_t* handle);
typedef int (*fwh_is_docker_present)(wh_t* handle);
typedef int (*fwh_docker_refresh)(wh_t* handle);
typedef int (*fwh_wmi_update_procs_perf)(wh_t* handle);
typedef wh_proc_perf_info (*fwh_wmi_get_proc_perf_info)(wh_t* handle, uint64_t pid);
typedef bool (*fwh_create_fd_list)(wh_t* handle);
typedef void (*fwh_free_fd_list)(wh_t* handle);
typedef wh_fdlist (*fwh_get_pid_fds)(wh_t* handle, uint32_t pid);
typedef bool (*fwh_create_tid_list)(wh_t* handle);
typedef void (*fwh_free_tid_list)(wh_t* handle);
typedef wh_tidlist (*fwh_get_pid_tids)(wh_t* handle, uint32_t pid);
fwh_getlasterror p_wh_getlasterror;
fwh_open p_wh_open;
fwh_close p_wh_close;
fwh_wmi_get_procs p_wh_wmi_get_procs;
fwh_is_docker_present p_wh_is_docker_present;
fwh_docker_refresh p_wh_docker_refresh;
fwh_wmi_update_procs_perf p_wh_wmi_update_procs_perf;
fwh_wmi_get_proc_perf_info p_wh_wmi_get_proc_perf_info;
fwh_create_fd_list p_wh_create_fd_list;
fwh_free_fd_list p_wh_free_fd_list;
fwh_get_pid_fds p_wh_get_pid_fds;
fwh_create_tid_list p_wh_create_tid_list;
fwh_free_tid_list p_wh_free_tid_list;
fwh_get_pid_tids p_wh_get_pid_tids;
int32_t scap_windows_hal_import(char* error)
{
HINSTANCE pdll = LoadLibrary("dragent_win_hal.dll");
if(pdll == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "dragent_win_hal.dll not found. Make sure the the windows HAL is installed. Error: %u",
GetLastError());
return SCAP_FAILURE;
}
p_wh_getlasterror = (fwh_getlasterror)GetProcAddress(pdll, "wh_getlasterror");
if(p_wh_getlasterror == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_getlasterror not found");
return SCAP_FAILURE;
}
p_wh_open = (fwh_open)GetProcAddress(pdll, "wh_open");
if(p_wh_open == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_open not found");
return SCAP_FAILURE;
}
p_wh_close = (fwh_close)GetProcAddress(pdll, "wh_close");
if(p_wh_close == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_close not found");
return SCAP_FAILURE;
}
p_wh_wmi_get_procs = (fwh_wmi_get_procs)GetProcAddress(pdll, "wh_wmi_get_procs");
if(p_wh_wmi_get_procs == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_wmi_get_procs not found");
return SCAP_FAILURE;
}
p_wh_is_docker_present = (fwh_is_docker_present)GetProcAddress(pdll, "wh_is_docker_present");
if(p_wh_is_docker_present == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_is_docker_present not found");
return SCAP_FAILURE;
}
p_wh_docker_refresh = (fwh_docker_refresh)GetProcAddress(pdll, "wh_docker_refresh");
if(p_wh_docker_refresh == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_docker_refresh not found");
return SCAP_FAILURE;
}
p_wh_wmi_update_procs_perf = (fwh_wmi_update_procs_perf)GetProcAddress(pdll, "wh_wmi_update_procs_perf");
if(p_wh_wmi_update_procs_perf == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_wmi_update_procs_perf not found");
return SCAP_FAILURE;
}
p_wh_wmi_get_proc_perf_info = (fwh_wmi_get_proc_perf_info)GetProcAddress(pdll, "wh_wmi_get_proc_perf_info");
if(p_wh_wmi_get_proc_perf_info == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_wmi_get_proc_perf_info not found");
return SCAP_FAILURE;
}
p_wh_create_fd_list = (fwh_create_fd_list)GetProcAddress(pdll, "wh_create_fd_list");
if(p_wh_create_fd_list == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_create_fd_list not found");
return SCAP_FAILURE;
}
p_wh_free_fd_list = (fwh_free_fd_list)GetProcAddress(pdll, "wh_free_fd_list");
if(p_wh_free_fd_list == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_free_fd_list not found");
return SCAP_FAILURE;
}
p_wh_get_pid_fds = (fwh_get_pid_fds)GetProcAddress(pdll, "wh_get_pid_fds");
if(p_wh_get_pid_fds == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_get_pid_fds not found");
return SCAP_FAILURE;
}
p_wh_create_tid_list = (fwh_create_tid_list)GetProcAddress(pdll, "wh_create_tid_list");
if(p_wh_create_tid_list == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_create_tid_list not found");
return SCAP_FAILURE;
}
p_wh_free_tid_list = (fwh_free_tid_list)GetProcAddress(pdll, "wh_free_tid_list");
if(p_wh_free_tid_list == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_free_tid_list not found");
return SCAP_FAILURE;
}
p_wh_get_pid_tids = (fwh_get_pid_tids)GetProcAddress(pdll, "wh_get_pid_tids");
if(p_wh_get_pid_tids == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "win hal symbol wh_get_pid_tids not found");
return SCAP_FAILURE;
}
return SCAP_SUCCESS;
}
wh_t* scap_windows_hal_open(char* error)
{
if(p_wh_getlasterror == NULL)
{
if(scap_windows_hal_import(error) != SCAP_SUCCESS)
{
return NULL;
}
}
return p_wh_open(error);
}
void scap_windows_hal_close(wh_t* handle)
{
p_wh_close(handle);
}
void scap_get_machine_info_windows(OUT uint32_t* num_cpus, OUT uint64_t* memory_size_bytes)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
*num_cpus = si.dwNumberOfProcessors;
ULONGLONG mem_kb;
GetPhysicallyInstalledSystemMemory(&mem_kb);
*memory_size_bytes = mem_kb * 1024;
}
int32_t scap_create_userlist_windows(scap_t* handle)
{
LPUSER_INFO_3 resbuf = NULL;
DWORD level = 20;
DWORD maxlen = MAX_PREFERRED_LENGTH;
DWORD eread = 0;
DWORD etot = 0;
DWORD resume_handle = 0;
NET_API_STATUS nueres;
nueres = NetUserEnum(NULL,
level,
FILTER_NORMAL_ACCOUNT, // global users
(LPBYTE*)&resbuf,
maxlen,
&eread,
&etot,
&resume_handle);
//
// Memory allocations
//
handle->m_userlist = (scap_userlist*)malloc(sizeof(scap_userlist));
if(handle->m_userlist == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation failed(1)");
return SCAP_FAILURE;
}
handle->m_userlist->nusers = eread;
handle->m_userlist->ngroups = 1;
handle->m_userlist->totsavelen = 0;
handle->m_userlist->users = (scap_userinfo*)malloc(handle->m_userlist->nusers * sizeof(scap_userinfo));
if(handle->m_userlist->users == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation failed(2)");
free(handle->m_userlist);
return SCAP_FAILURE;
}
handle->m_userlist->groups = (scap_groupinfo*)malloc(handle->m_userlist->ngroups * sizeof(scap_groupinfo));
if(handle->m_userlist->groups == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "grouplist allocation failed(2)");
free(handle->m_userlist->users);
free(handle->m_userlist);
return SCAP_FAILURE;
}
//
// Populate the users
//
for(uint32_t j = 0; j < eread; j++)
{
LPUSER_INFO_3 ui = &(resbuf[j]);
if(ui->usri3_name != NULL)
{
size_t clen = wcstombs(handle->m_userlist->users[j].name, ui->usri3_name, SCAP_MAX_PATH_SIZE);
if(clen == SCAP_MAX_PATH_SIZE)
{
handle->m_userlist->users[j].name[clen - 1] = 0;
}
}
else
{
strcpy(handle->m_userlist->users[j].name, "NA");
}
//
// Disabled because NetUserEnum seems to return a corrupted usri3_home_dir.
// Not a big deal.
//
// clen = wcstombs(handle->m_userlist->users[j].homedir, ui->usri3_home_dir, SCAP_MAX_PATH_SIZE);
// if(clen == SCAP_MAX_PATH_SIZE)
// {
// handle->m_userlist->users[j].name[clen - 1] = 0;
// }
handle->m_userlist->users[j].uid = ui->usri3_user_id;
handle->m_userlist->users[j].gid = ui->usri3_primary_group_id;
handle->m_userlist->users[j].homedir[0] = 0;
handle->m_userlist->users[j].shell[0] = 0;
}
//
// Only one fake group, since windows doesn't have unix groups
//
handle->m_userlist->groups[0].gid = 0;
strcpy(handle->m_userlist->groups[0].name, "NA");
return SCAP_SUCCESS;
}
int32_t scap_create_iflist_windows(scap_t* handle)
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
uint32_t ifcnt4 = 0;
uint32_t ifcnt6 = 0;
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO));
if(pAdapterInfo == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_create_iflist memory allocation error (1)");
return SCAP_FAILURE;
}
//
// Make an initial call to GetAdaptersAddresses to get
// the necessary size into the ulOutBufLen variable
//
DWORD rv, size;
PIP_ADAPTER_ADDRESSES adapter_addresses, aa;
PIP_ADAPTER_UNICAST_ADDRESS ua;
rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);
if(rv != ERROR_BUFFER_OVERFLOW) {
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "GetAdaptersAddresses failed (1)");
return SCAP_FAILURE;
}
adapter_addresses = (PIP_ADAPTER_ADDRESSES)malloc(size);
if(adapter_addresses == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_create_iflist memory allocation error (2)");
return SCAP_FAILURE;
}
//
// Get the interfaces
//
rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
if(rv != ERROR_SUCCESS)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "GetAdaptersAddresses failed (2)");
free(adapter_addresses);
return SCAP_FAILURE;
}
//
// First pass: count the number of interfaces
//
for(aa = adapter_addresses; aa != NULL; aa = aa->Next)
{
if(aa->OperStatus != IfOperStatusUp)
{
//
// Skip disabled interfaces
//
continue;
}
for(ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next)
{
int family = ua->Address.lpSockaddr->sa_family;
if(family == AF_INET)
{
ifcnt4++;
}
else if(family == AF_INET6)
{
//
// IPv6 support not implmented yet
//
//ifcnt6++;
}
}
}
//
// Allocate the handle and the arrays
//
handle->m_addrlist = (scap_addrlist*)malloc(sizeof(scap_addrlist));
if(!handle->m_addrlist)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getifaddrs allocation failed(1)");
free(adapter_addresses);
return SCAP_FAILURE;
}
if(ifcnt4 != 0)
{
handle->m_addrlist->v4list = (scap_ifinfo_ipv4*)malloc(ifcnt4 * sizeof(scap_ifinfo_ipv4));
if(!handle->m_addrlist->v4list)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getifaddrs allocation failed(2)");
free(adapter_addresses);
free(handle->m_addrlist);
return SCAP_FAILURE;
}
}
else
{
handle->m_addrlist->v4list = NULL;
}
if(ifcnt6 != 0)
{
handle->m_addrlist->v6list = (scap_ifinfo_ipv6*)malloc(ifcnt6 * sizeof(scap_ifinfo_ipv6));
if(!handle->m_addrlist->v6list)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getifaddrs allocation failed(3)");
if(handle->m_addrlist->v4list)
{
free(handle->m_addrlist->v4list);
}
free(adapter_addresses);
free(handle->m_addrlist);
return SCAP_FAILURE;
}
}
else
{
handle->m_addrlist->v6list = NULL;
}
handle->m_addrlist->n_v4_addrs = ifcnt4;
handle->m_addrlist->n_v6_addrs = ifcnt6;
//
// Second pass: populate the arrays
//
handle->m_addrlist->totlen = 0;
ifcnt4 = 0;
ifcnt6 = 0;
for(aa = adapter_addresses; aa != NULL; aa = aa->Next)
{
if(aa->OperStatus != IfOperStatusUp)
{
//
// Skip disabled interfaces
//
continue;
}
for(ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next)
{
int family = ua->Address.lpSockaddr->sa_family;
if(family == AF_INET)
{
handle->m_addrlist->v4list[ifcnt4].type = SCAP_II_IPV4;
void* tempAddrPtr = &((struct sockaddr_in *)ua->Address.lpSockaddr->sa_data)->sin_addr;
handle->m_addrlist->v4list[ifcnt4].addr = *(uint32_t*)tempAddrPtr;
if(ua->OnLinkPrefixLength != 0)
{
ConvertLengthToIpv4Mask(ua->OnLinkPrefixLength, &handle->m_addrlist->v4list[ifcnt4].netmask);
}
else
{
handle->m_addrlist->v4list[ifcnt4].netmask = 0;
}
// XXX Not implemented on Windows yet
handle->m_addrlist->v4list[ifcnt4].bcast = 0;
handle->m_addrlist->v4list[ifcnt4].ifnamelen = (uint16_t)wcstombs(handle->m_addrlist->v4list[ifcnt4].ifname, aa->FriendlyName, SCAP_MAX_PATH_SIZE);
handle->m_addrlist->v4list[ifcnt4].linkspeed = 0;
handle->m_addrlist->totlen += (sizeof(scap_ifinfo_ipv4) + handle->m_addrlist->v4list[ifcnt4].ifnamelen - SCAP_MAX_PATH_SIZE);
ifcnt4++;
}
else if(family == AF_INET6)
{
//
// IPv6 support not implmented yet
//
}
}
}
free(adapter_addresses);
return SCAP_SUCCESS;
}
static int32_t addprocess_windows(wh_procinfo* wpi, scap_t* handle, char* error)
{
struct scap_threadinfo* tinfo;
if(handle->m_proc_callback == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "process table construction in scap not supported on windows");
return SCAP_FAILURE;
}
//
// Allocate the procinfo object.
//
if((tinfo = scap_proc_alloc(handle)) == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "addprocess_windows memory allocation error");
return SCAP_FAILURE;
}
//
// Fill the procinfo object
//
memset(tinfo, 0, sizeof(struct scap_threadinfo));
tinfo->pid = wpi->pid;
tinfo->ptid = wpi->ptid;
snprintf(tinfo->comm, SCAP_MAX_PATH_SIZE, "%s", wpi->comm);
snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", wpi->exe);
snprintf(tinfo->exepath, SCAP_MAX_PATH_SIZE, "%s", wpi->exepath);
snprintf(tinfo->args, SCAP_MAX_PATH_SIZE, "%s", wpi->args);
tinfo->args_len = wpi->args_len;
tinfo->vmsize_kb = wpi->vmsize_kb;
tinfo->pfmajor = wpi->pfmajor;
tinfo->pfminor = wpi->pfminor;
tinfo->clone_ts = wpi->clone_ts;
tinfo->tty = wpi->tty;
tinfo->flags = 0;
wh_proc_perf_info pinfo = p_wh_wmi_get_proc_perf_info(handle->m_whh, tinfo->pid);
if(pinfo.m_result != 0)
{
tinfo->vmrss_kb = (uint32_t)(pinfo.m_memory_bytes / 1024);
tinfo->vmswap_kb = (uint32_t)(pinfo.m_swap_bytes / 1024);
}
else
{
tinfo->vmrss_kb = 0;
tinfo->vmswap_kb = 0;
}
wh_tidlist ptl = p_wh_get_pid_tids(handle->m_whh, (uint32_t)tinfo->pid);
if(ptl.m_result != 0)
{
if(ptl.m_count > 1)
{
for(uint32_t j = 1; j < ptl.m_count; j++)
{
tinfo->tid = ptl.m_tids[j];
handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, NULL);
}
}
tinfo->tid = ptl.m_tids[0];
}
else
{
tinfo->tid = tinfo->ptid;
}
wh_fdlist pfl = p_wh_get_pid_fds(handle->m_whh, (uint32_t)tinfo->pid);
if(pfl.m_result != 0)
{
for(uint32_t j = 0; j < pfl.m_count; j++)
{
wh_fdinfo* wfd = &pfl.m_fds[j];
int32_t res = SCAP_SUCCESS;
scap_fdinfo *fdi = NULL;
res = scap_fd_allocate_fdinfo(handle, &fdi, wfd->fd, wfd->type);
if(res == SCAP_FAILURE)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for file fd %" PRIu64, wfd->fd);
break;
}
switch(wfd->type)
{
case WH_FD_FILE:
strncpy(fdi->info.fname,
wfd->info.fname + 4, // the +4 removes the "\\?\" from the beginning of the string
SCAP_MAX_PATH_SIZE - 1);
fdi->info.fname[SCAP_MAX_PATH_SIZE - 1] = 0;
break;
case WH_FD_IPV4_SOCK:
fdi->info.ipv4info.sip = wfd->info.ipv4info.sip;
fdi->info.ipv4info.dip = wfd->info.ipv4info.dip;
fdi->info.ipv4info.sport = wfd->info.ipv4info.sport;
fdi->info.ipv4info.dport = wfd->info.ipv4info.dport;
fdi->info.ipv4info.l4proto = wfd->info.ipv4info.l4proto;
break;
default:
ASSERT(false);
continue;
}
int32_t ares = scap_add_fd_to_proc_table(handle, tinfo, fdi, error);
if(ares != SCAP_SUCCESS)
{
return ares;
}
if(handle->m_proc_callback != NULL)
{
if(fdi)
{
scap_fd_free_fdinfo(&fdi);
}
}
}
}
tinfo->flags |= PPM_CL_IS_MAIN_THREAD;
handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, NULL);
free(tinfo);
return SCAP_SUCCESS;
}
int32_t scap_get_procs_windows(scap_t* handle, char* error)
{
wh_proclist wgpres;
//
// Get the system processes through WMI
//
wgpres = p_wh_wmi_get_procs(handle->m_whh);
if(wgpres.m_result == 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "%s", p_wh_getlasterror(handle->m_whh));
return SCAP_FAILURE;
}
//
// While we're here, refresh the docker state and the process performance table
//
if(p_wh_is_docker_present(handle->m_whh))
{
if (p_wh_docker_refresh(handle->m_whh) == 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "%s", p_wh_getlasterror(handle->m_whh));
return SCAP_FAILURE;
}
}
if(p_wh_wmi_update_procs_perf(handle->m_whh) == 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "%s", p_wh_getlasterror(handle->m_whh));
return SCAP_FAILURE;
}
//
// Scan the system handles and construct the FD list
//
bool cfdres = p_wh_create_fd_list(handle->m_whh);
if(cfdres != WH_SUCCESS)
{
snprintf(error, SCAP_LASTERR_SIZE, "%s", p_wh_getlasterror(handle->m_whh));
return SCAP_FAILURE;
}
//
// Scan the system threads and construct the tid list
//
if(p_wh_create_tid_list(handle->m_whh) == false)
{
snprintf(error, SCAP_LASTERR_SIZE, "%s", p_wh_getlasterror(handle->m_whh));
return SCAP_FAILURE;
}
//
// Add the received processes to the scap list
//
for(uint32_t j = 0; j < wgpres.m_count; j++)
{
wh_procinfo* wpi = &(wgpres.m_procs[j]);
if(addprocess_windows(wpi, handle, error) != SCAP_SUCCESS)
{
p_wh_free_fd_list(handle->m_whh);
p_wh_free_tid_list(handle->m_whh);
return SCAP_FAILURE;
}
}
//
// Done with the file descriptors and tids, release the lists to save memory
//
p_wh_free_fd_list(handle->m_whh);
p_wh_free_tid_list(handle->m_whh);
return SCAP_SUCCESS;
}
|