Skip to content

Commit 065164b

Browse files
authored
Process group (#8)
* No need to delete namespace in case of kill We kill the child process, so the Wait will continue and clean everything properly * Create the child process in a new process group This solves the 'stdin not being printed' problem \o/
1 parent fde207b commit 065164b

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

main.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func main() {
263263
return
264264
}
265265

266-
log.Debug("Cleaning ...")
266+
log.Debug("Exiting properly ...")
267267
}
268268

269269
func execCmd(cmdString string) error {
@@ -287,14 +287,13 @@ func execCmd(cmdString string) error {
287287
return err
288288
}
289289

290-
// TODO: There seems to be a problem if a Signal is catched, stdin is not
291-
// printed anymore on the terminal. A `reset` is needed
292-
stdin := os.Stdin
293-
294290
// Pass stdin / stdout / stderr as proc attributes
295291
procAttr := os.ProcAttr{
296-
Files: []*os.File{stdin, os.Stdout, os.Stderr},
292+
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
297293
Dir: pwd,
294+
Sys: &syscall.SysProcAttr{
295+
Setpgid: true,
296+
},
298297
}
299298

300299
log.Debugf("Going to run `%s ( %s ) %s`", cmdElmnts[0], bin, strings.Join(cmdElmnts[1:], " "))
@@ -314,7 +313,6 @@ func execCmd(cmdString string) error {
314313
if err != nil {
315314
log.Warn("Failed to change the namespace: ", err)
316315
}
317-
stdin.Close()
318316

319317
// If there is a process, need to kill it
320318
if proc != nil {
@@ -328,20 +326,6 @@ func execCmd(cmdString string) error {
328326
} else {
329327
log.Debugf("No process to kill")
330328
}
331-
332-
// If there is a namespace, need to delete it
333-
if namespace != nil {
334-
log.Debugf("Deleting the namespace")
335-
err := deleteNS(namespace)
336-
if err != nil {
337-
log.Warnf("error while deleting namespace", err)
338-
} else {
339-
log.Debugf("Namespace deleted")
340-
}
341-
} else {
342-
log.Debugf("No namespace to delete")
343-
}
344-
os.Exit(1)
345329
}()
346330

347331
// Start the process
@@ -359,7 +343,6 @@ func execCmd(cmdString string) error {
359343
}
360344

361345
log.Debugf("Result : %s", state)
362-
stdin.Close()
363346

364347
return nil
365348
}
@@ -415,6 +398,8 @@ func getOriginalNS() (*netns.NsHandle, error) {
415398

416399
// deleteNS will delete the given namespace
417400
func deleteNS(ns *netns.NsHandle) error {
401+
log.Debugf("Deleting namespace")
402+
418403
// Close the nsHandler
419404
err := ns.Close()
420405
if err != nil {

0 commit comments

Comments
 (0)