本文整理汇总了Golang中os.ForkExec函数的典型用法代码示例。如果您正苦于以下问题:Golang ForkExec函数的具体用法?Golang ForkExec怎么用?Golang ForkExec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ForkExec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compile
func compile(w *World) *bytes.Buffer {
ioutil.WriteFile(TEMPPATH+".go", []byte(w.source()), 0644)
err := new(bytes.Buffer)
re, e, _ := os.Pipe()
os.ForkExec(
bin+"/"+arch+"g",
[]string{bin + "/" + arch + "g", "-o", TEMPPATH + ".6", TEMPPATH + ".go"},
os.Environ(),
"",
[]*os.File{nil, e, nil})
e.Close()
io.Copy(err, re)
if err.Len() > 0 {
return err
}
re, e, _ = os.Pipe()
os.ForkExec(
bin+"/"+arch+"l",
[]string{bin + "/" + arch + "l", "-o", TEMPPATH + "", TEMPPATH + ".6"},
os.Environ(),
"",
[]*os.File{nil, e, nil})
e.Close()
io.Copy(err, re)
return err
}
示例2: main
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s <script file>\n", os.Args[0])
return
}
namehash_hash := md5.New()
io.WriteString(namehash_hash, os.Args[1])
namehash := hex.EncodeToString(namehash_hash.Sum())
tempfile := temp_dir() + "/" + namehash
readpipe, writepipe, _ := os.Pipe()
stdfiles := [](*os.File){readpipe, os.Stdout, os.Stderr}
syscall.Umask(0077)
srcfile, err := os.Open(os.Args[1], os.O_RDONLY, 0)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
srcinfo, _ := srcfile.Stat()
outfile, err := os.Open(tempfile+".ld", os.O_RDONLY, 0)
var outinfo (*os.FileInfo)
if err == nil {
outinfo, _ = outfile.Stat()
outfile.Close()
}
if err != nil || outinfo.Mtime_ns < srcinfo.Mtime_ns {
//fmt.Printf("Compiling...")
cpid, cerr := os.ForkExec(GOBIN+"8g", []string{GOBIN + "8g", "-o", tempfile + ".cd", "/dev/stdin"}, nil, "", stdfiles)
src := textproto.NewReader(bufio.NewReader(srcfile))
firstline := true
for {
s, err := src.ReadLine()
if err != nil {
break
}
if (len(s) > 0) && (s[0] == '#') && firstline {
continue
}
firstline = false
writepipe.WriteString(s + "\n")
}
writepipe.Close()
srcfile.Close()
stage_check(cpid, cerr, "compile")
lpid, err := os.ForkExec(GOBIN+"8l", []string{GOBIN + "8l", "-o", tempfile + ".ld", tempfile + ".cd"}, nil, "", stdfiles)
stage_check(lpid, err, "link")
os.Chmod(tempfile+".ld", 0700)
//fmt.Println("done.")
} else {
//fmt.Println("Running cached.")
}
err = os.Exec(tempfile+".ld", os.Args[1:], nil)
fmt.Println(err)
os.Exit(1)
}
示例3: git_from_net
func git_from_net(url string) string {
var args [3]string
args[0] = "git"
args[1] = "clone"
args[2] = url
var fds []*os.File = new([3]*os.File)
fds[0] = os.Stdin
fds[1] = os.Stdout
fds[2] = os.Stderr
_, str := path.Split(url)
name := strings.Split(str, ".", -1)[0]
var git_path string
switch os.Getenv("GOOS") {
case "darwin":
git_path = "/usr/local/git/bin/git"
break
case "linux":
git_path = "/opt/local/bin/git"
break
}
/* Replace this with git's full path, or use a shell, and then call git in the args */
pid, err := os.ForkExec(git_path, &args, os.Envs, os.Getenv("GOROOT")+"/src/pkg/", fds)
if err != nil {
log.Exit(err)
}
os.Wait(pid, 0)
return string(os.Getenv("GOROOT") + "/src/pkg/" + name)
}
示例4: startProc
func startProc(path, args interface{}) interface{} {
p, ok := path.(string)
if !ok {
TypeError("string", path)
}
argv := make([]string, ListLen(args))
for cur, i := args, 0; cur != EMPTY_LIST; cur, i = Cdr(cur), i+1 {
x := Car(cur)
s, ok := x.(string)
if !ok {
TypeError("string", x)
}
argv[i] = s
}
inr, inw, err := os.Pipe()
if err != nil {
SystemError(err)
}
outr, outw, err := os.Pipe()
if err != nil {
SystemError(err)
}
_, err = os.ForkExec(p, argv, os.Envs, "", []*os.File{inr, outw, os.Stderr})
if err != nil {
SystemError(err)
}
return Cons(NewOutput(inw), NewInput(outr))
}
示例5: Keyspace
func Keyspace(servers string) (*KeyspaceProxy, os.Error) {
serverList := strings.Split(servers, " ", -1)
argv := make([]string, len(serverList)+1)
argv[0] = scriptPath
for idx, server := range serverList {
argv[idx+1] = server
}
stdinRead, stdinWrite, err := os.Pipe()
if err != nil {
return nil, err
}
stdoutRead, stdoutWrite, err := os.Pipe()
if err != nil {
return nil, err
}
stderrRead, stderrWrite, err := os.Pipe()
if err != nil {
return nil, err
}
fd := []*os.File{stdinRead, stdoutWrite, stderrWrite}
pid, err := os.ForkExec(scriptPath, argv, os.Environ(), "", fd)
if err != nil {
return nil, err
}
return &KeyspaceProxy{
Servers: servers,
pid: pid,
stdin: stdinWrite,
stdout: stdoutRead,
stderr: stderrRead,
lock: &sync.Mutex{},
},
nil
}
示例6: DateServer
// exec a program, redirecting output
func DateServer(c *http.Conn, req *http.Request) {
c.SetHeader("content-type", "text/plain; charset=utf-8")
r, w, err := os.Pipe()
if err != nil {
fmt.Fprintf(c, "pipe: %s\n", err)
return
}
pid, err := os.ForkExec("/bin/date", []string{"date"}, os.Environ(), "", []*os.File{nil, w, w})
defer r.Close()
w.Close()
if err != nil {
fmt.Fprintf(c, "fork/exec: %s\n", err)
return
}
io.Copy(c, r)
wait, err := os.Wait(pid, 0)
if err != nil {
fmt.Fprintf(c, "wait: %s\n", err)
return
}
if !wait.Exited() || wait.ExitStatus() != 0 {
fmt.Fprintf(c, "date: %v\n", wait)
return
}
}
示例7: run
func run() (*bytes.Buffer, *bytes.Buffer) {
out := new(bytes.Buffer)
err := new(bytes.Buffer)
re, e, _ := os.Pipe()
ro, o, _ := os.Pipe()
os.ForkExec(
TEMPPATH,
[]string{TEMPPATH},
os.Environ(),
"",
[]*os.File{nil, o, e})
e.Close()
io.Copy(err, re)
if err.Len() > 0 {
return nil, err
}
o.Close()
io.Copy(out, ro)
return out, err
}
示例8: GetOutput
// Return the output from running the given command
func GetOutput(args []string) (output string, error os.Error) {
read_pipe, write_pipe, err := os.Pipe()
if err != nil {
goto Error
}
defer read_pipe.Close()
pid, err := os.ForkExec(args[0], args, os.Environ(), ".", []*os.File{nil, write_pipe, nil})
if err != nil {
write_pipe.Close()
goto Error
}
_, err = os.Wait(pid, 0)
write_pipe.Close()
if err != nil {
goto Error
}
buffer := &bytes.Buffer{}
_, err = io.Copy(buffer, read_pipe)
if err != nil {
goto Error
}
output = buffer.String()
return output, nil
Error:
return "", &CommandError{args[0], args}
}
示例9: SpawnMonitor
func SpawnMonitor(argv0 string, argv []string, envv []string, dir string, fd []*File) os.Error {
pid,err: = os.ForkExec(argv0, argv, envv, dir, fd) (pid int, err Error)
if err != nil {
return err
}
for ;os.Kill(pid, 0) == 0;{
_=time.Sleep(10000)
}
}
示例10: runSystemCommand
func runSystemCommand(argv []string, dir string) string {
lookedPath, _ := exec.LookPath(argv[0])
r, w, _ := os.Pipe()
pid, _ := os.ForkExec(lookedPath, argv, nil, dir, []*os.File{nil, w, w})
w.Close()
os.Wait(pid, 0)
var b bytes.Buffer
io.Copy(&b, r)
return b.String()
}
示例11: Run
// Run starts the named binary running with
// arguments argv and environment envv.
// It returns a pointer to a new Cmd representing
// the command or an error.
//
// The parameters stdin, stdout, and stderr
// specify how to handle standard input, output, and error.
// The choices are DevNull (connect to /dev/null),
// PassThrough (connect to the current process's standard stream),
// Pipe (connect to an operating system pipe), and
// MergeWithStdout (only for standard error; use the same
// file descriptor as was used for standard output).
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
// of the returned Cmd is the other end of the pipe.
// Otherwise the field in Cmd is nil.
func Run(name string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
p = new(Cmd)
var fd [3]*os.File
if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
goto Error
}
if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
goto Error
}
if stderr == MergeWithStdout {
fd[2] = fd[1]
} else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
goto Error
}
// Run command.
p.Pid, err = os.ForkExec(name, argv, envv, dir, fd[0:])
if err != nil {
goto Error
}
if fd[0] != os.Stdin {
fd[0].Close()
}
if fd[1] != os.Stdout {
fd[1].Close()
}
if fd[2] != os.Stderr && fd[2] != fd[1] {
fd[2].Close()
}
return p, nil
Error:
if fd[0] != os.Stdin && fd[0] != nil {
fd[0].Close()
}
if fd[1] != os.Stdout && fd[1] != nil {
fd[1].Close()
}
if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
fd[2].Close()
}
if p.Stdin != nil {
p.Stdin.Close()
}
if p.Stdout != nil {
p.Stdout.Close()
}
if p.Stderr != nil {
p.Stderr.Close()
}
return nil, err
}
示例12: tryRunServer
func tryRunServer() os.Error {
path, err := exec.LookPath("gocode")
if err != nil {
return err
}
args := []string{"gocode", "-s", "-sock", *sock, "-addr", *addr}
_, err = os.ForkExec(path, args, os.Environ(), "", []*os.File{nil, nil, nil})
if err != nil {
return err
}
return nil
}
示例13: UnmountFuse
func UnmountFuse(mountpoint string) (err os.Error) {
var pid int
fmt.Println("Unmounting", mountpoint, "...")
for i := 0; i < len(paths); i++ {
var args []string = []string{paths[i], "-u", mountpoint}
pid, err = os.ForkExec(paths[i], args, []string{}, "",
[]*os.File{os.Stdin, os.Stdout, os.Stderr})
if err == nil {
os.Wait(pid, 0)
return err
}
}
return err
}
示例14: run
// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
cmd, err := exec.LookPath(argv[0]);
if err != nil {
fatal("exec %s: %s", argv[0], err);
}
r0, w0, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
r1, w1, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
r2, w2, err := os.Pipe();
if err != nil {
fatal("%s", err);
}
pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2});
if err != nil {
fatal("%s", err);
}
r0.Close();
w1.Close();
w2.Close();
c := make(chan bool);
go func() {
w0.Write(stdin);
w0.Close();
c <- true;
}();
var xstdout []byte; // TODO(rsc): delete after 6g can take address of out parameter
go func() {
xstdout, _ = io.ReadAll(r1);
r1.Close();
c <- true;
}();
stderr, _ = io.ReadAll(r2);
r2.Close();
<-c;
<-c;
stdout = xstdout;
w, err := os.Wait(pid, 0);
if err != nil {
fatal("%s", err);
}
ok = w.Exited() && w.ExitStatus() == 0;
return;
}
示例15: run
// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
cmd, err := exec.LookPath(argv[0])
if err != nil {
fatal("exec %s: %s", argv[0], err)
}
r0, w0, err := os.Pipe()
if err != nil {
fatal("%s", err)
}
r1, w1, err := os.Pipe()
if err != nil {
fatal("%s", err)
}
r2, w2, err := os.Pipe()
if err != nil {
fatal("%s", err)
}
pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2})
if err != nil {
fatal("%s", err)
}
r0.Close()
w1.Close()
w2.Close()
c := make(chan bool)
go func() {
w0.Write(stdin)
w0.Close()
c <- true
}()
var xstdout []byte // TODO(rsc): delete after 6g can take address of out parameter
go func() {
xstdout, _ = ioutil.ReadAll(r1)
r1.Close()
c <- true
}()
stderr, _ = ioutil.ReadAll(r2)
r2.Close()
<-c
<-c
stdout = xstdout
w, err := os.Wait(pid, 0)
if err != nil {
fatal("%s", err)
}
ok = w.Exited() && w.ExitStatus() == 0
return
}