@@ -46,7 +46,7 @@ type execProcess struct {
4646 mu sync.Mutex
4747 id string
4848 console console.Console
49- io runc. IO
49+ io * processIO
5050 status int
5151 exited time.Time
5252 pid * safePid
@@ -172,29 +172,30 @@ func (e *execProcess) start(ctx context.Context) (err error) {
172172 // access e.pid until it is updated.
173173 e .pid .Lock ()
174174 defer e .pid .Unlock ()
175+
175176 var (
176177 socket * runc.Socket
177- pidfile = filepath .Join (e .path , fmt .Sprintf ("%s.pid" , e .id ))
178+ pio * processIO
179+ pidFile = newExecPidFile (e .path , e .id )
178180 )
179181 if e .stdio .Terminal {
180182 if socket , err = runc .NewTempConsoleSocket (); err != nil {
181183 return errors .Wrap (err , "failed to create runc console socket" )
182184 }
183185 defer socket .Close ()
184- } else if e .stdio .IsNull () {
185- if e .io , err = runc .NewNullIO (); err != nil {
186- return errors .Wrap (err , "creating new NULL IO" )
187- }
188186 } else {
189- if e . io , err = runc . NewPipeIO ( e . parent .IoUID , e .parent .IoGID , withConditionalIO ( e .stdio ) ); err != nil {
190- return errors .Wrap (err , "failed to create runc io pipes " )
187+ if pio , err = createIO ( ctx , e . id , e . parent .IoUID , e .parent .IoGID , e .stdio ); err != nil {
188+ return errors .Wrap (err , "failed to create init process I/O " )
191189 }
190+ e .io = pio
192191 }
193192 opts := & runc.ExecOpts {
194- PidFile : pidfile ,
195- IO : e .io ,
193+ PidFile : pidFile .Path (),
196194 Detach : true ,
197195 }
196+ if pio != nil {
197+ opts .IO = pio .IO ()
198+ }
198199 if socket != nil {
199200 opts .ConsoleSocket = socket
200201 }
@@ -203,38 +204,43 @@ func (e *execProcess) start(ctx context.Context) (err error) {
203204 return e .parent .runtimeError (err , "OCI runtime exec failed" )
204205 }
205206 if e .stdio .Stdin != "" {
206- sc , err := fifo .OpenFifo (context .Background (), e .stdio .Stdin , syscall .O_WRONLY | syscall .O_NONBLOCK , 0 )
207- if err != nil {
208- return errors .Wrapf (err , "failed to open stdin fifo %s" , e .stdio .Stdin )
207+ if err := e .openStdin (e .stdio .Stdin ); err != nil {
208+ return err
209209 }
210- e .closers = append (e .closers , sc )
211- e .stdin = sc
212210 }
213- var copyWaitGroup sync.WaitGroup
214211 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
215212 defer cancel ()
216213 if socket != nil {
217214 console , err := socket .ReceiveMaster ()
218215 if err != nil {
219216 return errors .Wrap (err , "failed to retrieve console master" )
220217 }
221- if e .console , err = e .parent .Platform .CopyConsole (ctx , console , e .stdio .Stdin , e .stdio .Stdout , e .stdio .Stderr , & e .wg , & copyWaitGroup ); err != nil {
218+ if e .console , err = e .parent .Platform .CopyConsole (ctx , console , e .stdio .Stdin , e .stdio .Stdout , e .stdio .Stderr , & e .wg ); err != nil {
222219 return errors .Wrap (err , "failed to start console copy" )
223220 }
224- } else if ! e . stdio . IsNull () {
225- if err := copyPipes (ctx , e . io , e . stdio . Stdin , e . stdio . Stdout , e . stdio . Stderr , & e .wg , & copyWaitGroup ); err != nil {
221+ } else {
222+ if err := pio . Copy (ctx , & e .wg ); err != nil {
226223 return errors .Wrap (err , "failed to start io pipe copy" )
227224 }
228225 }
229- copyWaitGroup .Wait ()
230- pid , err := runc .ReadPidFile (opts .PidFile )
226+ pid , err := pidFile .Read ()
231227 if err != nil {
232228 return errors .Wrap (err , "failed to retrieve OCI runtime exec pid" )
233229 }
234230 e .pid .pid = pid
235231 return nil
236232}
237233
234+ func (e * execProcess ) openStdin (path string ) error {
235+ sc , err := fifo .OpenFifo (context .Background (), path , syscall .O_WRONLY | syscall .O_NONBLOCK , 0 )
236+ if err != nil {
237+ return errors .Wrapf (err , "failed to open stdin fifo %s" , path )
238+ }
239+ e .stdin = sc
240+ e .closers = append (e .closers , sc )
241+ return nil
242+ }
243+
238244func (e * execProcess ) Status (ctx context.Context ) (string , error ) {
239245 s , err := e .parent .Status (ctx )
240246 if err != nil {
0 commit comments