@@ -947,8 +947,8 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
947
947
char * token = NULL , * last_ptr = NULL , * last_second = NULL ;
948
948
char * cgroup_ptr = NULL ;
949
949
char buffer [4096 ];
950
- int is_systemd = 0 ;
951
950
char * prune_pos = NULL ;
951
+ int id_size = 0 ;
952
952
953
953
cgroup_fd = fopen (pid_cgroup , "r" );
954
954
if (unlikely (!cgroup_fd )) {
@@ -969,11 +969,9 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
969
969
buffer [strlen (buffer ) - 1 ] = '\0' ;
970
970
971
971
last_ptr = NULL ;
972
- token = buffer ;
973
- for (token = strtok_r (token , ":" , & last_ptr ); token ;
974
- token = NULL , token = strtok_r (token , ":" , & last_ptr )) {
972
+ for (token = strtok_r (buffer , ":" , & last_ptr ); token ; token = strtok_r (NULL , ":" , & last_ptr )) {
975
973
if (!strcmp (token , "memory" )) {
976
- cgroup_ptr = strtok_r (NULL , ": " , & last_ptr );
974
+ cgroup_ptr = strtok_r (NULL , "\n " , & last_ptr );
977
975
break ;
978
976
}
979
977
}
@@ -987,16 +985,28 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
987
985
LOGGER (4 , "can't find memory cgroup from %s" , pid_cgroup );
988
986
goto DONE ;
989
987
}
988
+ LOGGER (4 , "get cgroup path %s" , cgroup_ptr );
989
+
990
990
991
991
/**
992
992
* find container id
993
+ *
994
+ * if cgroup is cgroupfs, cgroup pattern should be like
995
+ *
996
+ * docker
997
+ * /kubepods/besteffort/pod27882189_b4d9_11e9_b287_ec0d9ae89a20/docker-4aa615892ab2a014d52178bdf3da1c4a45c8ddfb5171dd6e39dc910f96693e14
998
+ *
999
+ * containerd
1000
+ * /system.slice/containerd.service/kubepods-besteffort-pod019c1fe8_0d92_4aa0_b61c_4df58bdde71c.slice:cri-containerd:9e073649debeec6d511391c9ec7627ee67ce3a3fb508b0fa0437a97f8e58ba98
993
1001
*/
994
1002
last_ptr = NULL ;
995
1003
last_second = NULL ;
996
1004
token = cgroup_ptr ;
997
1005
while (* token ) {
998
- if (* token == '/' ) {
999
- last_second = last_ptr ;
1006
+ if (* token == '/' || * token == ':' ) {
1007
+ if (last_ptr && * last_ptr == '/' ) {
1008
+ last_second = last_ptr ;
1009
+ }
1000
1010
last_ptr = token ;
1001
1011
}
1002
1012
++ token ;
@@ -1006,80 +1016,69 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
1006
1016
goto DONE ;
1007
1017
}
1008
1018
1009
- strncpy (container_id , last_ptr + 1 , size );
1010
- container_id [size - 1 ] = '\0' ;
1019
+ token = last_ptr ;
1020
+ prune_pos = token ;
1021
+ while (* token ) {
1022
+ if (* token == '-' || * token == ':' ) {
1023
+ prune_pos = token ;
1024
+ }
1025
+ ++ token ;
1026
+ }
1027
+ // NOT minus 1 for the tailing `\0`
1028
+ id_size = token - prune_pos ;
1029
+ strncpy (container_id , prune_pos + 1 , id_size );
1030
+ container_id [id_size - 1 ] = '\0' ;
1011
1031
1012
1032
/**
1013
1033
* if cgroup is systemd, cgroup pattern should be like
1014
1034
* /kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod27882189_b4d9_11e9_b287_ec0d9ae89a20.slice/docker-4aa615892ab2a014d52178bdf3da1c4a45c8ddfb5171dd6e39dc910f96693e14.scope
1015
1035
* /kubepods.slice/kubepods-pod019c1fe8_0d92_4aa0_b61c_4df58bdde71c.slice/cri-containerd-9e073649debeec6d511391c9ec7627ee67ce3a3fb508b0fa0437a97f8e58ba98.scope
1016
1036
*/
1017
1037
if ((prune_pos = strstr (container_id , ".scope" ))) {
1018
- is_systemd = 1 ;
1019
1038
* prune_pos = '\0' ;
1020
1039
}
1040
+ LOGGER (4 , "get container id %s" , container_id );
1021
1041
1022
1042
/**
1023
1043
* find pod uid
1024
1044
*/
1025
- * last_ptr = '\0' ;
1026
1045
if (!last_second ) {
1027
1046
goto DONE ;
1028
1047
}
1048
+ id_size = last_ptr - last_second ;
1049
+ strncpy (pod_uid , last_second + 1 , id_size );
1050
+ pod_uid [id_size - 1 ] = '\0' ;
1051
+
1052
+ /*
1053
+ * remove Pod UID prefix
1054
+ * kubepods-besteffort-pod or pod
1055
+ * `po` chars will never appear in Pod UID
1056
+ * it is ok to search `pod` directly
1057
+ */
1058
+ while ((prune_pos = strstr (pod_uid , "pod" ))) {
1059
+ prune_pos += strlen ("pod" );
1060
+ memmove (pod_uid , prune_pos , strlen (prune_pos ));
1061
+ }
1029
1062
1030
- strncpy (pod_uid , last_second , size );
1031
- pod_uid [size - 1 ] = '\0' ;
1032
-
1033
- if (is_systemd && (prune_pos = strstr (pod_uid , ".slice" ))) {
1063
+ if ((prune_pos = strstr (pod_uid , ".slice" ))) {
1064
+ * prune_pos = '\0' ;
1065
+ }
1066
+ // NOT necessary currently, just make sure there is no other suffix for Pod UID
1067
+ if ((prune_pos = strstr (pod_uid , ":" ))) {
1034
1068
* prune_pos = '\0' ;
1035
1069
}
1036
1070
1037
- /**
1038
- * remove unnecessary chars from $container_id and $pod_uid
1039
- */
1040
- if (is_systemd ) {
1041
- /**
1042
- * For this kind of cgroup path, we need to find the last appearance of
1043
- * slash
1044
- * /kubepods.slice/kubepods-pod019c1fe8_0d92_4aa0_b61c_4df58bdde71c.slice/cri-containerd-9e073649debeec6d511391c9ec7627ee67ce3a3fb508b0fa0437a97f8e58ba98.scope
1045
- */
1046
- prune_pos = NULL ;
1047
- token = container_id ;
1048
- while (* token ) {
1049
- if (* token == '-' ) {
1050
- prune_pos = token ;
1051
- }
1052
- ++ token ;
1053
- }
1054
-
1055
- if (!prune_pos ) {
1056
- LOGGER (4 , "no - prefix" );
1057
- goto DONE ;
1071
+ prune_pos = pod_uid ;
1072
+ while (prune_pos && * prune_pos ) {
1073
+ if (* prune_pos == '_' ) {
1074
+ * prune_pos = '-' ;
1058
1075
}
1059
-
1060
- memmove (container_id , prune_pos + 1 , strlen (container_id ));
1061
-
1062
- prune_pos = strstr (pod_uid , "-pod" );
1063
- if (!prune_pos ) {
1064
- LOGGER (4 , "no pod string" );
1065
- goto DONE ;
1066
- }
1067
- prune_pos += strlen ("-pod" );
1068
- memmove (pod_uid , prune_pos , strlen (prune_pos ));
1069
- pod_uid [strlen (prune_pos )] = '\0' ;
1070
- prune_pos = pod_uid ;
1071
- while (* prune_pos ) {
1072
- if (* prune_pos == '_' ) {
1073
- * prune_pos = '-' ;
1074
- }
1075
- ++ prune_pos ;
1076
- }
1077
- } else {
1078
- memmove (pod_uid , pod_uid + strlen ("/pod" ), strlen (pod_uid ));
1076
+ ++ prune_pos ;
1079
1077
}
1078
+ LOGGER (4 , "get pod uid %s" , pod_uid );
1080
1079
1081
1080
ret = 0 ;
1082
- DONE :
1081
+ DONE :
1083
1082
if (cgroup_fd ) {
1084
1083
fclose (cgroup_fd );
1085
1084
}
0 commit comments