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 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
|
/*
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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAS_CAPTURE
#if !defined(CYGWING_AGENT) && !defined(_WIN32)
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "unixid.h"
#endif // CYGWING_AGENT
#endif // HAS_CAPTURE
#include "scap.h"
#include "../../driver/ppm_ringbuffer.h"
#include "scap-int.h"
#if defined(CYGWING_AGENT) || defined(_WIN32)
#include <io.h>
#define R_OK 4
#include <process.h>
#include "windows_hal.h"
#endif
#if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
#define strerror_r(errnum, buf, size) strerror_s(buf, size, errnum)
#endif
#if defined(HAS_CAPTURE)
#if !defined(CYGWING_AGENT) && !defined(_WIN32)
int32_t scap_proc_fill_cwd(scap_t *handle, char* procdirname, struct scap_threadinfo* tinfo)
{
int target_res;
char filename[SCAP_MAX_PATH_SIZE];
snprintf(filename, sizeof(filename), "%scwd", procdirname);
target_res = readlink(filename, tinfo->cwd, sizeof(tinfo->cwd) - 1);
if(target_res <= 0)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "readlink %s failed (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
tinfo->cwd[target_res] = '\0';
return SCAP_SUCCESS;
}
int32_t scap_proc_fill_info_from_stats(scap_t *handle, char* procdirname, struct scap_threadinfo* tinfo)
{
char filename[SCAP_MAX_PATH_SIZE];
uint32_t nfound = 0;
int64_t tmp;
uint32_t uid;
uint64_t tgid;
uint64_t ppid;
uint64_t vpid;
uint64_t vtid;
int64_t sid;
int64_t pgid;
int64_t vpgid;
uint32_t vmsize_kb;
uint32_t vmrss_kb;
uint32_t vmswap_kb;
uint64_t pfmajor;
uint64_t pfminor;
int32_t tty;
char line[512];
char tmpc;
char* s;
tinfo->uid = (uint32_t)-1;
tinfo->ptid = (uint32_t)-1LL;
tinfo->sid = 0;
tinfo->vpgid = 0;
tinfo->vmsize_kb = 0;
tinfo->vmrss_kb = 0;
tinfo->vmswap_kb = 0;
tinfo->pfmajor = 0;
tinfo->pfminor = 0;
tinfo->filtered_out = 0;
tinfo->tty = 0;
snprintf(filename, sizeof(filename), "%sstatus", procdirname);
FILE* f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "open status file %s failed (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
while(fgets(line, sizeof(line), f) != NULL)
{
if(strstr(line, "Tgid") == line)
{
nfound++;
if(sscanf(line, "Tgid: %" PRIu64, &tgid) == 1)
{
tinfo->pid = tgid;
}
else
{
ASSERT(false);
}
}
if(strstr(line, "Uid") == line)
{
nfound++;
if(sscanf(line, "Uid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2)
{
tinfo->uid = uid;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "Gid") == line)
{
nfound++;
if(sscanf(line, "Gid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2)
{
tinfo->gid = uid;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "PPid") == line)
{
nfound++;
if(sscanf(line, "PPid: %" PRIu64, &ppid) == 1)
{
tinfo->ptid = ppid;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmSize:") == line)
{
nfound++;
if(sscanf(line, "VmSize: %" PRIu32, &vmsize_kb) == 1)
{
tinfo->vmsize_kb = vmsize_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmRSS:") == line)
{
nfound++;
if(sscanf(line, "VmRSS: %" PRIu32, &vmrss_kb) == 1)
{
tinfo->vmrss_kb = vmrss_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmSwap:") == line)
{
nfound++;
if(sscanf(line, "VmSwap: %" PRIu32, &vmswap_kb) == 1)
{
tinfo->vmswap_kb = vmswap_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "NSpid:") == line)
{
nfound++;
if(sscanf(line, "NSpid: %*u %" PRIu64, &vtid) == 1)
{
tinfo->vtid = vtid;
}
else
{
tinfo->vtid = tinfo->tid;
}
}
else if(strstr(line, "NSpgid:") == line)
{
nfound++;
if(sscanf(line, "NSpgid: %*u %" PRIu64, &vpgid) == 1)
{
tinfo->vpgid = vpgid;
}
}
else if(strstr(line, "NStgid:") == line)
{
nfound++;
if(sscanf(line, "NStgid: %*u %" PRIu64, &vpid) == 1)
{
tinfo->vpid = vpid;
}
else
{
tinfo->vpid = tinfo->pid;
}
}
if(nfound == 10)
{
break;
}
}
ASSERT(nfound == 10 || nfound == 7 || nfound == 6);
fclose(f);
snprintf(filename, sizeof(filename), "%sstat", procdirname);
f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "read stat file %s failed (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
size_t ssres = fread(line, 1, sizeof(line) - 1, f);
if(ssres == 0)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read from stat file %s (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
line[ssres] = 0;
s = strrchr(line, ')');
if(s == NULL)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find closing bracket in stat file %s",
filename);
return SCAP_FAILURE;
}
//
// Extract the line content
//
if(sscanf(s + 2, "%c %" PRId64 " %" PRId64 " %" PRId64 " %" PRId32 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
&tmpc,
&tmp,
&pgid,
&sid,
&tty,
&tmp,
&tmp,
&pfminor,
&tmp,
&pfmajor) != 10)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read expected fields from stat file %s",
filename);
return SCAP_FAILURE;
}
tinfo->pfmajor = pfmajor;
tinfo->pfminor = pfminor;
tinfo->sid = (uint64_t) sid;
// If we did not find vpgid above, set it to pgid from the
// global namespace.
if(tinfo->vpgid == 0)
{
tinfo->vpgid = pgid;
}
tinfo->tty = tty;
fclose(f);
return SCAP_SUCCESS;
}
//
// use prlimit to extract the RLIMIT_NOFILE for the tid. On systems where prlimit
// is not supported, just return -1
//
static int32_t scap_proc_fill_flimit(scap_t *handle, uint64_t tid, struct scap_threadinfo* tinfo)
#ifdef SYS_prlimit64
{
struct rlimit rl;
#ifdef __NR_prlimit64
if(syscall(SYS_prlimit64, tid, RLIMIT_NOFILE, NULL, &rl) == 0)
{
tinfo->fdlimit = rl.rlim_cur;
return SCAP_SUCCESS;
}
#endif
tinfo->fdlimit = -1;
return SCAP_SUCCESS;
}
#else
{
tinfo->fdlimit = -1;
return SCAP_SUCCESS;
}
#endif
int32_t scap_proc_fill_cgroups(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname)
{
char filename[SCAP_MAX_PATH_SIZE];
char line[SCAP_MAX_CGROUPS_SIZE];
tinfo->cgroups_len = 0;
snprintf(filename, sizeof(filename), "%scgroup", procdirname);
if(access(filename, R_OK) == -1)
{
return SCAP_SUCCESS;
}
FILE* f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "open cgroup file %s failed (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
while(fgets(line, sizeof(line), f) != NULL)
{
char* token;
char* subsys_list;
char* cgroup;
char* scratch;
// Default subsys list for cgroups v2 unified hierarchy.
// These are the ones we actually use in cri container engine.
char default_subsys_list[] = "cpu,memory,cpuset";
// id
token = strtok_r(line, ":", &scratch);
if(token == NULL)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find id in cgroup file %s",
filename);
return SCAP_FAILURE;
}
// subsys
subsys_list = strtok_r(NULL, ":", &scratch);
if(subsys_list == NULL)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find subsys in cgroup file %s",
filename);
return SCAP_FAILURE;
}
// Hack to detect empty fields, because strtok does not support it
// strsep() should be used to fix this but it's not available
// on CentOS 6 (has been added from Glibc 2.19)
if(subsys_list-token-strlen(token) > 1)
{
// Subsys list empty (ie: it contains cgroup path instead)!
//
// See https://man7.org/linux/man-pages/man7/cgroups.7.html:
// 5:cpuacct,cpu,cpuset:/daemons
//
// The colon-separated fields are, from left to right:
//
// 1. For cgroups version 1 hierarchies, this field contains
// a unique hierarchy ID number that can be matched to a
// hierarchy ID in /proc/cgroups. For the cgroups version
// 2 hierarchy, this field contains the value 0.
//
// 2. For cgroups version 1 hierarchies, this field contains
// a comma-separated list of the controllers bound to the
// hierarchy. For the cgroups version 2 hierarchy, this
// field is empty.
//
// 3. This field contains the pathname of the control group
// in the hierarchy to which the process belongs. This
// pathname is relative to the mount point of the
// hierarchy.
//
// -> for cgroup2: id is always 0 and subsys list is always empty (single unified hierarchy)
// -> for cgroup1: skip subsys empty because it means controller is not mounted on any hierarchy
if (handle->m_cgroup_version == 2 && strcmp(token, "0") == 0)
{
cgroup = subsys_list;
subsys_list = default_subsys_list; // force-set a default subsys list
} else
{
// skip cgroups like this:
// 0::/init.scope
continue;
}
} else
{
// cgroup should be the only thing remaining so use newline as the delimiter.
cgroup = strtok_r(NULL, "\n", &scratch);
if(cgroup == NULL)
{
ASSERT(false);
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find cgroup in cgroup file %s",
filename);
return SCAP_FAILURE;
}
}
while((token = strtok_r(subsys_list, ",", &scratch)) != NULL)
{
subsys_list = NULL;
if(strlen(cgroup) + 1 + strlen(token) + 1 > SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len)
{
ASSERT(false);
fclose(f);
return SCAP_SUCCESS;
}
snprintf(tinfo->cgroups + tinfo->cgroups_len, SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len, "%s=%s", token, cgroup);
tinfo->cgroups_len += strlen(cgroup) + 1 + strlen(token) + 1;
}
}
fclose(f);
return SCAP_SUCCESS;
}
static int32_t scap_get_vtid(scap_t* handle, int64_t tid, int64_t *vtid)
{
if(handle->m_mode != SCAP_MODE_LIVE)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get vtid (not in live mode)");
return SCAP_FAILURE;
}
#if !defined(HAS_CAPTURE)
ASSERT(false)
return SCAP_FAILURE;
#else
if(handle->m_bpf || handle->m_udig)
{
*vtid = 0;
}
else
{
*vtid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_VTID, tid);
if(*vtid == -1)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get vtid failed (%s)",
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
}
return SCAP_SUCCESS;
#endif
}
static int32_t scap_get_vpid(scap_t* handle, int64_t tid, int64_t *vpid)
{
if(handle->m_mode != SCAP_MODE_LIVE)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get vtid (not in live mode)");
return SCAP_FAILURE;
}
#if !defined(HAS_CAPTURE)
ASSERT(false)
return SCAP_FAILURE;
#else
if(handle->m_bpf || handle->m_udig)
{
*vpid = 0;
}
else
{
*vpid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_VPID, tid);
if(*vpid == -1)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get vpid failed (%s)",
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
}
return SCAP_SUCCESS;
#endif
}
int32_t scap_proc_fill_root(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname)
{
char root_path[SCAP_MAX_PATH_SIZE];
snprintf(root_path, sizeof(root_path), "%sroot", procdirname);
if ( readlink(root_path, tinfo->root, sizeof(tinfo->root)) > 0)
{
return SCAP_SUCCESS;
}
else
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "readlink %s failed (%s)",
root_path, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
}
int32_t scap_proc_fill_loginuid(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname)
{
uint32_t loginuid;
char loginuid_path[SCAP_MAX_PATH_SIZE];
char line[512];
snprintf(loginuid_path, sizeof(loginuid_path), "%sloginuid", procdirname);
FILE* f = fopen(loginuid_path, "r");
if(f == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Open loginuid file %s failed (%s)",
loginuid_path, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
if (fgets(line, sizeof(line), f) == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read loginuid from %s (%s)",
loginuid_path, scap_strerror(handle, errno));
fclose(f);
return SCAP_FAILURE;
}
fclose(f);
if(sscanf(line, "%" PRId32, &loginuid) == 1)
{
tinfo->loginuid = loginuid;
return SCAP_SUCCESS;
}
else
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read loginuid from %s",
loginuid_path);
return SCAP_FAILURE;
}
}
int32_t scap_proc_fill_exe_writable(scap_t* handle, struct scap_threadinfo* tinfo, uint32_t uid, uint32_t gid, const char *procdirname, const char *exetarget)
{
char proc_exe_path[SCAP_MAX_PATH_SIZE];
struct stat targetstat;
snprintf(proc_exe_path, sizeof(proc_exe_path), "%sroot%s", procdirname, exetarget);
// if the file doesn't exist we can't determine if it was writable, assume false
if(stat(proc_exe_path, &targetstat) < 0)
{
return SCAP_SUCCESS;
}
// if you're the user owning the file you can chmod, so you can effectively write to it
if(targetstat.st_uid == uid) {
tinfo->exe_writable = true;
return SCAP_SUCCESS;
}
uid_t orig_uid = geteuid();
uid_t orig_gid = getegid();
//
// In order to check whether the current user can access the file we need to temporarily
// set the effective uid and gid of our thread to the target ones and then check access,
// but keep in mind that:
// - seteuid()/setegid() libc functions change the euid/egid of the whole process, not just
// the current thread
// - setfsuid()/setfsgid() operate on threads but cannot be paired with access(),
// so we would need to open() the file, but opening executable files in use may result
// in "text file busy" errors
//
// Therefore we need to directly call the appropriate setresuid syscall that operate on threads,
// implemented in the thread_seteuid() and thread_setegid() functions.
//
if(thread_seteuid(uid) != -1 && thread_setegid(gid) != -1) {
if(faccessat(0, proc_exe_path, W_OK, AT_EACCESS) == 0) {
tinfo->exe_writable = true;
}
}
if(thread_seteuid(orig_uid) == -1)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not restore original euid from %d to %d",
uid, orig_uid);
return SCAP_FAILURE;
}
if(thread_setegid(orig_gid) == -1)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not restore original egid from %d to %d",
gid, orig_gid);
return SCAP_FAILURE;
}
return SCAP_SUCCESS;
}
//
// Add a process to the list by parsing its entry under /proc
//
static int32_t scap_proc_add_from_proc(scap_t* handle, uint32_t tid, char* procdirname, struct scap_ns_socket_list** sockets_by_ns, scap_threadinfo** procinfo, char *error)
{
char dir_name[256];
char target_name[SCAP_MAX_PATH_SIZE];
int target_res;
char filename[252];
char line[SCAP_MAX_ENV_SIZE];
struct scap_threadinfo* tinfo;
int32_t uth_status = SCAP_SUCCESS;
FILE* f;
size_t filesize;
size_t exe_len;
bool free_tinfo = false;
int32_t res = SCAP_SUCCESS;
struct stat dirstat;
if (handle->m_cgroup_version == 0)
{
snprintf(dir_name, sizeof(dir_name), "%s/filesystems", procdirname);
f = fopen(dir_name, "r");
if (f)
{
while(fgets(line, sizeof(line), f) != NULL)
{
// NOTE: we do not support mixing cgroups v1 v2 controllers.
// Neither docker nor podman support this: https://github.com/docker/for-linux/issues/1256
if (strstr(line, "cgroup2"))
{
handle->m_cgroup_version = 2;
break;
}
if (strstr(line, "cgroup"))
{
handle->m_cgroup_version = 1;
}
}
fclose(f);
} else
{
ASSERT(false);
snprintf(error, SCAP_LASTERR_SIZE, "failed to fetch cgroup version information");
return SCAP_FAILURE;
}
}
snprintf(dir_name, sizeof(dir_name), "%s/%u/", procdirname, tid);
snprintf(filename, sizeof(filename), "%sexe", dir_name);
//
// Gather the executable full name
//
target_res = readlink(filename, target_name, sizeof(target_name) - 1); // Getting the target of the exe, i.e. to which binary it points to
if(target_res <= 0)
{
//
// No exe. This either
// - a kernel thread (if there is no cmdline). In that case we skip it.
// - a process that has been containerized or has some weird thing going on. In that case
// we accept it.
//
snprintf(filename, sizeof(filename), "%scmdline", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
return SCAP_SUCCESS;
}
ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE);
if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL)
{
fclose(f);
return SCAP_SUCCESS;
}
else
{
fclose(f);
}
target_name[0] = 0;
}
else
{
// null-terminate target_name (readlink() does not append a null byte)
target_name[target_res] = 0;
}
//
// This is a real user level process. Allocate the procinfo structure.
//
if((tinfo = scap_proc_alloc(handle)) == NULL)
{
// Error message saved in handle->m_lasterr
snprintf(error, SCAP_LASTERR_SIZE, "can't allocate procinfo struct: %s", handle->m_lasterr);
return SCAP_FAILURE;
}
tinfo->tid = tid;
tinfo->fdlist = NULL;
//
// Gathers the exepath
//
snprintf(tinfo->exepath, sizeof(tinfo->exepath), "%s", target_name);
//
// Gather the command name
//
snprintf(filename, sizeof(filename), "%sstatus", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't open %s (error %s)", filename, scap_strerror(handle, errno));
free(tinfo);
return SCAP_FAILURE;
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE);
if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't read from %s (%s)",
filename, scap_strerror(handle, errno));
fclose(f);
free(tinfo);
return SCAP_FAILURE;
}
line[SCAP_MAX_PATH_SIZE - 1] = 0;
sscanf(line, "Name:%1024s", tinfo->comm);
fclose(f);
}
bool suppressed;
if ((res = scap_update_suppressed(handle, tinfo->comm, tid, 0, &suppressed)) != SCAP_SUCCESS)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't update set of suppressed tids (%s)", handle->m_lasterr);
free(tinfo);
return res;
}
if (suppressed && !procinfo)
{
free(tinfo);
return SCAP_SUCCESS;
}
//
// Gather the command line
//
snprintf(filename, sizeof(filename), "%scmdline", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't open cmdline file %s (%s)",
filename, scap_strerror(handle, errno));
free(tinfo);
return SCAP_FAILURE;
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_ARGS_SIZE);
filesize = fread(line, 1, SCAP_MAX_ARGS_SIZE - 1, f);
if(filesize > 0)
{
line[filesize] = 0;
exe_len = strlen(line);
if(exe_len < filesize)
{
++exe_len;
}
snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", line);
tinfo->args_len = filesize - exe_len;
memcpy(tinfo->args, line + exe_len, tinfo->args_len);
tinfo->args[SCAP_MAX_ARGS_SIZE - 1] = 0;
}
else
{
tinfo->args[0] = 0;
tinfo->exe[0] = 0;
}
fclose(f);
}
//
// Gather the environment
//
snprintf(filename, sizeof(filename), "%senviron", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't open environ file %s (%s)",
filename, scap_strerror(handle, errno));
free(tinfo);
return SCAP_FAILURE;
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_ENV_SIZE);
filesize = fread(line, 1, SCAP_MAX_ENV_SIZE, f);
if(filesize > 0)
{
line[filesize - 1] = 0;
tinfo->env_len = filesize;
memcpy(tinfo->env, line, tinfo->env_len);
tinfo->env[SCAP_MAX_ENV_SIZE - 1] = 0;
}
else
{
tinfo->env[0] = 0;
}
fclose(f);
}
//
// set the current working directory of the process
//
if(SCAP_FAILURE == scap_proc_fill_cwd(handle, dir_name, tinfo))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill cwd for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
//
// extract the user id and ppid from /proc/pid/status
//
if(SCAP_FAILURE == scap_proc_fill_info_from_stats(handle, dir_name, tinfo))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill uid and pid for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
//
// Set the file limit
//
if(SCAP_FAILURE == scap_proc_fill_flimit(handle, tinfo->tid, tinfo))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill flimit for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
if(scap_proc_fill_cgroups(handle, tinfo, dir_name) == SCAP_FAILURE)
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill cgroups for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
// These values should be read already from /status file, leave these
// fallback functions for older kernels < 4.1
if(tinfo->vtid == 0 && scap_get_vtid(handle, tinfo->tid, &tinfo->vtid) == SCAP_FAILURE)
{
tinfo->vtid = tinfo->tid;
}
if(tinfo->vpid == 0 && scap_get_vpid(handle, tinfo->tid, &tinfo->vpid) == SCAP_FAILURE)
{
tinfo->vpid = tinfo->pid;
}
//
// set the current root of the process
//
if(SCAP_FAILURE == scap_proc_fill_root(handle, tinfo, dir_name))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill root for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
//
// set the loginuid
//
if(SCAP_FAILURE == scap_proc_fill_loginuid(handle, tinfo, dir_name))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill loginuid for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
if(stat(dir_name, &dirstat) == 0)
{
tinfo->clone_ts = dirstat.st_ctim.tv_sec*1000000000 + dirstat.st_ctim.tv_nsec;
}
// If tid is different from pid, assume this is a thread and that the FDs are shared, and set the
// corresponding process flags.
// XXX we should see if the process creation flags are stored somewhere in /proc and handle this
// properly instead of making assumptions.
//
if(tinfo->tid == tinfo->pid)
{
tinfo->flags = 0;
}
else
{
tinfo->flags = PPM_CL_CLONE_THREAD | PPM_CL_CLONE_FILES;
}
if(SCAP_FAILURE == scap_proc_fill_exe_writable(handle, tinfo, tinfo->uid, tinfo->gid, dir_name, target_name))
{
snprintf(error, SCAP_LASTERR_SIZE, "can't fill exe writable access for %s (%s)",
dir_name, handle->m_lasterr);
free(tinfo);
return SCAP_FAILURE;
}
//
// if procinfo is set we assume this is a runtime lookup so no
// need to use the table
//
if(!procinfo)
{
//
// Done. Add the entry to the process table, or fire the notification callback
//
if(handle->m_proc_callback == NULL)
{
HASH_ADD_INT64(handle->m_proclist, tid, tinfo);
if(uth_status != SCAP_SUCCESS)
{
snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)");
free(tinfo);
return SCAP_FAILURE;
}
}
else
{
handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, NULL);
free_tinfo = true;
}
}
else
{
*procinfo = tinfo;
}
//
// Only add fds for processes, not threads
//
if(tinfo->pid == tinfo->tid)
{
res = scap_fd_scan_fd_dir(handle, dir_name, tinfo, sockets_by_ns, error);
}
if(free_tinfo)
{
free(tinfo);
}
return res;
}
//
// Read a single thread info from /proc
//
int32_t scap_proc_read_thread(scap_t* handle, char* procdirname, uint64_t tid, struct scap_threadinfo** pi, char *error, bool scan_sockets)
{
struct scap_ns_socket_list* sockets_by_ns = NULL;
int32_t res;
char add_error[SCAP_LASTERR_SIZE];
if(!scan_sockets)
{
sockets_by_ns = (void*)-1;
}
res = scap_proc_add_from_proc(handle, tid, procdirname, &sockets_by_ns, pi, add_error);
if(res != SCAP_SUCCESS)
{
snprintf(error, SCAP_LASTERR_SIZE, "cannot add proc tid = %"PRIu64", dirname = %s, error=%s", tid, procdirname, add_error);
}
if(sockets_by_ns != NULL && sockets_by_ns != (void*)-1)
{
scap_fd_free_ns_sockets_list(handle, &sockets_by_ns);
}
return res;
}
//
// Scan a directory containing multiple processes under /proc
//
static int32_t _scap_proc_scan_proc_dir_impl(scap_t* handle, char* procdirname, int parenttid, char *error)
{
DIR *dir_p;
struct dirent *dir_entry_p;
scap_threadinfo* tinfo;
uint64_t tid;
int32_t res = SCAP_SUCCESS;
char childdir[SCAP_MAX_PATH_SIZE];
struct scap_ns_socket_list* sockets_by_ns = NULL;
dir_p = opendir(procdirname);
if(dir_p == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "error opening the %s directory (%s)",
procdirname, scap_strerror(handle, errno));
return SCAP_NOTFOUND;
}
while((dir_entry_p = readdir(dir_p)) != NULL)
{
if(strspn(dir_entry_p->d_name, "0123456789") != strlen(dir_entry_p->d_name))
{
continue;
}
//
// Gather the process TID, which is the directory name
//
tid = atoi(dir_entry_p->d_name);
//
// Skip the main thread entry
//
if(parenttid != -1 && tid == parenttid)
{
continue;
}
//
// This is the initial /proc scan so duplicate threads
// are an error, or at least unexpected. Check the process
// list to see if we've encountered this tid already
//
HASH_FIND_INT64(handle->m_proclist, &tid, tinfo);
if(tinfo != NULL)
{
ASSERT(false);
snprintf(error, SCAP_LASTERR_SIZE, "duplicate process %"PRIu64, tid);
res = SCAP_FAILURE;
break;
}
char add_error[SCAP_LASTERR_SIZE];
//
// We have a process that needs to be explored
//
res = scap_proc_add_from_proc(handle, tid, procdirname, &sockets_by_ns, NULL, add_error);
if(res != SCAP_SUCCESS)
{
//
// When a /proc lookup fails (while scanning the whole directory,
// not just while looking up a single tid),
// we should drop this thread/process completely.
// We will fill the gap later, when the first event
// for that process arrives.
//
//
res = SCAP_SUCCESS;
//
// Continue because if we failed to read details of pid=1234,
// it doesn’t say anything about pid=1235
//
continue;
}
//
// See if this process includes tasks that need to be added
// Note the use of recursion will re-enter this function for the childdir.
//
if(parenttid == -1 && handle->m_mode != SCAP_MODE_NODRIVER)
{
snprintf(childdir, sizeof(childdir), "%s/%u/task", procdirname, (int)tid);
if(_scap_proc_scan_proc_dir_impl(handle, childdir, tid, error) == SCAP_FAILURE)
{
res = SCAP_FAILURE;
break;
}
}
}
closedir(dir_p);
if(sockets_by_ns != NULL && sockets_by_ns != (void*)-1)
{
scap_fd_free_ns_sockets_list(handle, &sockets_by_ns);
}
return res;
}
int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, char *error)
{
return _scap_proc_scan_proc_dir_impl(handle, procdirname, -1, error);
}
#endif // CYGWING_AGENT
int32_t scap_getpid_global(scap_t* handle, int64_t* pid)
{
#if !defined(CYGWING_AGENT) && !defined(_WIN32)
if(handle->m_mode != SCAP_MODE_LIVE)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get pid (not in live mode)");
ASSERT(false);
return SCAP_FAILURE;
}
#if !defined(HAS_CAPTURE)
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get pid (capture not enabled)");
return SCAP_FAILURE;
#else
if(handle->m_bpf || handle->m_udig)
{
char filename[SCAP_MAX_PATH_SIZE];
char line[512];
snprintf(filename, sizeof(filename), "%s/proc/self/status", scap_get_host_root());
FILE* f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can not open status file %s (%s)",
filename, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
while(fgets(line, sizeof(line), f) != NULL)
{
if(sscanf(line, "Tgid: %" PRId64, pid) == 1)
{
fclose(f);
return SCAP_SUCCESS;
}
}
fclose(f);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "could not find tgid in status file %s",
filename);
return SCAP_FAILURE;
}
else
{
*pid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_CURRENT_PID);
if(*pid == -1)
{
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get pid failed (%s)",
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
}
return SCAP_SUCCESS;
#endif
#else // CYGWING_AGENT
*pid = _getpid();
return SCAP_SUCCESS;
#endif // CYGWING_AGENT
}
#endif // HAS_CAPTURE
#if defined(CYGWING_AGENT) || defined(_WIN32)
int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, char *error)
{
return scap_get_procs_windows(handle, error);
}
#endif
//
// Delete a process entry
//
void scap_proc_delete(scap_t* handle, scap_threadinfo* proc)
{
//
// First, free the fd table for this process descriptor
//
scap_fd_free_proc_fd_table(handle, proc);
//
// Second, remove the process descriptor from the table
//
HASH_DEL(handle->m_proclist, proc);
//
// Third, free the memory
//
free(proc);
}
//
// Free the process table
//
void scap_proc_free_table(scap_t* handle)
{
struct scap_threadinfo* tinfo;
struct scap_threadinfo* ttinfo;
HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo)
{
scap_proc_delete(handle, tinfo);
}
}
struct scap_threadinfo* scap_proc_get(scap_t* handle, int64_t tid, bool scan_sockets)
{
#if !defined(HAS_CAPTURE) || defined(_WIN32)
return NULL;
#else
//
// No /proc parsing for offline captures
//
if(handle->m_mode == SCAP_MODE_CAPTURE)
{
return NULL;
}
struct scap_threadinfo* tinfo = NULL;
char filename[SCAP_MAX_PATH_SIZE];
snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root());
if(scap_proc_read_thread(handle, filename, tid, &tinfo, handle->m_lasterr, scan_sockets) != SCAP_SUCCESS)
{
free(tinfo);
return NULL;
}
return tinfo;
#endif // HAS_CAPTURE
}
bool scap_is_thread_alive(scap_t* handle, int64_t pid, int64_t tid, const char* comm)
{
#if !defined(HAS_CAPTURE)
return false;
#else
char charbuf[SCAP_MAX_PATH_SIZE];
FILE* f;
//
// No /proc parsing for offline captures
//
if(handle->m_mode == SCAP_MODE_CAPTURE)
{
return false;
}
snprintf(charbuf, sizeof(charbuf), "%s/proc/%" PRId64 "/task/%" PRId64 "/comm", scap_get_host_root(), pid, tid);
f = fopen(charbuf, "r");
if(f != NULL)
{
if(fgets(charbuf, sizeof(charbuf), f) != NULL)
{
if(strncmp(charbuf, comm, strlen(comm)) == 0)
{
fclose(f);
return true;
}
}
fclose(f);
}
else
{
//
// If /proc/<pid>/task/<tid>/comm does not exist but /proc/<pid>/task/<tid>/exe does exist, we assume we're on an ancient
// OS like RHEL5 and we return true.
// This could generate some false positives on such old distros, and we're going to accept it.
//
snprintf(charbuf, sizeof(charbuf), "%s/proc/%" PRId64 "/task/%" PRId64 "/exe", scap_get_host_root(), pid, tid);
f = fopen(charbuf, "r");
if(f != NULL)
{
fclose(f);
return true;
}
}
return false;
#endif // HAS_CAPTURE
}
#if defined(HAS_CAPTURE)
int scap_proc_scan_proc_table(scap_t *handle)
{
char filename[SCAP_MAX_PATH_SIZE];
//
// Create the process list
//
handle->m_lasterr[0] = '\0';
snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root());
return scap_proc_scan_proc_dir(handle, filename, handle->m_lasterr);
}
void scap_refresh_proc_table(scap_t* handle)
{
if(handle->m_proclist)
{
scap_proc_free_table(handle);
handle->m_proclist = NULL;
}
scap_proc_scan_proc_table(handle);
}
#else
void scap_refresh_proc_table(scap_t* handle)
{
}
#endif // HAS_CAPTURE
struct scap_threadinfo *scap_proc_alloc(scap_t *handle)
{
struct scap_threadinfo *tinfo = (struct scap_threadinfo*) calloc(1, sizeof(scap_threadinfo));
if(tinfo == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (1)");
return NULL;
}
return tinfo;
}
void scap_proc_free(scap_t* handle, struct scap_threadinfo* proc)
{
scap_fd_free_proc_fd_table(handle, proc);
free(proc);
}
int32_t scap_proc_add(scap_t* handle, uint64_t tid, scap_threadinfo* tinfo)
{
int32_t uth_status = SCAP_SUCCESS;
HASH_ADD_INT64(handle->m_proclist, tid, tinfo);
if(uth_status == SCAP_SUCCESS)
{
return SCAP_SUCCESS;
}
else
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not add tid to hash table");
return SCAP_FAILURE;
}
}
int32_t scap_fd_add(scap_t *handle, scap_threadinfo* tinfo, uint64_t fd, scap_fdinfo* fdinfo)
{
int32_t uth_status = SCAP_SUCCESS;
HASH_ADD_INT64(tinfo->fdlist, fd, fdinfo);
if(uth_status == SCAP_SUCCESS)
{
return SCAP_SUCCESS;
}
else
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not add fd to hash table");
return SCAP_FAILURE;
}
}
//
// Internal helper functions to output the process table to screen
//
void scap_proc_print_info(scap_t *handle, scap_threadinfo* tinfo)
{
fprintf(stderr, "TID:%"PRIu64" PID:%"PRIu64" FLAGS:%"PRIu32" COMM:%s EXE:%s ARGS:%s CWD:%s FLIMIT:%" PRId64 "\n", tinfo->tid, tinfo->pid, tinfo->flags,tinfo->comm, tinfo->exe, tinfo->args, tinfo->cwd, tinfo->fdlimit);
scap_fd_print_table(handle, tinfo);
}
void scap_proc_print_proc_by_tid(scap_t* handle, uint64_t tid)
{
scap_threadinfo* tinfo;
scap_threadinfo* ttinfo;
HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo)
{
if(tinfo->tid == tid)
{
scap_proc_print_info(handle, tinfo);
}
}
}
void scap_proc_print_table(scap_t* handle)
{
scap_threadinfo* tinfo;
scap_threadinfo* ttinfo;
printf("************** PROCESS TABLE **************\n");
HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo)
{
scap_proc_print_info(handle, tinfo);
}
printf("*******************************************\n");
}
const char *scap_strerror(scap_t *handle, int errnum)
{
int rc;
if((rc = strerror_r(errnum, handle->m_strerror_buf, SCAP_LASTERR_SIZE) != 0))
{
if(rc != ERANGE)
{
snprintf(handle->m_strerror_buf, SCAP_LASTERR_SIZE, "Errno %d", errnum);
}
}
return handle->m_strerror_buf;
}
int32_t scap_update_suppressed(scap_t *handle,
const char *comm,
uint64_t tid, uint64_t ptid,
bool *suppressed)
{
uint32_t i;
scap_tid *stid;
*suppressed = false;
HASH_FIND_INT64(handle->m_suppressed_tids, &ptid, stid);
if(stid != NULL)
{
*suppressed = true;
}
else
{
for(i=0; i < handle->m_num_suppressed_comms; i++)
{
if(strcmp(handle->m_suppressed_comms[i], comm) == 0)
{
*suppressed = true;
break;
}
}
}
// Also check to see if the tid is already in the set of
// suppressed tids.
HASH_FIND_INT64(handle->m_suppressed_tids, &tid, stid);
if(*suppressed && stid == NULL)
{
stid = (scap_tid *) malloc(sizeof(scap_tid));
stid->tid = tid;
int32_t uth_status = SCAP_SUCCESS;
HASH_ADD_INT64(handle->m_suppressed_tids, tid, stid);
if(uth_status != SCAP_SUCCESS)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't add tid to suppressed hash table");
free(stid);
return SCAP_FAILURE;
}
*suppressed = true;
}
else if (!*suppressed && stid != NULL)
{
HASH_DEL(handle->m_suppressed_tids, stid);
free(stid);
*suppressed = false;
}
return SCAP_SUCCESS;
}
int32_t scap_check_suppressed(scap_t *handle, scap_evt *pevent, bool *suppressed)
{
uint16_t *lens;
char *valptr;
uint32_t j;
int32_t res = SCAP_SUCCESS;
const char *comm = NULL;
uint64_t *ptid = NULL;
scap_tid *stid;
*suppressed = false;
// For events that can create a new tid (fork, vfork, clone),
// we need to check the comm, which might also update the set
// of suppressed tids.
switch(pevent->type)
{
case PPME_SYSCALL_CLONE_20_X:
case PPME_SYSCALL_FORK_20_X:
case PPME_SYSCALL_VFORK_20_X:
case PPME_SYSCALL_EXECVE_19_X:
case PPME_SYSCALL_EXECVEAT_X:
case PPME_SYSCALL_CLONE3_X:
lens = (uint16_t *)((char *)pevent + sizeof(struct ppm_evt_hdr));
valptr = (char *)lens + pevent->nparams * sizeof(uint16_t);
if(pevent->nparams < 14)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find process comm in event argument list");
return SCAP_FAILURE;
}
// For all of these events, the comm is argument 14,
// so we need to walk the list of params that far to
// find the comm.
for(j = 0; j < 13; j++)
{
if(j == 5)
{
ptid = (uint64_t *) valptr;
}
valptr += lens[j];
}
if(ptid == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find ptid in event argument list");
return SCAP_FAILURE;
}
comm = valptr;
if((res = scap_update_suppressed(handle,
comm,
pevent->tid, *ptid,
suppressed)) != SCAP_SUCCESS)
{
// scap_update_suppressed already set handle->m_lasterr on error.
return res;
}
break;
default:
HASH_FIND_INT64(handle->m_suppressed_tids, &(pevent->tid), stid);
// When threads exit they are always removed and no longer suppressed.
if(pevent->type == PPME_PROCEXIT_1_E)
{
if(stid != NULL)
{
HASH_DEL(handle->m_suppressed_tids, stid);
free(stid);
*suppressed = true;
}
else
{
*suppressed = false;
}
}
else
{
*suppressed = (stid != NULL);
}
break;
}
return SCAP_SUCCESS;
}
struct ppm_proclist_info *scap_procfs_get_threadlist(scap_t *handle)
{
struct ppm_proclist_info *res = NULL;
#ifdef __linux__
DIR *dir_p = NULL;
DIR *taskdir_p = NULL;
FILE *fp = NULL;
struct dirent *dir_entry_p;
char procdirname[SCAP_MAX_PATH_SIZE];
handle->m_driver_procinfo->n_entries = 0;
snprintf(procdirname, sizeof(procdirname), "%s/proc", scap_get_host_root());
dir_p = opendir(procdirname);
if(dir_p == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error opening the %s directory", procdirname);
goto error;
}
while((dir_entry_p = readdir(dir_p)) != NULL)
{
char tasksdirname[SCAP_MAX_PATH_SIZE];
struct dirent *taskdir_entry_p;
DIR *taskdir_p;
if(strspn(dir_entry_p->d_name, "0123456789") != strlen(dir_entry_p->d_name))
{
continue;
}
snprintf(tasksdirname, sizeof(tasksdirname), "%s/%s/task", procdirname, dir_entry_p->d_name);
taskdir_p = opendir(tasksdirname);
if(taskdir_p == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error opening the %s directory", tasksdirname);
continue;
}
while((taskdir_entry_p = readdir(taskdir_p)) != NULL)
{
char filename[SCAP_MAX_PATH_SIZE];
unsigned long utime;
unsigned long stime;
int tid;
if(strspn(taskdir_entry_p->d_name, "0123456789") != strlen(taskdir_entry_p->d_name))
{
continue;
}
snprintf(filename, sizeof(filename), "%s/%s/stat", tasksdirname, taskdir_entry_p->d_name);
fp = fopen(filename, "r");
if(fp == NULL)
{
continue;
}
if(fscanf(fp, "%d %*[^)] %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu", &tid, &utime, &stime) != 3)
{
fclose(fp);
fp = NULL;
continue;
}
if(handle->m_driver_procinfo->n_entries == handle->m_driver_procinfo->max_entries)
{
if(!scap_alloc_proclist_info(handle, handle->m_driver_procinfo->n_entries + 256))
{
goto error;
}
}
handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].pid = tid;
handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].utime = utime;
handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].stime = stime;
++handle->m_driver_procinfo->n_entries;
fclose(fp);
fp = NULL;
}
closedir(taskdir_p);
taskdir_p = NULL;
}
res = handle->m_driver_procinfo;
error:
if(dir_p)
{
closedir(dir_p);
}
if(taskdir_p)
{
closedir(taskdir_p);
}
if(fp)
{
fclose(fp);
}
#endif /* __linux__ */
return res;
}
|