Created
June 28, 2017 14:11
-
-
Save mrunalp/b56180e67093d66de7a191932bb4b7fe to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"syscall" | |
"time" | |
) | |
func main() { | |
console, err := os.OpenFile("/dev/console", os.O_RDWR, 0) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Fprintln(console, "write before dup /dev/null to stdio") | |
makeNullStdio() | |
fmt.Fprintln(console, "write after dup /dev/null to stdio") | |
console.Close() | |
logf, _ := os.Create("/tmp/logfile") | |
for { | |
console, err = os.OpenFile("/dev/console", os.O_RDWR, 0) | |
if err != nil { | |
fmt.Fprint(logf, err) | |
os.Exit(1) | |
} | |
fmt.Fprintln(console, "open, write to console, and close") | |
console.Close() | |
time.Sleep(time.Second) | |
} | |
} | |
func makeNullStdio() { | |
nullFd, _ := os.OpenFile("/dev/null", os.O_RDWR, 0) | |
defer nullFd.Close() | |
fd := int(nullFd.Fd()) | |
syscall.Dup2(fd, 0) | |
syscall.Dup2(fd, 1) | |
syscall.Dup2(fd, 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment