@@ -335,26 +335,37 @@ void hyper_free_container(struct hyper_container *c)
335335 container_free_sysctl (c );
336336 container_free_fsmap (c );
337337 container_free_cmd (c );
338+
339+ list_del_init (& c -> list );
340+ free (c );
338341}
339342
340- static int hyper_parse_container (struct hyper_pod * pod , struct hyper_container * c ,
343+ static int hyper_parse_container (struct hyper_pod * pod , struct hyper_container * * container ,
341344 char * json , jsmntok_t * toks )
342345{
343346 int i = 0 , j , next , next_container ;
347+ struct hyper_container * c = NULL ;
344348 jsmntok_t * t ;
345349
346350 if (toks [i ].type != JSMN_OBJECT ) {
347351 fprintf (stderr , "format incorrect\n" );
348352 return -1 ;
349353 }
350354
355+ c = calloc (1 , sizeof (* c ));
356+ if (c == NULL ) {
357+ fprintf (stdout , "alloc memory for container failed\n" );
358+ return -1 ;
359+ }
360+
351361 c -> exec .init = 1 ;
352362 c -> exec .code = -1 ;
353363 c -> exec .e .fd = -1 ;
354364 c -> exec .errev .fd = -1 ;
355365 c -> exec .ptyfd = -1 ;
356366 c -> exec .errfd = -1 ;
357367 c -> ns = -1 ;
368+ INIT_LIST_HEAD (& c -> list );
358369
359370 next_container = toks [i ].size ;
360371 fprintf (stdout , "next container %d\n" , next_container );
@@ -426,47 +437,44 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
426437 }
427438 }
428439
440+ * container = c ;
429441 return i ;
430-
431442fail :
432443 hyper_free_container (c );
444+ * container = NULL ;
433445 return -1 ;
434446}
435447
436448static int hyper_parse_containers (struct hyper_pod * pod , char * json , jsmntok_t * toks )
437449{
438- int i = 0 , j = 0 , next ;
450+ int i = 0 , j = 0 , next , c_num ;
451+ struct hyper_container * c , * n ;
439452
440453 if (toks [i ].type != JSMN_ARRAY ) {
441454 fprintf (stdout , "format incorrect\n" );
442455 return -1 ;
443456 }
444457
445- pod -> c = calloc (toks [i ].size , sizeof (* pod -> c ));
446- if (pod -> c == NULL ) {
447- fprintf (stdout , "alloc memory for container failed\n" );
448- goto fail ;
449- }
450-
451- pod -> remains = pod -> c_num = toks [i ].size ;
452- fprintf (stdout , "container count %d\n" , pod -> c_num );
458+ c_num = toks [i ].size ;
459+ fprintf (stdout , "container count %d\n" , c_num );
453460
454461 i ++ ;
455- for (j = 0 ; j < pod -> c_num ; j ++ ) {
456- next = hyper_parse_container (pod , & pod -> c [ j ] , json , toks + i );
462+ for (j = 0 ; j < c_num ; j ++ ) {
463+ next = hyper_parse_container (pod , & c , json , toks + i );
457464 if (next < 0 )
458465 goto fail ;
459466
467+ /* Pod created containers, Add to list immediately */
468+ list_add_tail (& c -> list , & pod -> containers );
460469 i += next ;
461470 }
462471
472+ pod -> remains = c_num ;
463473 return i ;
464474fail :
465- for (; j > 0 ; j -- )
466- hyper_free_container (& pod -> c [ j ] );
475+ list_for_each_entry_safe ( c , n , & pod -> containers , list )
476+ hyper_free_container (c );
467477
468- free (pod -> c );
469- pod -> c = NULL ;
470478 return -1 ;
471479}
472480
@@ -722,13 +730,7 @@ struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *j
722730 goto fail ;
723731 }
724732
725- c = calloc (1 , sizeof (* c ));
726- if (c == NULL ) {
727- fprintf (stdout , "alloc memory for container failed\n" );
728- goto fail ;
729- }
730-
731- if (hyper_parse_container (pod , c , json , toks ) < 0 )
733+ if (hyper_parse_container (pod , & c , json , toks ) < 0 )
732734 goto fail ;
733735
734736 c -> exec .init = 2 ; // dynamic container type
@@ -737,7 +739,6 @@ struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *j
737739
738740fail :
739741 free (toks );
740- free (c );
741742 return NULL ;
742743}
743744
0 commit comments