Protect devfs last ruleset acquiring with mutex

This commit is contained in:
yo 2023-06-25 23:32:15 +02:00
parent 812c77790a
commit 5eed121f0b
2 changed files with 16 additions and 5 deletions

View File

@ -1,11 +1,12 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"fmt"
"sync"
"strings"
"io/ioutil"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -55,6 +56,7 @@ var (
gFetchIntoDS string
gFetchFrom string
gMdevfs sync.Mutex
rootCmd = &cobra.Command{
Use: "gocage",

View File

@ -476,12 +476,17 @@ func genNatIpv4(jail *Jail) ([]string, error) {
return ippair, nil
}
func buildDevfsRuleSet(jail *Jail) (error, int) {
// FIXME : Must lock this function so parallel start do not
func buildDevfsRuleSet(jail *Jail, m *sync.Mutex) (error, int) {
rulesets := []int{}
m.Lock()
//defer m.Unlock()
// Get known rulesets
out, err := executeCommand("devfs rule showsets")
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error executing command \"devfs rule showsets\": %v; command returned: %s\n", err, out)), 0
}
srs := strings.Split(out, "\n")
@ -509,19 +514,23 @@ func buildDevfsRuleSet(jail *Jail) (error, int) {
// UPDATE: We don't need this as every jail have a default Devfs_ruleset value
/*ds, err := getDatastoreFromArray(jail.Datastore, gDatastores)
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error getting datastore %s for jail %s", jail.Datastore, jail.Name)), 0
}
defaultrs, err := strconv.ParseInt(ds.DefaultJailConfig.Devfs_ruleset, 10, 64)
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error parsing default devfs_ruleset for datastore %s", jail.Datastore)), 0
}*/
// Clone configured devfs_ruleset to a dynamic ruleset
if false == isStringInArray(srs, jail.Config.Devfs_ruleset) {
m.Unlock()
return errors.New(fmt.Sprintf("Unknown ruleset: %s", jail.Config.Devfs_ruleset)), 0
}
rs, _ := strconv.Atoi(jail.Config.Devfs_ruleset)
err = copyDevfsRuleset(ruleset, rs)
m.Unlock()
if err != nil {
return err, 0
}
@ -1343,7 +1352,7 @@ func StartJail(args []string) {
net = append(net, strings.Split(cj.Config.Vnet_interfaces, " ")...)
}
err, dynrs := buildDevfsRuleSet(cj)
err, dynrs := buildDevfsRuleSet(cj, &gMdevfs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return