@@ -172,19 +172,19 @@ func (c *linuxContainer) Set(config configs.Config) error {
172172}
173173
174174// Create will initialize(create) a new container by
175- // creating the namespaces associated with it
175+ // creating the namespaces associated with it.
176176func (c * linuxContainer ) Create (process * Process ) error {
177- // Our hacking trigger for init routine
178- process .Args = []string {"createC" }
179-
180177 c .m .Lock ()
181178 defer c .m .Unlock ()
182- status , err := c .currentStatus ()
183- if err != nil {
184- return err
185- }
186- doInit := status == Created
187- parent , err := c .newParentProcess (process , doInit )
179+
180+ /*
181+ status, err := c.currentStatus()
182+ if err != nil {
183+ return err
184+ }
185+ */
186+
187+ parent , err := c .newParentProcess (process , initCreate )
188188 if err != nil {
189189 return newSystemError (err )
190190 }
@@ -195,19 +195,15 @@ func (c *linuxContainer) Create(process *Process) error {
195195 }
196196 return newSystemError (err )
197197 }
198- if doInit {
199- if err := c .updateState (parent ); err != nil {
200- return err
201- }
202- _ , err := parent .wait ()
203- if err != nil {
204- return err
205- }
206- } else {
207- panic ("should not be here" )
208- c .state .transition (& runningState {
209- c : c ,
210- })
198+
199+ if err := c .updateState (parent ); err != nil {
200+ return err
201+ }
202+
203+ // Wait for process to end
204+ _ , err = parent .wait ()
205+ if err != nil {
206+ return err
211207 }
212208
213209 return nil
@@ -220,14 +216,17 @@ func (c *linuxContainer) Start(process *Process) error {
220216 if err != nil {
221217 return err
222218 }
223- // doInit := status == Destroyed
219+
220+ // doInit will be true if we're creating the main proc of the container.
221+ // Otherwise we're just joining the namespaces of the existing proc.
224222 doInit := status == Created
225223
226- // fmt.Printf("state: %#v\n", c.state)
227- // fmt.Printf("status: %#v\n", status)
228- // fmt.Printf("status: %q\n", status)
229- // fmt.Printf("doInit: %v\n", doInit)
230- parent , err := c .newParentProcess (process , doInit )
224+ it := initStandard
225+ if ! doInit {
226+ it = initSetns
227+ }
228+
229+ parent , err := c .newParentProcess (process , it )
231230 if err != nil {
232231 return newSystemError (err )
233232 }
@@ -273,7 +272,7 @@ func (c *linuxContainer) Signal(s os.Signal) error {
273272 return nil
274273}
275274
276- func (c * linuxContainer ) newParentProcess (p * Process , doInit bool ) (parentProcess , error ) {
275+ func (c * linuxContainer ) newParentProcess (p * Process , it initType ) (parentProcess , error ) {
277276 parentPipe , childPipe , err := newPipe ()
278277 if err != nil {
279278 return nil , newSystemError (err )
@@ -282,10 +281,15 @@ func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProces
282281 if err != nil {
283282 return nil , newSystemError (err )
284283 }
285- if ! doInit {
284+ switch it {
285+ case initCreate :
286+ return c .newCreateProcess (p , cmd , parentPipe , childPipe )
287+ case initSetns :
286288 return c .newSetnsProcess (p , cmd , parentPipe , childPipe )
289+ case initStandard :
290+ return c .newInitProcess (p , cmd , parentPipe , childPipe )
287291 }
288- return c . newInitProcess ( p , cmd , parentPipe , childPipe )
292+ panic ( fmt . Sprintf ( "should not get here - it: %v" , it ) )
289293}
290294
291295func (c * linuxContainer ) commandTemplate (p * Process , childPipe * os.File ) (* exec.Cmd , error ) {
@@ -311,6 +315,35 @@ func (c *linuxContainer) commandTemplate(p *Process, childPipe *os.File) (*exec.
311315 return cmd , nil
312316}
313317
318+ // newCreateProcess is the same as newInitProcess except the INITTYPE value
319+ // will differ.
320+ func (c * linuxContainer ) newCreateProcess (p * Process , cmd * exec.Cmd , parentPipe , childPipe * os.File ) (* initProcess , error ) {
321+ t := "_LIBCONTAINER_INITTYPE=" + string (initCreate )
322+ cloneFlags := c .config .Namespaces .CloneFlags ()
323+ if cloneFlags & syscall .CLONE_NEWUSER != 0 {
324+ if err := c .addUidGidMappings (cmd .SysProcAttr ); err != nil {
325+ // user mappings are not supported
326+ return nil , err
327+ }
328+ enableSetgroups (cmd .SysProcAttr )
329+ // Default to root user when user namespaces are enabled.
330+ if cmd .SysProcAttr .Credential == nil {
331+ cmd .SysProcAttr .Credential = & syscall.Credential {}
332+ }
333+ }
334+ cmd .Env = append (cmd .Env , t )
335+ cmd .SysProcAttr .Cloneflags = cloneFlags
336+ return & initProcess {
337+ cmd : cmd ,
338+ childPipe : childPipe ,
339+ parentPipe : parentPipe ,
340+ manager : c .cgroupManager ,
341+ config : c .newInitConfig (p ),
342+ container : c ,
343+ process : p ,
344+ }, nil
345+ }
346+
314347func (c * linuxContainer ) newInitProcess (p * Process , cmd * exec.Cmd , parentPipe , childPipe * os.File ) (* initProcess , error ) {
315348 t := "_LIBCONTAINER_INITTYPE=" + string (initStandard )
316349 cloneFlags := c .config .Namespaces .CloneFlags ()
0 commit comments