-
Notifications
You must be signed in to change notification settings - Fork 727
/
container.cpp
1299 lines (1134 loc) · 35.5 KB
/
container.cpp
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
/*
Copyright (C) 2013-2018 Draios Inc dba Sysdig.
This file is part of sysdig.
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.
*/
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#endif
#include "sinsp.h"
#include "sinsp_int.h"
#include "container.h"
#include "utils.h"
#ifdef CYGWING_AGENT
#include "dragent_win_hal_public.h"
#endif
void sinsp_container_info::parse_json_mounts(const Json::Value &mnt_obj, vector<sinsp_container_info::container_mount_info> &mounts)
{
if(!mnt_obj.isNull() && mnt_obj.isArray())
{
for(uint32_t i=0; i<mnt_obj.size(); i++)
{
const Json::Value &mount = mnt_obj[i];
mounts.emplace_back(mount["Source"], mount["Destination"],
mount["Mode"], mount["RW"],
mount["Propagation"]);
}
}
}
const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_idx(uint32_t idx) const
{
if (idx >= m_mounts.size())
{
return NULL;
}
return &(m_mounts[idx]);
}
const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_source(std::string &source) const
{
// note: linear search
for (auto &mntinfo :m_mounts)
{
if(sinsp_utils::glob_match(source.c_str(), mntinfo.m_source.c_str()))
{
return &mntinfo;
}
}
return NULL;
}
const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_dest(std::string &dest) const
{
// note: linear search
for (auto &mntinfo :m_mounts)
{
if(sinsp_utils::glob_match(dest.c_str(), mntinfo.m_dest.c_str()))
{
return &mntinfo;
}
}
return NULL;
}
#if !defined(CYGWING_AGENT) && defined(HAS_CAPTURE)
CURLM *sinsp_container_engine_docker::m_curlm = NULL;
CURL *sinsp_container_engine_docker::m_curl = NULL;
#endif
bool sinsp_container_engine_docker::m_query_image_info = true;
sinsp_container_engine_docker::sinsp_container_engine_docker() :
m_unix_socket_path(string(scap_get_host_root()) + "/var/run/docker.sock"),
m_api_version("/v1.24")
{
#if !defined(CYGWING_AGENT) && defined(HAS_CAPTURE)
if(!m_curlm)
{
m_curl = curl_easy_init();
m_curlm = curl_multi_init();
if(m_curlm)
{
curl_multi_setopt(m_curlm, CURLMOPT_PIPELINING, CURLPIPE_HTTP1|CURLPIPE_MULTIPLEX);
}
if(m_curl)
{
curl_easy_setopt(m_curl, CURLOPT_UNIX_SOCKET_PATH, m_unix_socket_path.c_str());
curl_easy_setopt(m_curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curl_write_callback);
}
}
#endif
}
void sinsp_container_engine_docker::cleanup()
{
#if !defined(CYGWING_AGENT) && defined(HAS_CAPTURE)
curl_easy_cleanup(m_curl);
m_curl = NULL;
curl_multi_cleanup(m_curlm);
m_curlm = NULL;
#endif
}
void sinsp_container_engine_docker::set_query_image_info(bool query_image_info)
{
m_query_image_info = query_image_info;
}
#if !defined(CYGWING_AGENT) && defined(HAS_CAPTURE)
size_t sinsp_container_engine_docker::curl_write_callback(const char* ptr, size_t size, size_t nmemb, string* json)
{
const std::size_t total = size * nmemb;
json->append(ptr, total);
return total;
}
#endif
bool sinsp_container_engine_docker::parse_docker(sinsp_container_manager* manager, sinsp_container_info *container, sinsp_threadinfo* tinfo)
{
string json;
#ifndef CYGWING_AGENT
sinsp_docker_response resp = get_docker(manager, "http://localhost" + m_api_version + "/containers/" + container->m_id + "/json", json);
#else
sinsp_docker_response resp = get_docker(manager, "GET /v1.30/containers/" + container->m_id + "/json HTTP/1.1\r\nHost: docker\r\n\r\n", json);
#endif
switch(resp) {
case sinsp_docker_response::RESP_BAD_REQUEST:
m_api_version = "";
json = "";
#ifndef CYGWING_AGENT
resp = get_docker(manager, "http://localhost/containers/" + container->m_id + "/json", json);
#else
resp = get_docker(manager, "GET /containers/" + container->m_id + "/json HTTP/1.1\r\nHost: docker\r\n\r\n", json);
#endif
if (resp == sinsp_docker_response::RESP_OK)
{
break;
}
/* FALLTHRU */
case sinsp_docker_response::RESP_ERROR:
ASSERT(false);
return false;
case sinsp_docker_response::RESP_OK:
break;
}
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(json, root);
if(!parsingSuccessful)
{
ASSERT(false);
return false;
}
const Json::Value& config_obj = root["Config"];
container->m_image = config_obj["Image"].asString();
string imgstr = root["Image"].asString();
size_t cpos = imgstr.find(":");
if(cpos != string::npos)
{
container->m_imageid = imgstr.substr(cpos + 1);
}
// containers can be spawned using just the imageID as image name,
// with or without the hash prefix (e.g. sha256:)
bool no_name = !container->m_imageid.empty() &&
strncmp(container->m_image.c_str(), container->m_imageid.c_str(),
MIN(container->m_image.length(), container->m_imageid.length())) == 0;
no_name |= !imgstr.empty() &&
strncmp(container->m_image.c_str(), imgstr.c_str(),
MIN(container->m_image.length(), imgstr.length())) == 0;
if(!no_name || !m_query_image_info)
{
string hostname, port;
sinsp_utils::split_container_image(container->m_image,
hostname,
port,
container->m_imagerepo,
container->m_imagetag,
container->m_imagedigest,
false);
}
if(m_query_image_info && !container->m_imageid.empty() &&
(no_name || container->m_imagedigest.empty() || (!container->m_imagedigest.empty() && container->m_imagetag.empty())))
{
string img_json;
#ifndef CYGWING_AGENT
if(get_docker(manager, "http://localhost" + m_api_version + "/images/" + container->m_imageid + "/json?digests=1", img_json) == sinsp_docker_response::RESP_OK)
#else
if(get_docker(manager, "GET /v1.30/images/" + container->m_imageid + "/json?digests=1 HTTP/1.1\r\nHost: docker \r\n\r\n", img_json) == sinsp_docker_response::RESP_OK)
#endif
{
Json::Value img_root;
if(reader.parse(img_json, img_root))
{
for(const auto& rdig : img_root["RepoDigests"])
{
if(rdig.isString())
{
string repodigest = rdig.asString();
if(container->m_imagerepo.empty())
{
container->m_imagerepo = repodigest.substr(0, repodigest.find("@"));
}
if(repodigest.find(container->m_imagerepo) != string::npos)
{
container->m_imagedigest = repodigest.substr(repodigest.find("@")+1);
break;
}
}
}
for(const auto& rtag : img_root["RepoTags"])
{
if(rtag.isString())
{
string repotag = rtag.asString();
if(container->m_imagerepo.empty())
{
container->m_imagerepo = repotag.substr(0, repotag.rfind(":"));
}
if(repotag.find(container->m_imagerepo) != string::npos)
{
container->m_imagetag = repotag.substr(repotag.rfind(":")+1);
break;
}
}
}
}
}
}
if(container->m_imagetag.empty())
{
container->m_imagetag = "latest";
}
container->m_name = root["Name"].asString();
if(!container->m_name.empty() && container->m_name[0] == '/')
{
container->m_name = container->m_name.substr(1);
}
const Json::Value& net_obj = root["NetworkSettings"];
string ip = net_obj["IPAddress"].asString();
if(ip.empty())
{
const Json::Value& hconfig_obj = root["HostConfig"];
string net_mode = hconfig_obj["NetworkMode"].asString();
if(strncmp(net_mode.c_str(), "container:", strlen("container:")) == 0)
{
std::string container_id = net_mode.substr(net_mode.find(":") + 1);
uint32_t container_ip;
const sinsp_container_info *container_info = manager->get_container(container_id);
if(container_info)
{
container_ip = container_info->m_container_ip;
}
else
{
sinsp_container_info pcnt;
pcnt.m_id = container_id;
parse_docker(manager, &pcnt, tinfo);
container_ip = pcnt.m_container_ip;
}
container->m_container_ip = container_ip;
}
}
else
{
if(inet_pton(AF_INET, ip.c_str(), &container->m_container_ip) == -1)
{
ASSERT(false);
}
container->m_container_ip = ntohl(container->m_container_ip);
}
vector<string> ports = net_obj["Ports"].getMemberNames();
for(vector<string>::const_iterator it = ports.begin(); it != ports.end(); ++it)
{
size_t tcp_pos = it->find("/tcp");
if(tcp_pos == string::npos)
{
continue;
}
uint16_t container_port = atoi(it->c_str());
const Json::Value& v = net_obj["Ports"][*it];
if(v.isArray())
{
for(uint32_t j = 0; j < v.size(); ++j)
{
sinsp_container_info::container_port_mapping port_mapping;
ip = v[j]["HostIp"].asString();
string port = v[j]["HostPort"].asString();
if(inet_pton(AF_INET, ip.c_str(), &port_mapping.m_host_ip) == -1)
{
ASSERT(false);
continue;
}
port_mapping.m_host_ip = ntohl(port_mapping.m_host_ip);
port_mapping.m_container_port = container_port;
port_mapping.m_host_port = atoi(port.c_str());
container->m_port_mappings.push_back(port_mapping);
}
}
}
vector<string> labels = config_obj["Labels"].getMemberNames();
for(vector<string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
{
string val = config_obj["Labels"][*it].asString();
container->m_labels[*it] = val;
}
const Json::Value& env_vars = config_obj["Env"];
for(const auto& env_var : env_vars)
{
if(env_var.isString())
{
container->m_env.emplace_back(env_var.asString());
}
}
if (sinsp_container_engine_mesos::set_mesos_task_id(container, tinfo))
{
g_logger.log("Mesos Docker container: [" + root["Id"].asString() + "], Mesos task ID: [" + container->m_mesos_task_id + ']', sinsp_logger::SEV_DEBUG);
}
const auto& host_config_obj = root["HostConfig"];
container->m_memory_limit = host_config_obj["Memory"].asInt64();
container->m_swap_limit = host_config_obj["MemorySwap"].asInt64();
const auto cpu_shares = host_config_obj["CpuShares"].asInt64();
if(cpu_shares > 0)
{
container->m_cpu_shares = cpu_shares;
}
container->m_cpu_quota = host_config_obj["CpuQuota"].asInt64();
const auto cpu_period = host_config_obj["CpuPeriod"].asInt64();
if(cpu_period > 0)
{
container->m_cpu_period = cpu_period;
}
const Json::Value &privileged = host_config_obj["Privileged"];
if(!privileged.isNull() && privileged.isBool())
{
container->m_privileged = privileged.asBool();
}
sinsp_container_info::parse_json_mounts(root["Mounts"], container->m_mounts);
#ifdef HAS_ANALYZER
sinsp_utils::find_env(container->m_sysdig_agent_conf, container->get_env(), "SYSDIG_AGENT_CONF");
// container->m_sysdig_agent_conf = get_docker_env(env_vars, "SYSDIG_AGENT_CONF");
#endif
return true;
}
#ifdef CYGWING_AGENT
bool sinsp_container_engine_docker::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info);
{
wh_docker_container_info wcinfo = wh_docker_resolve_pid(m_inspector->get_wmi_handle(), tinfo->m_pid);
if(!wcinfo.m_res)
{
return false;
}
container_info.m_type = CT_DOCKER;
container_info.m_id = wcinfo.m_container_id;
container_info.m_name = wcinfo.m_container_name;
tinfo->m_container_id = container_info.m_id;
if (!manager->container_exists(container_info.m_id))
{
if (query_os_for_missing_info)
{
parse_docker(manager, &container_info, tinfo);
}
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
}
}
sinsp_docker_response sinsp_container_engine_docker::get_docker(const sinsp_container_manager* manager, const string& url, string &json)
{
const char* response;
bool qdres = wh_query_docker(manager->m_inspector->get_wmi_handle(),
(char*)url.c_str(),
&response);
if(qdres == false)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
json = response;
if(strncmp(json.c_str(), "HTTP/1.0 200 OK", sizeof("HTTP/1.0 200 OK") -1))
{
return sinsp_docker_response::RESP_BAD_REQUEST;
}
size_t pos = json.find("{");
if(pos == string::npos)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
json = json.substr(pos);
return sinsp_docker_response::RESP_OK;
}
#else
bool sinsp_container_engine_docker::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info)
{
sinsp_container_info container_info;
bool matches = false;
for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it)
{
string cgroup = it->second;
size_t pos;
//
// Non-systemd Docker
//
pos = cgroup.find_last_of("/");
if(pos != string::npos)
{
if(cgroup.length() - pos - 1 == 64 &&
cgroup.find_first_not_of("0123456789abcdefABCDEF", pos + 1) == string::npos)
{
container_info.m_type = CT_DOCKER;
container_info.m_id = cgroup.substr(pos + 1, 12);
matches = true;
break;
}
}
//
// systemd Docker
//
pos = cgroup.find("docker-");
if(pos != string::npos)
{
size_t pos2 = cgroup.find(".scope");
if(pos2 != string::npos &&
pos2 - pos - sizeof("docker-") + 1 == 64)
{
container_info.m_type = CT_DOCKER;
container_info.m_id = cgroup.substr(pos + sizeof("docker-") - 1, 12);
matches = true;
break;
}
}
}
if (!matches)
return false;
tinfo->m_container_id = container_info.m_id;
if (!manager->container_exists(container_info.m_id))
{
#ifndef _WIN32
if (query_os_for_missing_info)
{
parse_docker(manager, &container_info, tinfo);
}
#endif
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
}
return true;
}
sinsp_docker_response sinsp_container_engine_docker::get_docker(const sinsp_container_manager* manager, const string& url, string &json)
{
#ifdef HAS_CAPTURE
if(curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str()) != CURLE_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
if(curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &json) != CURLE_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
if(curl_multi_add_handle(m_curlm, m_curl) != CURLM_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
int still_running;
do
{
CURLMcode res = curl_multi_perform(m_curlm, &still_running);
if(res != CURLM_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
}
while(still_running);
if(curl_multi_remove_handle(m_curlm, m_curl) != CURLM_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
long http_code = 0;
if(curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &http_code) != CURLE_OK)
{
ASSERT(false);
return sinsp_docker_response::RESP_ERROR;
}
if(http_code != 200)
{
return sinsp_docker_response::RESP_BAD_REQUEST;
}
return sinsp_docker_response::RESP_OK;
#else
return sinsp_docker_response::RESP_ERROR;
#endif
}
bool sinsp_container_engine_lxc::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info)
{
sinsp_container_info container_info;
bool matches = false;
for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it)
{
string cgroup = it->second;
size_t pos;
//
// Non-systemd LXC
//
pos = cgroup.find("/lxc/");
if(pos != string::npos)
{
auto id_start = pos + sizeof("/lxc/") - 1;
auto id_end = cgroup.find('/', id_start);
container_info.m_type = CT_LXC;
container_info.m_id = cgroup.substr(id_start, id_end - id_start);
matches = true;
break;
}
}
if (!matches)
return false;
tinfo->m_container_id = container_info.m_id;
if (!manager->container_exists(container_info.m_id))
{
container_info.m_name = container_info.m_id;
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
}
return true;
}
bool sinsp_container_engine_libvirt_lxc::match(sinsp_threadinfo* tinfo, sinsp_container_info* container_info)
{
for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it)
{
string cgroup = it->second;
size_t pos;
//
// Non-systemd libvirt-lxc
//
pos = cgroup.find(".libvirt-lxc");
if(pos != string::npos &&
pos == cgroup.length() - sizeof(".libvirt-lxc") + 1)
{
size_t pos2 = cgroup.find_last_of("/");
if(pos2 != string::npos)
{
container_info->m_type = CT_LIBVIRT_LXC;
container_info->m_id = cgroup.substr(pos2 + 1, pos - pos2 - 1);
return true;
}
}
//
// systemd libvirt-lxc
//
pos = cgroup.find("-lxc\\x2");
if(pos != string::npos)
{
size_t pos2 = cgroup.find(".scope");
if(pos2 != string::npos &&
pos2 == cgroup.length() - sizeof(".scope") + 1)
{
container_info->m_type = CT_LIBVIRT_LXC;
container_info->m_id = cgroup.substr(pos + sizeof("-lxc\\x2"), pos2 - pos - sizeof("-lxc\\x2"));
return true;
}
}
//
// Legacy libvirt-lxc
//
pos = cgroup.find("/libvirt/lxc/");
if(pos != string::npos)
{
container_info->m_type = CT_LIBVIRT_LXC;
container_info->m_id = cgroup.substr(pos + sizeof("/libvirt/lxc/") - 1);
return true;
}
}
return false;
}
bool sinsp_container_engine_libvirt_lxc::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info)
{
sinsp_container_info container_info;
if (!match(tinfo, &container_info))
return false;
tinfo->m_container_id = container_info.m_id;
if (!manager->container_exists(container_info.m_id))
{
container_info.m_name = container_info.m_id;
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
}
return true;
}
bool sinsp_container_engine_mesos::match(sinsp_threadinfo* tinfo, sinsp_container_info* container_info)
{
for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it)
{
string cgroup = it->second;
size_t pos;
pos = cgroup.find("/mesos/");
if(pos != string::npos)
{
// It should match `/mesos/a9f41620-b165-4d24-abe0-af0af92e7b20`
auto id = cgroup.substr(pos + sizeof("/mesos/") - 1);
if(id.size() == 36 && id.find_first_not_of("0123456789abcdefABCDEF-") == string::npos)
{
container_info->m_type = CT_MESOS;
container_info->m_id = move(id);
// Consider a mesos container valid only if we find the mesos_task_id
// this will exclude from the container itself the mesos-executor
// but makes sure that we have task_id parsed properly. Otherwise what happens
// is that we'll create a mesos container struct without a mesos_task_id
// and for all other processes we'll use it
return set_mesos_task_id(container_info, tinfo);
}
}
}
return false;
}
bool sinsp_container_engine_mesos::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info)
{
sinsp_container_info container_info;
if (!match(tinfo, &container_info))
return false;
tinfo->m_container_id = container_info.m_id;
if (!manager->container_exists(container_info.m_id))
{
container_info.m_name = container_info.m_id;
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
}
return true;
}
string sinsp_container_engine_mesos::get_env_mesos_task_id(sinsp_threadinfo* tinfo)
{
string mtid;
sinsp_threadinfo::visitor_func_t visitor = [&mtid] (sinsp_threadinfo *ptinfo)
{
// Mesos task ID detection is not a straightforward task;
// this list may have to be extended.
mtid = ptinfo->get_env("MESOS_TASK_ID"); // Marathon
if(!mtid.empty()) { return false; }
mtid = ptinfo->get_env("mesos_task_id"); // Chronos
if(!mtid.empty()) { return false; }
mtid = ptinfo->get_env("MESOS_EXECUTOR_ID"); // others
if(!mtid.empty()) { return false; }
return true;
};
// Try the current thread first. visitor returns true if mtid
// was not filled in. In this case we should traverse the
// parents.
if(tinfo && visitor(tinfo))
{
tinfo->traverse_parent_state(visitor);
}
return mtid;
}
bool sinsp_container_engine_mesos::set_mesos_task_id(sinsp_container_info* container, sinsp_threadinfo* tinfo)
{
ASSERT(container);
ASSERT(tinfo);
// there are applications that do not share their environment in /proc/[PID]/environ
// since we need MESOS_TASK_ID environment variable to discover Mesos containers,
// there is a workaround for such cases:
// - for docker containers, we discover it directly from container, through Remote API
// (see sinsp_container_manager::parse_docker() for details)
// - for mesos native containers, parent process has the MESOS_TASK_ID (or equivalent, see
// get_env_mesos_task_id(sinsp_threadinfo*) implementation) environment variable, so we
// peek into the parent process environment to discover it
if(container && tinfo)
{
string& mtid = container->m_mesos_task_id;
if(mtid.empty())
{
mtid = get_env_mesos_task_id(tinfo);
// Ensure that the mesos task id vaguely looks
// like a real id. We assume it must be at
// least 3 characters and contain a dot or underscore
if(!mtid.empty() && mtid.length()>=3 &&
(mtid.find_first_of("._") != std::string::npos))
{
g_logger.log("Mesos native container: [" + container->m_id + "], Mesos task ID: " + mtid, sinsp_logger::SEV_DEBUG);
return true;
}
else
{
g_logger.log("Mesos container [" + container->m_id + "],"
"thread [" + std::to_string(tinfo->m_tid) +
"], has likely malformed mesos task id [" + mtid + "], ignoring", sinsp_logger::SEV_DEBUG);
}
}
}
return false;
}
bool sinsp_container_engine_rkt::match(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, sinsp_container_info* container_info, string& rkt_podid, string& rkt_appname, bool query_os_for_missing_info)
{
for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it)
{
string cgroup = it->second;
static const string COREOS_PODID_VAR = "container_uuid=";
static const string SYSTEMD_UUID_ARG = "--uuid=";
static const string SERVICE_SUFFIX = ".service";
if(cgroup.rfind(SERVICE_SUFFIX) == cgroup.size() - SERVICE_SUFFIX.size())
{
// check if there is a parent with pod uuid var
sinsp_threadinfo::visitor_func_t visitor = [&](sinsp_threadinfo* ptinfo)
{
for(const auto& env_var : ptinfo->get_env())
{
auto container_uuid_pos = env_var.find(COREOS_PODID_VAR);
if(container_uuid_pos == 0)
{
rkt_podid = env_var.substr(COREOS_PODID_VAR.size());
return false;
}
}
for(const auto& arg : ptinfo->m_args)
{
if(arg.find(SYSTEMD_UUID_ARG) != string::npos)
{
rkt_podid = arg.substr(SYSTEMD_UUID_ARG.size());
return false;
}
}
return true;
};
tinfo->traverse_parent_state(visitor);
if(!rkt_podid.empty())
{
auto last_slash = cgroup.find_last_of("/");
rkt_appname = cgroup.substr(last_slash + 1, cgroup.size() - last_slash - SERVICE_SUFFIX.size() - 1);
char image_manifest_path[SCAP_MAX_PATH_SIZE];
snprintf(image_manifest_path, sizeof(image_manifest_path), "%s/var/lib/rkt/pods/run/%s/appsinfo/%s/manifest", scap_get_host_root(), rkt_podid.c_str(), rkt_appname.c_str());
// First lookup if the container exists in our table, otherwise only if we are live check if it has
// an entry in /var/lib/rkt. In capture mode only the former will be used.
// In live mode former will be used only if we already hit that container
bool is_rkt_pod_id_valid = manager->container_exists(rkt_podid + ":" + rkt_appname); // if it's already on our table
#ifdef HAS_CAPTURE
if(!is_rkt_pod_id_valid && query_os_for_missing_info)
{
is_rkt_pod_id_valid = (access(image_manifest_path, F_OK) == 0);
}
#endif
if(is_rkt_pod_id_valid)
{
container_info->m_type = CT_RKT;
container_info->m_id = rkt_podid + ":" + rkt_appname;
container_info->m_name = rkt_appname;
return true;
}
}
}
}
// Try parsing from process root,
// Strings used to detect rkt stage1-cores pods
// TODO: detecting stage1-coreos rkt pods in this way is deprecated
// we can remove it in the future
static const string COREOS_PREFIX = "/opt/stage2/";
static const string COREOS_APP_SUFFIX = "/rootfs";
static const string COREOS_PODID_VAR = "container_uuid=";
auto prefix = tinfo->m_root.find(COREOS_PREFIX);
if(prefix == 0)
{
auto suffix = tinfo->m_root.find(COREOS_APP_SUFFIX, prefix);
if(suffix != string::npos)
{
bool valid_id = false;
rkt_appname = tinfo->m_root.substr(prefix + COREOS_PREFIX.size(), suffix - prefix - COREOS_PREFIX.size());
// It is a rkt pod with stage1-coreos
sinsp_threadinfo::visitor_func_t visitor = [&] (sinsp_threadinfo *ptinfo)
{
for(const auto& env_var : ptinfo->get_env())
{
auto container_uuid_pos = env_var.find(COREOS_PODID_VAR);
if(container_uuid_pos == 0)
{
rkt_podid = env_var.substr(COREOS_PODID_VAR.size());
container_info->m_type = CT_RKT;
container_info->m_id = rkt_podid + ":" + rkt_appname;
container_info->m_name = rkt_appname;
valid_id = true;
return false;
}
}
return true;
};
// Try the current thread first. visitor returns true if no coreos pid
// info was found. In this case we traverse the parents.
if (visitor(tinfo))
{
tinfo->traverse_parent_state(visitor);
}
return valid_id;
}
}
else
{
// String used to detect stage1-fly pods
static const string FLY_PREFIX = "/var/lib/rkt/pods/run/";
static const string FLY_PODID_SUFFIX = "/stage1/rootfs/opt/stage2/";
static const string FLY_APP_SUFFIX = "/rootfs";
auto prefix = tinfo->m_root.find(FLY_PREFIX);
if(prefix == 0)
{
auto podid_suffix = tinfo->m_root.find(FLY_PODID_SUFFIX, prefix+FLY_PREFIX.size());
if(podid_suffix != string::npos)
{
rkt_podid = tinfo->m_root.substr(prefix + FLY_PREFIX.size(), podid_suffix - prefix - FLY_PREFIX.size());
auto appname_suffix = tinfo->m_root.find(FLY_APP_SUFFIX, podid_suffix+FLY_PODID_SUFFIX.size());
if(appname_suffix != string::npos)
{
rkt_appname = tinfo->m_root.substr(podid_suffix + FLY_PODID_SUFFIX.size(),
appname_suffix-podid_suffix-FLY_PODID_SUFFIX.size());
container_info->m_type = CT_RKT;
container_info->m_id = rkt_podid + ":" + rkt_appname;
container_info->m_name = rkt_appname;
return true;
}
}
}
}
return false;
}
bool sinsp_container_engine_rkt::resolve(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info)
{
sinsp_container_info container_info;
string rkt_podid, rkt_appname;
if (!match(manager, tinfo, &container_info, rkt_podid, rkt_appname, query_os_for_missing_info))
{
return false;
}
tinfo->m_container_id = container_info.m_id;
if (!query_os_for_missing_info || manager->container_exists(container_info.m_id))
{
return true;
}
#ifndef _WIN32
bool have_rkt = parse_rkt(&container_info, rkt_podid, rkt_appname);
#else
bool have_rkt = true;
#endif
if (have_rkt)
{
manager->add_container(container_info, tinfo);
manager->notify_new_container(container_info);
return true;
}
else
{
return false;
}
}
bool sinsp_container_engine_rkt::parse_rkt(sinsp_container_info *container, const string &podid, const string &appname)
{
bool ret = false;
Json::Reader reader;
Json::Value jroot;
char image_manifest_path[SCAP_MAX_PATH_SIZE];
snprintf(image_manifest_path, sizeof(image_manifest_path), "%s/var/lib/rkt/pods/run/%s/appsinfo/%s/manifest", scap_get_host_root(), podid.c_str(), appname.c_str());
ifstream image_manifest(image_manifest_path);
if(reader.parse(image_manifest, jroot))
{
container->m_image = jroot["name"].asString();
for(const auto& label_entry : jroot["labels"])
{
container->m_labels.emplace(label_entry["name"].asString(), label_entry["value"].asString());
}
auto version_label_it = container->m_labels.find("version");
if(version_label_it != container->m_labels.end())
{
container->m_image += ":" + version_label_it->second;
}
ret = true;
}
char net_info_path[SCAP_MAX_PATH_SIZE];
snprintf(net_info_path, sizeof(net_info_path), "%s/var/lib/rkt/pods/run/%s/net-info.json", scap_get_host_root(), podid.c_str());
ifstream net_info(net_info_path);
if(reader.parse(net_info, jroot) && jroot.size() > 0)
{
const auto& first_net = jroot[0];
if(inet_pton(AF_INET, first_net["ip"].asCString(), &container->m_container_ip) == -1)
{
ASSERT(false);
}
container->m_container_ip = ntohl(container->m_container_ip);
}