github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/cmds/exp/cpu/init.go (about)

     1  // Copyright 2018-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This is init code for the case that cpu finds itself as pid 1.
     6  // This is duplicative of the real init, but we're implementing it
     7  // as a duplicate so we can get some idea of:
     8  // what an init package should have
     9  // what an init interface should have
    10  // So we take a bit of duplication now to better understand these
    11  // things. We also assume for now this is a busybox environment.
    12  // It is unusual (I guess?) for cpu to be an init in anything else.
    13  // So far, the case for an init pkg is not as strong as I thought
    14  // it might be.
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"log"
    20  	"os/exec"
    21  	"syscall"
    22  
    23  	"github.com/u-root/u-root/pkg/uroot/util"
    24  )
    25  
    26  var (
    27  	test     = flag.Bool("test", false, "Test mode: don't try to set control tty")
    28  	osInitGo = func() {}
    29  )
    30  
    31  func cpuSetup() error {
    32  	log.Printf("Welcome to Plan 9(tm)!")
    33  	util.Rootfs()
    34  	log.Printf("Done Rootfs")
    35  	osInitGo()
    36  	// TODO: this needs to be added as prt of the Rootfs() stuff
    37  	if o, err := exec.Command("ip", "link", "set", "dev", "lo", "up").CombinedOutput(); err != nil {
    38  		log.Fatalf("ip link set dev lo: %v (%v)", string(o), err)
    39  	}
    40  	return nil
    41  }
    42  
    43  func cpuDone(c chan int) {
    44  	// We need to reap all children before exiting.
    45  	var procs int
    46  	log.Printf("init: Waiting for orphaned children")
    47  	for {
    48  		var s syscall.WaitStatus
    49  		var r syscall.Rusage
    50  		p, err := syscall.Wait4(-1, &s, 0, &r)
    51  		if p == -1 {
    52  			break
    53  		}
    54  		log.Printf("%v: exited with %v, status %v, rusage %v", p, err, s, r)
    55  		procs++
    56  	}
    57  	log.Printf("cpu: All commands exited")
    58  	log.Printf("cpu: Syncing filesystems")
    59  	syscall.Sync()
    60  	c <- procs
    61  }