@@ -947,8 +947,8 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
947947 char * token = NULL , * last_ptr = NULL , * last_second = NULL ;
948948 char * cgroup_ptr = NULL ;
949949 char buffer [4096 ];
950- int is_systemd = 0 ;
951950 char * prune_pos = NULL ;
951+ int id_size = 0 ;
952952
953953 cgroup_fd = fopen (pid_cgroup , "r" );
954954 if (unlikely (!cgroup_fd )) {
@@ -969,11 +969,9 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
969969 buffer [strlen (buffer ) - 1 ] = '\0' ;
970970
971971 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 )) {
975973 if (!strcmp (token , "memory" )) {
976- cgroup_ptr = strtok_r (NULL , ": " , & last_ptr );
974+ cgroup_ptr = strtok_r (NULL , "\n " , & last_ptr );
977975 break ;
978976 }
979977 }
@@ -987,16 +985,28 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
987985 LOGGER (4 , "can't find memory cgroup from %s" , pid_cgroup );
988986 goto DONE ;
989987 }
988+ LOGGER (4 , "get cgroup path %s" , cgroup_ptr );
989+
990990
991991 /**
992992 * 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
9931001 */
9941002 last_ptr = NULL ;
9951003 last_second = NULL ;
9961004 token = cgroup_ptr ;
9971005 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+ }
10001010 last_ptr = token ;
10011011 }
10021012 ++ token ;
@@ -1006,80 +1016,69 @@ int get_cgroup_data(const char *pid_cgroup, char *pod_uid, char *container_id,
10061016 goto DONE ;
10071017 }
10081018
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' ;
10111031
10121032 /**
10131033 * if cgroup is systemd, cgroup pattern should be like
10141034 * /kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod27882189_b4d9_11e9_b287_ec0d9ae89a20.slice/docker-4aa615892ab2a014d52178bdf3da1c4a45c8ddfb5171dd6e39dc910f96693e14.scope
10151035 * /kubepods.slice/kubepods-pod019c1fe8_0d92_4aa0_b61c_4df58bdde71c.slice/cri-containerd-9e073649debeec6d511391c9ec7627ee67ce3a3fb508b0fa0437a97f8e58ba98.scope
10161036 */
10171037 if ((prune_pos = strstr (container_id , ".scope" ))) {
1018- is_systemd = 1 ;
10191038 * prune_pos = '\0' ;
10201039 }
1040+ LOGGER (4 , "get container id %s" , container_id );
10211041
10221042 /**
10231043 * find pod uid
10241044 */
1025- * last_ptr = '\0' ;
10261045 if (!last_second ) {
10271046 goto DONE ;
10281047 }
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+ }
10291062
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 , ":" ))) {
10341068 * prune_pos = '\0' ;
10351069 }
10361070
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 = '-' ;
10581075 }
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 ;
10791077 }
1078+ LOGGER (4 , "get pod uid %s" , pod_uid );
10801079
10811080 ret = 0 ;
1082- DONE :
1081+ DONE :
10831082 if (cgroup_fd ) {
10841083 fclose (cgroup_fd );
10851084 }
0 commit comments