backive/cmd/backive_udev/main.go

50 lines
1.1 KiB
Go
Raw Normal View History

2021-10-04 21:56:44 +02:00
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"strings"
)
2021-10-04 21:56:44 +02:00
2021-11-14 15:07:20 +01:00
// main Simple main function for the udev callback executable, registered with the udev service.
2021-10-04 21:56:44 +02:00
func main() {
2021-12-13 23:44:23 +01:00
udev_logdir := "/var/log/backive"
udev_logname := "/var/log/backive/udev.log"
if _, err := os.Stat(udev_logdir); err == nil {
//ignore
} else if os.IsNotExist(err) {
os.MkdirAll(udev_logdir, 0755)
}
f, err := os.OpenFile(udev_logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Println("Error creating logfile!")
panic("no logfile no info")
}
defer f.Close()
log.SetOutput(f)
env := map[string]string{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
env[pair[0]] = pair[1]
2021-10-28 16:42:02 +02:00
log.Println(e)
}
c, err := net.Dial("unix", "/var/local/backive/backive.sock")
if err != nil {
log.Fatalln("Could not instantiate unix socket. Aborting")
}
jsonstr, err := json.Marshal(env)
if err != nil {
log.Fatalln("Could not convert to json. Aborting")
}
c.Write(jsonstr)
defer c.Close()
log.Printf("Sent %d bytes to unix socket.\n", len(jsonstr))
2021-10-04 21:56:44 +02:00
}