Example #1
0
// initd listens on a socket, spawns requested processes and reaps their
// exit statuses.
func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Fprintf(os.Stderr, "initd: panicked: %s\n", r)
			os.Exit(4)
		}
	}()

	dropCaps := flag.Bool("dropCapabilities", false, "drop capabilities before running processes")
	flag.Parse()

	container_daemon.Detach("/dev/null", "/dev/null")
	logger := lager.NewLogger("initd")

	syncWriter := os.NewFile(uintptr(4), "/dev/sync_writer")
	syscall.RawSyscall(syscall.SYS_FCNTL, uintptr(4), syscall.F_SETFD, syscall.FD_CLOEXEC)
	defer syncWriter.Close()

	sync := &containerizer.PipeSynchronizer{
		Writer: syncWriter,
	}

	reaper := system.StartReaper(logger)
	defer reaper.Stop()

	daemon := &container_daemon.ContainerDaemon{
		CmdPreparer: &container_daemon.ProcessSpecPreparer{
			Users:                  container_daemon.LibContainerUser{},
			Rlimits:                &container_daemon.RlimitsManager{},
			ProcStarterPath:        "/sbin/proc_starter",
			AlwaysDropCapabilities: *dropCaps,
		},
		Spawner: &container_daemon.Spawn{
			Runner: reaper,
			PTY:    system.KrPty,
		},
	}

	socketFile := os.NewFile(uintptr(5), "/dev/host.sock")
	listener, err := unix_socket.NewListenerFromFile(socketFile)
	if err != nil {
		fail(fmt.Sprintf("initd: failed to create listener: %s\n", err), 5)
	}

	socketFile.Close()

	if err := sync.SignalSuccess(); err != nil {
		fail(fmt.Sprintf("signal host: %s", err), 6)
	}

	if err := daemon.Run(listener); err != nil {
		fail(fmt.Sprintf("run daemon: %s", err), 7)
	}
}
						Expect(err).To(HaveOccurred())

						socketFile.Close()
					})
				})

				Context("when the file is a socket file", func() {
					It("creates the listener", func() {
						socketPath := fmt.Sprintf("/tmp/a_socket-%d.sock", GinkgoParallelNode())
						socketListener, err := net.Listen("unix", socketPath)
						Expect(err).ToNot(HaveOccurred())

						socketFile, err := socketListener.(*net.UnixListener).File()
						Expect(err).ToNot(HaveOccurred())

						listener, err := unix_socket.NewListenerFromFile(socketFile)
						Expect(err).ToNot(HaveOccurred())

						Expect(socketFile.Close()).To(Succeed())
						Expect(listener.Close()).To(Succeed())
					})
				})
			})
		})
	})

	Describe("Connect", func() {
		Context("when the server is not running", func() {
			It("fails to connect", func() {
				_, err := connector.Connect(nil)
				Expect(err).To(MatchError(ContainSubstring("unix_socket: connect to server socket")))