Ejemplo n.º 1
0
func main() {
	fixListenPid()

	files := activation.Files(false)

	if len(files) == 0 {
		panic("No files")
	}

	if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" {
		panic("Should not unset envs")
	}

	files = activation.Files(true)

	if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" {
		panic("Can not unset envs")
	}

	// Write out the expected strings to the two pipes
	files[0].Write([]byte("Hello world"))
	files[1].Write([]byte("Goodbye world"))

	return
}
Ejemplo n.º 2
0
func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
	id := context.Args().First()
	if id == "" {
		return -1, errEmptyID
	}
	container, err := createContainer(context, id, spec)
	if err != nil {
		return -1, err
	}

	// ensure that the container is always removed if we were the process
	// that created it.
	detach := context.Bool("detach")
	if !detach {
		defer destroy(container)
	}

	// Support on-demand socket activation by passing file descriptors into the container init process.
	listenFDs := []*os.File{}
	if os.Getenv("LISTEN_FDS") != "" {
		listenFDs = activation.Files(false)
	}

	status, err := runProcess(container, &spec.Process, listenFDs, context.String("console"), context.String("pid-file"), detach)
	if err != nil {
		destroy(container)
		return -1, err
	}
	return status, nil
}
Ejemplo n.º 3
0
func startContainer(context *cli.Context, spec *specs.Spec, create bool) (int, error) {
	id := context.Args().First()
	if id == "" {
		return -1, errEmptyID
	}
	container, err := createContainer(context, id, spec)
	if err != nil {
		return -1, err
	}
	// Support on-demand socket activation by passing file descriptors into the container init process.
	listenFDs := []*os.File{}
	if os.Getenv("LISTEN_FDS") != "" {
		listenFDs = activation.Files(false)
	}
	r := &runner{
		enableSubreaper: !context.Bool("no-subreaper"),
		shouldDestroy:   true,
		container:       container,
		listenFDs:       listenFDs,
		console:         context.String("console"),
		detach:          context.Bool("detach"),
		pidFile:         context.String("pid-file"),
		create:          create,
	}
	return r.run(&spec.Process)
}
Ejemplo n.º 4
0
func main() {
	flag.Parse()
	if cpuProfile != "" {
		f, err := os.Create(cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	stopCh := make(chan bool)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for sig := range c {
			log.Printf("Signal %#v", sig)
			stopCh <- true
		}
	}()

	if !cliEnabled() {
		var err error
		var l net.Listener
		if !noEpmd {
			listen_fds := activation.Files(false)
			if listen_fds != nil {
				l, err = net.FileListener(listen_fds[0])
			} else {
				l, err = net.Listen("tcp", net.JoinHostPort("", listenPort))
			}
			if err != nil || noEpmd {
				// Cannot bind, eclus instance already running, connect to it
				eclusCli()
			} else {
				epm := make(chan regReq, 10)
				go epmReg(epm)
				go func() {
					for {
						conn, err := l.Accept()
						log.Printf("Accept new")
						if err != nil {
							log.Printf(err.Error())
						} else {
							go mLoop(conn, epm)
						}
					}
				}()
				if nodeEnabled() {
					go runNode()
				}
				<-stopCh
			}
		}
	} else {
		eclusCli()
	}
}
Ejemplo n.º 5
0
func main() {
	files := activation.Files(true)

	if len(files) != 1 {
		panic("Unexpected number of socket activation fds")
	}

	l, err := net.FileListener(files[0])
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/", HelloServer)
	http.Serve(l, nil)
}
Ejemplo n.º 6
0
func gocat(socket, server, cert, key string) {
	var ln net.Listener
	var err error
	if activatedFds := activation.Files(); len(activatedFds) == 0 {
		ln, err = net.Listen("tcp", server)
		if err != nil {
			panic(err)
		}
	} else if len(activatedFds) == 1 {
		fmt.Println("socket activation!") //TODO: remove
		ln, err = net.FileListener(activatedFds[0])
		if err != nil {
			panic(err)
		}
		//TODO: does activatedFDs[0] need to be closed?
	} else {
		panic("Too many activated sockets! Check .socket file configuration.")
	}

	if key != "" {
		ln = wrapTLS(ln, cert, key)
	} else {
		fmt.Println("danger: no certificate or key specified - starting without TLS!")
	}

	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Fprintln(os.Stderr, "accept failure:", err)
			continue
		}

		unix, err := net.Dial("unix", socket)
		if err != nil {
			fmt.Fprintln(os.Stderr, "socket connection error:", err)
			conn.Close()
			continue
		}

		go func() {
			//TODO: consider ways the connection might close
			go io.Copy(unix, conn)
			io.Copy(conn, unix)
			conn.Close()
		}()
	}
}
Ejemplo n.º 7
0
func listenFD() ([]net.Listener, error) {
	files := activation.Files(false)
	if files == nil || len(files) == 0 {
		return nil, errors.New("No sockets found")
	}

	listeners := make([]net.Listener, len(files))
	for i, f := range files {
		var err error
		listeners[i], err = net.FileListener(f)
		if err != nil {
			return nil, fmt.Errorf("Error setting up FileListener for fd %d: %s", f.Fd(), err.Error())
		}
	}

	return listeners, nil
}
Ejemplo n.º 8
0
// startContainer starts the container. Returns the exit status or -1 and an
// error. Signals sent to the current process will be forwarded to container.
func startContainer(spec *specs.Spec, id, pidFile string, detach, useSystemdCgroup bool) (int, error) {
	// create the libcontainer config
	config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{
		CgroupName:       id,
		UseSystemdCgroup: useSystemdCgroup,
		NoPivotRoot:      false,
		Spec:             spec,
	})
	if err != nil {
		return -1, err
	}

	if _, err := os.Stat(config.Rootfs); err != nil {
		if os.IsNotExist(err) {
			return -1, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs)
		}
		return -1, err
	}

	factory, err := loadFactory(useSystemdCgroup)
	if err != nil {
		return -1, err
	}

	container, err := factory.Create(id, config)
	if err != nil {
		return -1, err
	}

	// Support on-demand socket activation by passing file descriptors into the container init process.
	listenFDs := []*os.File{}
	if os.Getenv("LISTEN_FDS") != "" {
		listenFDs = activation.Files(false)
	}

	r := &runner{
		enableSubreaper: true,
		shouldDestroy:   true,
		container:       container,
		console:         console,
		detach:          detach,
		pidFile:         pidFile,
		listenFDs:       listenFDs,
	}
	return r.run(&spec.Process)
}
Ejemplo n.º 9
0
func setupSocketActivation() (net.Listener, error) {
	if !util.IsRunningSystemd() {
		return nil, nil
	}
	listenFds := activation.Files(false)
	if len(listenFds) > 1 {
		return nil, fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
	}
	var listener net.Listener
	if len(listenFds) == 1 {
		l, err := net.FileListener(listenFds[0])
		if err != nil {
			return nil, err
		}
		listener = l
	}
	return listener, nil
}
func main() {
	flag.Parse()

	file, err := os.Open(configFileName)
	if err != nil {
		log.Fatal(err)
	}
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&serverConfig)
	if err != nil {
		log.Fatal(err)
	}
	dm = devicemaster.New(deviceapi.DefaultRegistry)
	for _, value := range serverConfig.Devices {
		name, id, lamp := deviceFromConfig(value)
		if name != "" && id != "" && lamp != nil {
			dm.AddDevice(name, id, lamp)
		}
	}

	r := mux.NewRouter()
	r.HandleFunc("/devices", DeviceListHandler)
	r.HandleFunc("/devices/{id}", DeviceHandler).Methods("GET")
	r.HandleFunc("/devices/{id}/effect", EffectGetHandler).Methods("GET")
	r.HandleFunc("/devices/{id}/effect", EffectPutHandler).Methods("PUT")
	r.HandleFunc("/devices/{id}/active", ActivePutHandler).Methods("PUT")
	r.HandleFunc("/devices/{id}/available", EffectListHandler)

	files := activation.Files(false)
	var l net.Listener
	if len(files) != 1 {
		l, err = net.Listen("tcp", serverConfig.ListenAddress)
	} else {
		l, err = net.FileListener(files[0])
	}

	if err != nil {
		log.Fatal("Could not create Listener: ", err)
	}

	if err = http.Serve(l, r); err != nil {
		log.Fatal("Serve: ", err)
	}
}
Ejemplo n.º 11
0
func newUnixListener(pluginName string, group string) (net.Listener, string, error) {
	path, err := fullSocketAddress(pluginName)
	if err != nil {
		return nil, "", err
	}
	listenFds := activation.Files(false)
	if len(listenFds) > 1 {
		return nil, "", fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
	}
	if len(listenFds) == 1 {
		l, err := net.FileListener(listenFds[0])
		if err != nil {
			return nil, "", err
		}
		return l, path, nil
	}
	listener, err := sockets.NewUnixSocket(path, group)
	if err != nil {
		return nil, "", err
	}
	return listener, path, nil
}