-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- handle IPC mechanism communication between scoped application and CLI TODO: - cli - unit test - integration test - IPC namespace switch Closes: #1108
- Loading branch information
1 parent
68a7e7a
commit c3698bf
Showing
11 changed files
with
366 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/criblio/scope/inspect" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// inspectCmd represents the inspect command | ||
var inspectCmd = &cobra.Command{ | ||
Use: "inspect [flags]", | ||
Short: "Display scope inspect", | ||
Long: `Outputs inspect info.`, | ||
Example: `scope inspect`, | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
inspect.Inspect() | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(inspectCmd) | ||
} |
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,9 @@ | ||
package inspect | ||
|
||
import "fmt" | ||
|
||
// startConfig represents a processed configuration | ||
func Inspect() error { | ||
fmt.Println("inspect Test") | ||
return nil | ||
} |
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,74 @@ | ||
package util | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const MessageQueueDefaultMode int = 0644 | ||
const MessageQueueMaxQueueSize int64 = 10 | ||
const MessageQueueMaxMessageSize int64 = 8192 | ||
|
||
type MessageQueue struct { | ||
fd int | ||
name string // Name of the message queue | ||
attributes MessageQueueAttributes | ||
} | ||
|
||
type MessageQueueAttributes struct { | ||
Flags int64 // Message queue flags | ||
MaxQueueSize int64 // Max # of messages in queue | ||
MaxMessageSize int64 // Max message size in bytes | ||
CurrentMessages int64 // Current # of messages in queue | ||
} | ||
|
||
func Open(name string, flags int64, cfg MessageQueueAttributes) (*MessageQueue, error) { | ||
unixName, err := unix.BytePtrFromString(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mqfd, _, errno := unix.Syscall6( | ||
unix.SYS_MQ_OPEN, | ||
uintptr(unsafe.Pointer(unixName)), | ||
uintptr(flags), | ||
uintptr(MessageQueueDefaultMode), | ||
uintptr(unsafe.Pointer(&cfg)), | ||
0, // Last 2 unused | ||
0, | ||
) | ||
|
||
if errno != 0 { | ||
return nil, errno | ||
} | ||
|
||
return &MessageQueue{ | ||
fd: int(mqfd), | ||
name: name, | ||
attributes: cfg, | ||
}, nil | ||
} | ||
|
||
func (mq *MessageQueue) Close() error { | ||
return unix.Close(int(mq.fd)) | ||
} | ||
|
||
func (mq *MessageQueue) Unlink() error { | ||
unixName, err := unix.BytePtrFromString(mq.name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, _, errno := unix.Syscall( | ||
unix.SYS_MQ_UNLINK, | ||
uintptr(unsafe.Pointer(unixName)), | ||
0, // Last 2 unused | ||
0, | ||
) | ||
if errno != 0 { | ||
return errno | ||
} | ||
|
||
return nil | ||
} |
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,8 @@ | ||
# IPC Notes | ||
|
||
- For IPC communication we are using pair of message queue: | ||
|
||
- `scope` is responsible to create and read on `ScopeCLI` | ||
- Scoped application is responsible to create and read `ScopeIPC.<PID>` | ||
|
||
TODO: Picture |
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,77 @@ | ||
#define _GNU_SOURCE | ||
|
||
#include "ipc.h" | ||
|
||
/* Message-queue | ||
* | ||
* Message-queue system limits which are related to `ipcOpenConnection` | ||
* are defined in following files: | ||
* | ||
* "/proc/sys/fs/mqueue/msg_max" | ||
* - describes maximum number of messsages in a queue (QUEUE_MSG_MAX) | ||
* | ||
* "/proc/sys/fs/mqueue/msgsize_max" | ||
* - describes maximum message size in a queue (QUEUE_MSG_SIZE) | ||
* | ||
* "/proc/sys/fs/mqueue/queues_max" | ||
* - describes system-wide limit on the number of message queues that can be created | ||
* | ||
* See details in: https://man7.org/linux/man-pages/man7/mq_overview.7.html | ||
*/ | ||
|
||
#define QUEUE_MSG_MAX 10 | ||
#define QUEUE_MSG_SIZE 8192 | ||
|
||
int | ||
ipcSend(mqd_t mqdes, const char *data, size_t len) { | ||
return scope_mq_send(mqdes, data, len, 0); | ||
} | ||
|
||
int | ||
ipcSendWithTimeout(mqd_t mqdes, const char *data, size_t len, const struct timespec *req) { | ||
return scope_mq_timedsend(mqdes, data, len, 0, req); | ||
} | ||
|
||
ssize_t | ||
ipcRecv(mqd_t mqdes, char *buf, size_t len) { | ||
return scope_mq_receive(mqdes, buf, len, 0); | ||
} | ||
|
||
ssize_t | ||
ipcRecvWithTimeout(mqd_t mqdes, char *buf, size_t len, const struct timespec *req) { | ||
return scope_mq_timedreceive(mqdes, buf, len, 0, req); | ||
} | ||
|
||
mqd_t | ||
ipcOpenConnection(const char *name, int flags) { | ||
struct mq_attr msg_attr = {.mq_flags = 0, | ||
.mq_maxmsg = QUEUE_MSG_MAX, | ||
.mq_msgsize = QUEUE_MSG_SIZE, | ||
.mq_curmsgs = 0}; | ||
|
||
if (flags == O_WRONLY) { | ||
return scope_mq_open(name, flags); | ||
} | ||
return scope_mq_open(name, flags, 0666, &msg_attr); | ||
} | ||
|
||
int | ||
ipcCloseConnection(mqd_t mqdes) { | ||
return scope_mq_close(mqdes); | ||
} | ||
|
||
int | ||
ipcDestroyConnection(const char *name) { | ||
return scope_mq_unlink(name); | ||
} | ||
|
||
int | ||
ipcGetInfo(mqd_t mqdes, long *mqFlags, long *mqMaxMsg, long *mqMsgSize, long *mqCurMsgs) { | ||
struct mq_attr attr; | ||
int res = scope_mq_getattr(mqdes, &attr); | ||
*mqFlags = attr.mq_flags; | ||
*mqMaxMsg = attr.mq_maxmsg; | ||
*mqMsgSize = attr.mq_msgsize; | ||
*mqCurMsgs = attr.mq_curmsgs; | ||
return res; | ||
} |
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 @@ | ||
#ifndef __IPC_H__ | ||
#define __IPC_H__ | ||
|
||
#include "scopestdlib.h" | ||
|
||
// Send message from Inter-process communication | ||
int ipcSend(mqd_t, const char *, size_t); | ||
int ipcSendWithTimeout(mqd_t, const char *, size_t, const struct timespec *); | ||
|
||
// Receive message from Inter-process communication | ||
ssize_t ipcRecv(mqd_t, char *, size_t); | ||
ssize_t ipcRecvWithTimeout(mqd_t, char *, size_t, const struct timespec *); | ||
|
||
// Manage Inter-process communication | ||
mqd_t ipcOpenConnection(const char *, int); | ||
int ipcCloseConnection(mqd_t); | ||
int ipcDestroyConnection(const char *); | ||
int ipcGetInfo(mqd_t, long *, long *, long *, long *); | ||
|
||
|
||
#endif // __IPC_H__ |
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
Oops, something went wrong.