Skip to content

Commit

Permalink
Added poweroff/reboot command in source code of halt.
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Dec 19, 2021
1 parent 98782e1 commit e5b790e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ All notable changes to this project will be documented in this file. The format

## [0.32.1] - 2021-12-19
### Added
- tr command.
- gzip command.
- tr command.
- poweroff command.
- reboot command.
### Changed
- halt command.
- Fix bug(GitHub Issue #33)
Expand Down
2 changes: 2 additions & 0 deletions docs/introduction/en/CommandAppletList.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
| mv | Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY|
| nl| Write each FILE to standard output with line numbers added|
| path | Manipulate filename path|
| poweroff| Power off the system|
| printenv| Print environment variable|
| pwd | Print Working Directory|
| reboot| Reboot the system|
| remove-shell|Remove shell name from /etc/shells|
| reset| Reset terminal|
| rm | Remove file(s) or directory(s)|
Expand Down
2 changes: 2 additions & 0 deletions internal/applets/applet.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ func init() {
"mv": {mv.Run, "Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY"},
"nl": {nl.Run, "Write each FILE to standard output with line numbers added"},
"path": {path.Run, "Manipulate filename path"},
"poweroff": {halt.Run, "Power off the system"},
"printenv": {printenv.Run, "Print environment variable"},
"pwd": {pwd.Run, "Print Working Directory"},
"remove-shell": {removeShell.Run, "Remove shell name from /etc/shells"},
"reboot": {halt.Run, "Reboot the system"},
"reset": {reset.Run, "Reset terminal"},
"rm": {rm.Run, "Remove file(s) or directory(s)"},
"rmdir": {rmdir.Run, "Remove directory"},
Expand Down
103 changes: 83 additions & 20 deletions internal/applets/pmutils/halt/halt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package halt

import (
"errors"
"fmt"
"os"
"syscall"
Expand All @@ -26,9 +27,9 @@ import (
"github.com/jessevdk/go-flags"
)

const cmdName string = "halt"
var cmdName string = "halt"

const version = "1.0.0"
const version = "1.0.1"

var osExit = os.Exit

Expand All @@ -38,29 +39,70 @@ const (
ExitFailuer
)

type options struct {
type haltOpts struct {
Version bool `short:"v" long:"version" description:"Show halt command version"`
}

type poweroffOpts struct {
Version bool `short:"v" long:"version" description:"Show poweroff command version"`
}

type rebootOpts struct {
Version bool `short:"v" long:"version" description:"Show reboot command version"`
}

type allOptions struct {
halt haltOpts
po poweroffOpts
reboot rebootOpts
}

func Run() (int, error) {
var opts options
var allOpts allOptions = allOptions{}
var args []string
var err error

if _, err = parseArgs(&opts); err != nil {
setCmdName(os.Args[0])
if args, err = parseArgs(&allOpts); err != nil {
return ExitFailuer, nil
}
return halt()

switch cmdName {
case "halt":
return halt(args, allOpts.halt)
case "poweroff":
return poweroff(args, allOpts.po)
case "reboot":
return reboot(args, allOpts.reboot)
}
return ExitFailuer, errors.New("mimixbox failed to parse the argument (not halt, poweroff, reboot error)")
}

func halt() (int, error) {
func halt(args []string, opts haltOpts) (int, error) {
fmt.Fprintln(os.Stdout, "The system is going down NOW !!")
if err := killInitProcess(); err != nil {

recordWtmp()
if err := powerOffSystem(); err != nil {
return ExitFailuer, err
}
return ExitSuccess, nil
}

func poweroff(args []string, opts poweroffOpts) (int, error) {
if err := powerOffSystem(); err != nil {
return ExitFailuer, err
}
return ExitSuccess, nil
}

func killInitProcess() error {
func reboot(args []string, opts rebootOpts) (int, error) {
if err := rebootSystem(); err != nil {
return ExitFailuer, err
}
return ExitSuccess, nil
}

func powerOffSystem() error {
process, err := os.FindProcess(1)
if err != nil {
return err
Expand All @@ -74,30 +116,51 @@ func killInitProcess() error {
// LINUX_REBOOT_CMD_HALT is semantically correct, but
// implementations vary (halt(8)), and most users will
// want power off.
syscall.Reboot(0x4321fedc)
return nil
return syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
}

func rebootSystem() error {
process, err := os.FindProcess(1)
if err != nil {
return err
}
err = process.Signal(syscall.Signal(mb.ConvSignalNameToNum("SIGUSR2")))
if err != nil {
return err
}
syscall.Sync()
return syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}

func recordWtmp() {
return // TODO:
}

func setCmdName(name string) {
cmdName = name
}

func parseArgs(opts *options) ([]string, error) {
func parseArgs(opts *allOptions) ([]string, error) {
p := initParser(opts)

args, err := p.Parse()
if err != nil {
return nil, err
}

if opts.Version {
mb.ShowVersion(cmdName, version)
osExit(ExitSuccess)
}

showVersionAndExitIfNeeded(opts)
return args, nil
}

func initParser(opts *options) *flags.Parser {
func initParser(opts *allOptions) *flags.Parser {
parser := flags.NewParser(opts, flags.Default)
parser.Name = cmdName
parser.Usage = "[OPTIONS]"

return parser
}

func showVersionAndExitIfNeeded(opts *allOptions) {
if opts.halt.Version || opts.po.Version || opts.reboot.Version {
mb.ShowVersion(cmdName, version)
osExit(ExitSuccess)
}
}

0 comments on commit e5b790e

Please sign in to comment.