@@ -477,6 +477,66 @@ def exec_and_get_affinity_mask(cid, exec_cpu_affinity=None):
477477 shutil .rmtree (tempdir )
478478 return 0
479479
480+ def test_exec_cpu_affinity_config_file ():
481+ """Test that exec honors execCPUAffinity from container configuration when not specified in exec process"""
482+ if len (os .sched_getaffinity (0 )) < 4 :
483+ return 77
484+
485+ conf = base_config ()
486+ conf ['process' ]['args' ] = ['/init' , 'pause' ]
487+ # Set execCPUAffinity in the container's main process configuration
488+ conf ['process' ]['execCPUAffinity' ] = {"initial" : "0-1" , "final" : "0-2" }
489+ add_all_namespaces (conf )
490+ cid = None
491+ tempdir = tempfile .mkdtemp ()
492+
493+ def cpu_mask_from_proc_status (status ):
494+ for l in status .split ("\n " ):
495+ parts = l .split (":" )
496+ if parts [0 ] == "Cpus_allowed_list" :
497+ return parts [1 ].strip ()
498+ return ""
499+
500+ def exec_without_cpu_affinity_and_get_mask (cid ):
501+ """Execute a process without specifying execCPUAffinity and get its CPU mask"""
502+ process_file = os .path .join (tempdir , "process.json" )
503+ with open (process_file , "w" ) as f :
504+ process = {
505+ "user" : {
506+ "uid" : 0 ,
507+ "gid" : 0
508+ },
509+ "terminal" : False ,
510+ "cwd" : "/" ,
511+ "args" : [
512+ "/init" ,
513+ "cat" ,
514+ "/proc/self/status"
515+ ]
516+ # Note: No execCPUAffinity specified here - should fall back to container config
517+ }
518+ json .dump (process , f )
519+
520+ out = run_crun_command (["exec" , "--process" , process_file , cid ])
521+ return cpu_mask_from_proc_status (out )
522+
523+ try :
524+ _ , cid = run_and_get_output (conf , command = 'run' , detach = True )
525+
526+ # Execute without specifying execCPUAffinity - should use container's config final value
527+ mask = exec_without_cpu_affinity_and_get_mask (cid )
528+ if mask != "0-2" :
529+ sys .stderr .write ("# execCPUAffinity fallback test failed: cpu mask %s != 0-2\n " % mask )
530+ sys .stderr .write ("# expected to use container's execCPUAffinity.final value\n " )
531+ return - 1
532+
533+ return 0
534+ finally :
535+ if cid is not None :
536+ run_crun_command (["delete" , "-f" , cid ])
537+ shutil .rmtree (tempdir )
538+ return 0
539+
480540def test_exec_getpgrp ():
481541 conf = base_config ()
482542 add_all_namespaces (conf )
@@ -695,6 +755,7 @@ def test_exec_exit_code():
695755 "exec_populate_home_env_from_process_uid" : test_exec_populate_home_env_from_process_uid ,
696756 "exec-test-uid-tty" : test_uid_tty ,
697757 "exec-cpu-affinity" : test_exec_cpu_affinity ,
758+ "exec-cpu-affinity-config-file" : test_exec_cpu_affinity_config_file ,
698759 "exec-getpgrp" : test_exec_getpgrp ,
699760 "exec-help" : test_exec_help ,
700761 "exec-error-propagation" : test_exec_error_propagation ,
0 commit comments