-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fda0406
commit 9cb104f
Showing
14 changed files
with
382 additions
and
22 deletions.
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
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,21 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"rlxos/services" | ||
"rlxos/services/power/service" | ||
) | ||
|
||
func main() { | ||
client, err := services.Import(service.PowerManager{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
args := service.RebootArgs{} | ||
var reply service.PoweroffReply | ||
|
||
if err := client.Call("Poweroff", args, &reply); 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,20 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"reflect" | ||
) | ||
|
||
func getObjectId(object interface{}) string { | ||
t := reflect.TypeOf(object) | ||
if t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
} | ||
id := t.Name() | ||
return id | ||
} | ||
|
||
func getObjectSocket(object interface{}) string { | ||
return path.Join(servicesPath, fmt.Sprintf("%s.sock", getObjectId(object))) | ||
} |
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,51 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/rpc" | ||
"os" | ||
) | ||
|
||
const ( | ||
servicesPath = "/run/services" | ||
) | ||
|
||
type Middleware func(id string, conn net.Conn) error | ||
|
||
func Export(object interface{}, middlewares ...Middleware) error { | ||
id := getObjectId(object) | ||
socketPath := getObjectSocket(object) | ||
if _, err := os.Stat(socketPath); err == nil { | ||
return fmt.Errorf("service socket already exists") | ||
} | ||
|
||
loadPolicies() | ||
|
||
rpc.Register(object) | ||
listener, err := net.Listen("unix", socketPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer listener.Close() | ||
defer os.Remove(socketPath) | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
log.Printf("ERROR: [%s] failed to accept connection %v", id, err) | ||
continue | ||
} | ||
|
||
for _, middleware := range append([]Middleware{AuthenticationMiddleware}, middlewares...) { | ||
if err := middleware(id, conn); err != nil { | ||
conn.Close() | ||
return err | ||
} | ||
} | ||
|
||
go rpc.ServeConn(conn) | ||
} | ||
|
||
} |
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,38 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/rpc" | ||
"os" | ||
) | ||
|
||
type Client struct { | ||
conn net.Conn | ||
client *rpc.Client | ||
id string | ||
} | ||
|
||
func (c *Client) Call(fun string, args any, reply any) error { | ||
return c.client.Call(fmt.Sprintf("%s.%s", c.id, fun), args, reply) | ||
} | ||
|
||
func Import(object interface{}) (*Client, error) { | ||
id := getObjectId(object) | ||
socketPath := getObjectSocket(object) | ||
|
||
if _, err := os.Stat(socketPath); err != nil { | ||
return nil, fmt.Errorf("service socket not exists") | ||
} | ||
|
||
conn, err := net.Dial("unix", socketPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to server: %v", err) | ||
} | ||
|
||
return &Client{ | ||
id: id, | ||
conn: conn, | ||
client: rpc.NewClient(conn), | ||
}, nil | ||
} |
Oops, something went wrong.