Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
ipc: delete semaphores when DelInst()
Browse files Browse the repository at this point in the history
  • Loading branch information
LionNatsu committed Mar 19, 2018
1 parent 635a3ca commit d2c40e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (i *Container) AddInst(name string) error {
return i.Instance(name).Init()
}
func (i *Container) DelInst(name string) error {
i.Instance(name).RunLock().Remove()
i.Instance(name).FileSystemLock().Remove()
return os.RemoveAll(path.Join(i.InstDir(), name))
}

Expand Down
10 changes: 7 additions & 3 deletions internal/container/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

const (
LayerDirName = "layers"
SemIdFileSystemMutex = 101
SemIdBootMutex = 1
SemIdFileSystemMutex = 0x11
SemIdRunMutex = 0x22
)

var (
Expand Down Expand Up @@ -143,7 +143,7 @@ func (i *Instance) Run(ctx context.Context, ctnInfo *nspawn.ContainerInfo, runIn
defer RecoverTerminalAttr()
machineId := fmt.Sprintf("%s_%x", i.Name, ipc.GenFileKey(i.Parent.GetCiel().GetBasePath(), 0))

CriticalSection := ipc.NewMutex(i.Dir(), SemIdBootMutex, true)
CriticalSection := i.RunLock()

if i.RunningAsExcludedMode() {
return -1, ErrMode
Expand Down Expand Up @@ -265,6 +265,10 @@ func (i *Instance) FileSystemLock() ipc.Mutex {
return ipc.NewMutex(i.Dir(), SemIdFileSystemMutex, true)
}

func (i *Instance) RunLock() ipc.Mutex {
return ipc.NewMutex(i.Dir(), SemIdRunMutex, true)
}

func (i *Instance) Shell(user string) (string, error) {
shell := "/bin/sh"
passwdFileName := path.Join(i.MountPoint(), "/etc/passwd")
Expand Down
10 changes: 10 additions & 0 deletions ipc/lowlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ int init_sem(int semid, int val) {
sop.sem_flg = 0;
return semop(semid, &sop, 1);
}
int remove_sem(int semid) {
return semctl(semid, 0, IPC_RMID);
}
int op_sem(int semid, int op, int flag) {
struct sembuf sop;
sop.sem_num = 0;
Expand Down Expand Up @@ -122,6 +125,10 @@ func (s Semaphore) TryWaitHold() bool {
_, err := C.try_wait_sem(C.int(s))
return err == nil
}
func (s Semaphore) Remove() error {
_, err := C.remove_sem(C.int(s))
return err
}

type Mutex struct {
s Semaphore
Expand Down Expand Up @@ -152,3 +159,6 @@ func (m Mutex) TryLock() bool {
return m.s.TryWait()
}
}
func (m Mutex) Remove() error {
return m.s.Remove()
}

0 comments on commit d2c40e9

Please sign in to comment.