Skip to content

Commit e5b790e

Browse files
committed
Added poweroff/reboot command in source code of halt.
1 parent 98782e1 commit e5b790e

File tree

4 files changed

+90
-21
lines changed

4 files changed

+90
-21
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ All notable changes to this project will be documented in this file. The format
33

44
## [0.32.1] - 2021-12-19
55
### Added
6-
- tr command.
76
- gzip command.
7+
- tr command.
8+
- poweroff command.
9+
- reboot command.
810
### Changed
911
- halt command.
1012
- Fix bug(GitHub Issue #33)

docs/introduction/en/CommandAppletList.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
| mv | Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY|
3636
| nl| Write each FILE to standard output with line numbers added|
3737
| path | Manipulate filename path|
38+
| poweroff| Power off the system|
3839
| printenv| Print environment variable|
3940
| pwd | Print Working Directory|
41+
| reboot| Reboot the system|
4042
| remove-shell|Remove shell name from /etc/shells|
4143
| reset| Reset terminal|
4244
| rm | Remove file(s) or directory(s)|

internal/applets/applet.go

+2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ func init() {
134134
"mv": {mv.Run, "Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY"},
135135
"nl": {nl.Run, "Write each FILE to standard output with line numbers added"},
136136
"path": {path.Run, "Manipulate filename path"},
137+
"poweroff": {halt.Run, "Power off the system"},
137138
"printenv": {printenv.Run, "Print environment variable"},
138139
"pwd": {pwd.Run, "Print Working Directory"},
139140
"remove-shell": {removeShell.Run, "Remove shell name from /etc/shells"},
141+
"reboot": {halt.Run, "Reboot the system"},
140142
"reset": {reset.Run, "Reset terminal"},
141143
"rm": {rm.Run, "Remove file(s) or directory(s)"},
142144
"rmdir": {rmdir.Run, "Remove directory"},

internal/applets/pmutils/halt/halt.go

+83-20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package halt
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"syscall"
@@ -26,9 +27,9 @@ import (
2627
"github.com/jessevdk/go-flags"
2728
)
2829

29-
const cmdName string = "halt"
30+
var cmdName string = "halt"
3031

31-
const version = "1.0.0"
32+
const version = "1.0.1"
3233

3334
var osExit = os.Exit
3435

@@ -38,29 +39,70 @@ const (
3839
ExitFailuer
3940
)
4041

41-
type options struct {
42+
type haltOpts struct {
4243
Version bool `short:"v" long:"version" description:"Show halt command version"`
4344
}
4445

46+
type poweroffOpts struct {
47+
Version bool `short:"v" long:"version" description:"Show poweroff command version"`
48+
}
49+
50+
type rebootOpts struct {
51+
Version bool `short:"v" long:"version" description:"Show reboot command version"`
52+
}
53+
54+
type allOptions struct {
55+
halt haltOpts
56+
po poweroffOpts
57+
reboot rebootOpts
58+
}
59+
4560
func Run() (int, error) {
46-
var opts options
61+
var allOpts allOptions = allOptions{}
62+
var args []string
4763
var err error
4864

49-
if _, err = parseArgs(&opts); err != nil {
65+
setCmdName(os.Args[0])
66+
if args, err = parseArgs(&allOpts); err != nil {
5067
return ExitFailuer, nil
5168
}
52-
return halt()
69+
70+
switch cmdName {
71+
case "halt":
72+
return halt(args, allOpts.halt)
73+
case "poweroff":
74+
return poweroff(args, allOpts.po)
75+
case "reboot":
76+
return reboot(args, allOpts.reboot)
77+
}
78+
return ExitFailuer, errors.New("mimixbox failed to parse the argument (not halt, poweroff, reboot error)")
5379
}
5480

55-
func halt() (int, error) {
81+
func halt(args []string, opts haltOpts) (int, error) {
5682
fmt.Fprintln(os.Stdout, "The system is going down NOW !!")
57-
if err := killInitProcess(); err != nil {
83+
84+
recordWtmp()
85+
if err := powerOffSystem(); err != nil {
86+
return ExitFailuer, err
87+
}
88+
return ExitSuccess, nil
89+
}
90+
91+
func poweroff(args []string, opts poweroffOpts) (int, error) {
92+
if err := powerOffSystem(); err != nil {
5893
return ExitFailuer, err
5994
}
6095
return ExitSuccess, nil
6196
}
6297

63-
func killInitProcess() error {
98+
func reboot(args []string, opts rebootOpts) (int, error) {
99+
if err := rebootSystem(); err != nil {
100+
return ExitFailuer, err
101+
}
102+
return ExitSuccess, nil
103+
}
104+
105+
func powerOffSystem() error {
64106
process, err := os.FindProcess(1)
65107
if err != nil {
66108
return err
@@ -74,30 +116,51 @@ func killInitProcess() error {
74116
// LINUX_REBOOT_CMD_HALT is semantically correct, but
75117
// implementations vary (halt(8)), and most users will
76118
// want power off.
77-
syscall.Reboot(0x4321fedc)
78-
return nil
119+
return syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
120+
}
121+
122+
func rebootSystem() error {
123+
process, err := os.FindProcess(1)
124+
if err != nil {
125+
return err
126+
}
127+
err = process.Signal(syscall.Signal(mb.ConvSignalNameToNum("SIGUSR2")))
128+
if err != nil {
129+
return err
130+
}
131+
syscall.Sync()
132+
return syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
133+
}
134+
135+
func recordWtmp() {
136+
return // TODO:
137+
}
138+
139+
func setCmdName(name string) {
140+
cmdName = name
79141
}
80142

81-
func parseArgs(opts *options) ([]string, error) {
143+
func parseArgs(opts *allOptions) ([]string, error) {
82144
p := initParser(opts)
83145

84146
args, err := p.Parse()
85147
if err != nil {
86148
return nil, err
87149
}
88-
89-
if opts.Version {
90-
mb.ShowVersion(cmdName, version)
91-
osExit(ExitSuccess)
92-
}
93-
150+
showVersionAndExitIfNeeded(opts)
94151
return args, nil
95152
}
96153

97-
func initParser(opts *options) *flags.Parser {
154+
func initParser(opts *allOptions) *flags.Parser {
98155
parser := flags.NewParser(opts, flags.Default)
99156
parser.Name = cmdName
100157
parser.Usage = "[OPTIONS]"
101-
102158
return parser
103159
}
160+
161+
func showVersionAndExitIfNeeded(opts *allOptions) {
162+
if opts.halt.Version || opts.po.Version || opts.reboot.Version {
163+
mb.ShowVersion(cmdName, version)
164+
osExit(ExitSuccess)
165+
}
166+
}

0 commit comments

Comments
 (0)