github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/runtime/engines/singularity/rpc/client/client.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package client
     7  
     8  import (
     9  	"net/rpc"
    10  	"os"
    11  
    12  	args "github.com/sylabs/singularity/internal/pkg/runtime/engines/singularity/rpc"
    13  	"github.com/sylabs/singularity/pkg/util/loop"
    14  )
    15  
    16  // RPC holds the state necessary for remote procedure calls.
    17  type RPC struct {
    18  	Client *rpc.Client
    19  	Name   string
    20  }
    21  
    22  // Mount calls tme mount RPC using the supplied arguments.
    23  func (t *RPC) Mount(source string, target string, filesystem string, flags uintptr, data string) (int, error) {
    24  	arguments := &args.MountArgs{
    25  		Source:     source,
    26  		Target:     target,
    27  		Filesystem: filesystem,
    28  		Mountflags: flags,
    29  		Data:       data,
    30  	}
    31  	var reply int
    32  	err := t.Client.Call(t.Name+".Mount", arguments, &reply)
    33  	return reply, err
    34  }
    35  
    36  // Mkdir calls the mkdir RPC using the supplied arguments.
    37  func (t *RPC) Mkdir(path string, perm os.FileMode) (int, error) {
    38  	arguments := &args.MkdirArgs{
    39  		Path: path,
    40  		Perm: perm,
    41  	}
    42  	var reply int
    43  	err := t.Client.Call(t.Name+".Mkdir", arguments, &reply)
    44  	return reply, err
    45  }
    46  
    47  // Chroot calls the chroot RPC using the supplied arguments.
    48  func (t *RPC) Chroot(root string, method string) (int, error) {
    49  	arguments := &args.ChrootArgs{
    50  		Root:   root,
    51  		Method: method,
    52  	}
    53  	var reply int
    54  	err := t.Client.Call(t.Name+".Chroot", arguments, &reply)
    55  	return reply, err
    56  }
    57  
    58  // LoopDevice calls the loop device RPC using the supplied arguments.
    59  func (t *RPC) LoopDevice(image string, mode int, info loop.Info64, maxDevices int, shared bool) (int, error) {
    60  	arguments := &args.LoopArgs{
    61  		Image:      image,
    62  		Mode:       mode,
    63  		Info:       info,
    64  		MaxDevices: maxDevices,
    65  		Shared:     shared,
    66  	}
    67  	var reply int
    68  	err := t.Client.Call(t.Name+".LoopDevice", arguments, &reply)
    69  	return reply, err
    70  }
    71  
    72  // SetHostname calls the sethostname RPC using the supplied arguments.
    73  func (t *RPC) SetHostname(hostname string) (int, error) {
    74  	arguments := &args.HostnameArgs{
    75  		Hostname: hostname,
    76  	}
    77  	var reply int
    78  	err := t.Client.Call(t.Name+".SetHostname", arguments, &reply)
    79  	return reply, err
    80  }
    81  
    82  // HasNamespace calls the HasNamespace RPC using the supplied arguments.
    83  func (t *RPC) HasNamespace(pid int, nstype string) (bool, error) {
    84  	arguments := &args.HasNamespaceArgs{
    85  		Pid:    pid,
    86  		NsType: nstype,
    87  	}
    88  	var reply int
    89  	err := t.Client.Call(t.Name+".HasNamespace", arguments, &reply)
    90  	if err != nil {
    91  		return false, err
    92  	}
    93  	if reply == 1 {
    94  		return true, err
    95  	}
    96  	return false, err
    97  }
    98  
    99  // SetFsID calls the setfsid RPC using the supplied arguments.
   100  func (t *RPC) SetFsID(uid int, gid int) (int, error) {
   101  	arguments := &args.SetFsIDArgs{
   102  		UID: uid,
   103  		GID: gid,
   104  	}
   105  	var reply int
   106  	err := t.Client.Call(t.Name+".SetFsID", arguments, &reply)
   107  	return reply, err
   108  }