-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Freebsdcpud #289
Open
rminnich
wants to merge
4
commits into
u-root:main
Choose a base branch
from
rminnich:freebsdcpud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−27
Open
Freebsdcpud #289
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018-2022 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// This is init code for the case that cpu finds itself as pid 1. | ||
// This is duplicative of the real init, but we're implementing it | ||
// as a duplicate so we can get some idea of: | ||
// what an init package should have | ||
// what an init interface should have | ||
// So we take a bit of duplication now to better understand these | ||
// things. We also assume for now this is a busybox environment. | ||
// It is unusual (I guess?) for cpu to be an init in anything else. | ||
// So far, the case for an init pkg is not as strong as I thought | ||
// it might be. | ||
package main | ||
|
||
import ( | ||
"log" | ||
"runtime" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/u-root/u-root/pkg/libinit" | ||
) | ||
|
||
func cpuSetup() error { | ||
// The process reaper runs from here, and needs to run | ||
// as PID 1. | ||
runtime.LockOSThread() | ||
log.Printf(` | ||
|
||
#### ##### # # ## | ||
# # # # # # ## | ||
# # # # # ## | ||
# ##### # # ## | ||
# # # # # | ||
#### # #### ## | ||
`) | ||
// libinit.SetEnv() | ||
// libinit.CreateRootfs() | ||
libinit.NetInit() | ||
// Wait for orphans, forever. | ||
// Since there is no way of knowning when we are | ||
// done for good, our work here is never done. | ||
// A complication is that for long periods of time, there | ||
// may be no orphans.In that case, sleep for one second, | ||
// and try again. This background load is hardly enough | ||
// to matter. And, in general, it will happen by definition | ||
// when there is nothing to wait for, i.e. there is nothing | ||
// on the node to be upset about. | ||
// Were this ever to be a concern, an option is to kick off | ||
// a process that will never exit, such that wait4 will always | ||
// block and always return when any child process exits. | ||
go func() { | ||
var numReaped int | ||
for { | ||
var ( | ||
s syscall.WaitStatus | ||
r syscall.Rusage | ||
) | ||
p, err := syscall.Wait4(-1, &s, 0, &r) | ||
// Once per second, Wait 4 returns if there's nothing | ||
// else to do. | ||
if err != nil && err.Error() == "no child processes" { | ||
continue | ||
} | ||
verbose("orphan reaper: returns with %v", p) | ||
if p == -1 { | ||
verbose("Nothing to wait for, %d wait for so far", numReaped) | ||
time.Sleep(time.Second) | ||
} | ||
if err != nil { | ||
log.Printf("CPUD: a process exited with %v, status %v, rusage %v, err %v", p, s, r, err) | ||
} | ||
numReaped++ | ||
} | ||
}() | ||
|
||
runtime.UnlockOSThread() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2018-2019 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
// We use this ssh because it implements port redirection. | ||
// It can not, however, unpack password-protected keys yet. | ||
|
||
"github.com/u-root/cpu/session" | ||
) | ||
|
||
var ( | ||
// For the ssh server part | ||
hostKeyFile = flag.String("hk", "" /*"/etc/ssh/ssh_host_rsa_key"*/, "file for host key") | ||
pubKeyFile = flag.String("pk", "key.pub", "file for public key") | ||
port = flag.String("sp", "17010", "cpu default port") | ||
|
||
debug = flag.Bool("d", false, "enable debug prints") | ||
runAsInit = flag.Bool("init", false, "run as init (Debug only; normal test is if we are pid 1") | ||
// v allows debug printing. | ||
// Do not call it directly, call verbose instead. | ||
v = func(string, ...interface{}) {} | ||
remote = flag.Bool("remote", false, "indicates we are the remote side of the cpu session") | ||
network = flag.String("net", "tcp", "network to use") | ||
port9p = flag.String("port9p", "", "port9p # on remote machine for 9p mount") | ||
klog = flag.Bool("klog", false, "Log cpud messages in kernel log, not stdout") | ||
|
||
// Some networks are not well behaved, and for them we implement registration. | ||
registerAddr = flag.String("register", "", "address and port to register with after listen on cpu server port") | ||
registerTO = flag.Duration("registerTO", time.Duration(5*time.Second), "time.Duration for Dial address for registering") | ||
|
||
// if we start up too quickly, mDNS won't work correctly. | ||
// This sleep may be useful for other cases, so it is here, | ||
// not specifically for mDNS uses. | ||
sleepBeforeServing = flag.Duration("sleepBeforeServing", 0, "add a sleep before serving -- usually only needed if cpud runs as init with mDNS") | ||
|
||
pid1 bool | ||
) | ||
|
||
func verbose(f string, a ...interface{}) { | ||
if *remote { | ||
v("CPUD(remote):"+f+"\r\n", a...) | ||
} else { | ||
v("CPUD:"+f, a...) | ||
} | ||
} | ||
|
||
// There are three distinct cases to cover. | ||
// 1. running as init (indicated by pid == 1 OR -init=true switch | ||
// 2. running as server. pid != 1 AND -remote=true AND -init=false | ||
// 3. running as 'remote', i.e. the thing that starts a command for | ||
// a client. Indicated by remote=true. | ||
// | ||
// case (3) overrides case 2 and 1. | ||
// This has evolved over the years, and, likely, the init and remote | ||
// switches ought to be renamed to 'role'. But so it goes. | ||
// The rules on arguments are very strict now. In the remote case, | ||
// os.Args[1] MUST be remote; no other invocation is accepted, because | ||
// the args to remote and the args to server are different. | ||
// This invocation requirement is known to the server package. | ||
func main() { | ||
if len(os.Args) > 1 && (os.Args[1] == "-remote" || os.Args[1] == "-remote=true") { | ||
*remote = true | ||
} | ||
|
||
if *remote { | ||
// remote has far fewer args. Since they are specified by the client, | ||
// we want to limit the set of args it can set. | ||
flag.CommandLine = flag.NewFlagSet("cpud-remote", flag.ExitOnError) | ||
debug = flag.Bool("d", false, "enable debug prints") | ||
remote = flag.Bool("remote", false, "indicates we are the remote side of the cpu session") | ||
port9p = flag.String("port9p", "", "port9p # on remote machine for 9p mount") | ||
|
||
flag.Parse() | ||
if *debug { | ||
v = log.Printf | ||
session.SetVerbose(verbose) | ||
} | ||
// If we are here, no matter what they may set, *remote must be true. | ||
// sadly, cpud -d -remote=true -remote=false ... works. | ||
*remote = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no sure if I get this, but for me line 69 defines remote value, maybe we should not parse it at all? I mean remove |
||
} else { | ||
flag.Parse() | ||
// If we are here, no matter what they may set, *remote must be false. | ||
*remote = false | ||
if err := commonsetup(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
pid := os.Getpid() | ||
pid1 = pid == 1 | ||
*runAsInit = *runAsInit || pid1 | ||
verbose("Args %v pid %d *runasinit %v *remote %v env %v", os.Args, pid, *runAsInit, *remote, os.Environ()) | ||
args := flag.Args() | ||
if *remote { | ||
verbose("args %q, port9p %v", args, *port9p) | ||
|
||
// This can happen if the user gets clever and | ||
// invokes cpu with, e.g., nothing but switches. | ||
if len(args) == 0 { | ||
shell, ok := os.LookupEnv("SHELL") | ||
if !ok { | ||
log.Fatal("No arguments and $SHELL is not set") | ||
} | ||
args = []string{shell} | ||
} | ||
s := session.New(*port9p, args[0], args[1:]...) | ||
if err := s.Run(); err != nil { | ||
log.Fatalf("CPUD(remote): %v", err) | ||
} | ||
} else { | ||
log.Printf("CPUD:PID(%d):running as a server (a.k.a. starter of cpud's for sessions)", pid) | ||
if *runAsInit { | ||
log.Printf("CPUD:also running as init") | ||
if err := initsetup(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
time.Sleep(*sleepBeforeServing) | ||
if err := serve(os.Args[0]); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2018-2022 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package server | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// cpud can run in one of three modes | ||
// o init | ||
// o daemon started by init | ||
// o manager of one cpu session. | ||
func init() { | ||
// placeholder. It's not clear we ever want to do this. We used to create | ||
// a root file system here, but that should be up to the server. The files | ||
// might magically exist, b/c of initrd; or be automagically mounted via | ||
// some other mechanism. | ||
if os.Getpid() == 1 { | ||
verbose("PID 1") | ||
} | ||
} | ||
|
||
func command(n string, args ...string) *exec.Cmd { | ||
return exec.Command(n, args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018-2022 the u-root Authors. All rights reserved | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package session | ||
|
||
// Namespace does nothing; no 9p on freebsd yet. | ||
func (s *Session) Namespace() error { | ||
return nil | ||
} | ||
|
||
func osMounts() error { | ||
return nil | ||
} | ||
|
||
// runSetup performs kernel-specific operations for starting a Session. | ||
func runSetup() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how this function can return an error. But is this more or less duplicate, I think it's fine.