@@ -179,6 +179,16 @@ func startInitialization() (retErr error) {
179179 defer consoleSocket .Close ()
180180 }
181181
182+ var pidfdSocket * os.File
183+ if envSockFd := os .Getenv ("_LIBCONTAINER_PIDFD_SOCK" ); envSockFd != "" {
184+ sockFd , err := strconv .Atoi (envSockFd )
185+ if err != nil {
186+ return fmt .Errorf ("unable to convert _LIBCONTAINER_PIDFD_SOCK: %w" , err )
187+ }
188+ pidfdSocket = os .NewFile (uintptr (sockFd ), "pidfd-socket" )
189+ defer pidfdSocket .Close ()
190+ }
191+
182192 // Get mount files (O_PATH).
183193 mountSrcFds , err := parseFdsFromEnv ("_LIBCONTAINER_MOUNT_FDS" )
184194 if err != nil {
@@ -222,10 +232,10 @@ func startInitialization() (retErr error) {
222232 }
223233
224234 // If init succeeds, it will not return, hence none of the defers will be called.
225- return containerInit (it , & config , syncPipe , consoleSocket , fifofd , logFD , dmzExe , mountFds {sourceFds : mountSrcFds , idmapFds : idmapFds })
235+ return containerInit (it , & config , syncPipe , consoleSocket , pidfdSocket , fifofd , logFD , dmzExe , mountFds {sourceFds : mountSrcFds , idmapFds : idmapFds })
226236}
227237
228- func containerInit (t initType , config * initConfig , pipe * syncSocket , consoleSocket * os.File , fifoFd , logFd int , dmzExe * os.File , mountFds mountFds ) error {
238+ func containerInit (t initType , config * initConfig , pipe * syncSocket , consoleSocket , pidfdSocket * os.File , fifoFd , logFd int , dmzExe * os.File , mountFds mountFds ) error {
229239 if err := populateProcessEnvironment (config .Env ); err != nil {
230240 return err
231241 }
@@ -240,6 +250,7 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
240250 i := & linuxSetnsInit {
241251 pipe : pipe ,
242252 consoleSocket : consoleSocket ,
253+ pidfdSocket : pidfdSocket ,
243254 config : config ,
244255 logFd : logFd ,
245256 dmzExe : dmzExe ,
@@ -249,6 +260,7 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
249260 i := & linuxStandardInit {
250261 pipe : pipe ,
251262 consoleSocket : consoleSocket ,
263+ pidfdSocket : pidfdSocket ,
252264 parentPid : unix .Getppid (),
253265 config : config ,
254266 fifoFd : fifoFd ,
@@ -694,3 +706,20 @@ func signalAllProcesses(m cgroups.Manager, s unix.Signal) error {
694706
695707 return nil
696708}
709+
710+ // setupPidfd opens a process file descriptor of init process, and sends the
711+ // file descriptor back to the socket.
712+ func setupPidfd (socket * os.File , initType string ) error {
713+ defer socket .Close ()
714+
715+ pidFd , err := unix .PidfdOpen (os .Getpid (), 0 )
716+ if err != nil {
717+ return fmt .Errorf ("failed to pidfd_open: %w" , err )
718+ }
719+
720+ if err := utils .SendRawFd (socket , initType , uintptr (pidFd )); err != nil {
721+ unix .Close (pidFd )
722+ return fmt .Errorf ("failed to send pidfd on socket: %w" , err )
723+ }
724+ return unix .Close (pidFd )
725+ }
0 commit comments