-
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 - integration test - IPC namespace switch Closes: #1108
- Loading branch information
1 parent
8e098d9
commit 14b5cfe
Showing
13 changed files
with
675 additions
and
3 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,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
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,160 @@ | ||
#define _GNU_SOURCE | ||
|
||
#include "ipc.h" | ||
|
||
/* Inter-process communication module based on the 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 | ||
|
||
#define INPUT_CMD_LEN(x) (sizeof(x)-1) | ||
#define CMD_TABLE_SIZE(x) (sizeof(x)/sizeof(x[0])) | ||
|
||
/* Output message function definition | ||
* Fills up the input buffer return the size of message | ||
* | ||
*/ | ||
typedef size_t (*out_msg_func_t)(char *, size_t); | ||
|
||
static size_t | ||
cmdGetAttach(char *buf, size_t len) { | ||
// scope_snprintf(buf, len, "[%d] %s", g_proc.pid, g_cfg.funcs_attached ? "Attached" : "Detached"); | ||
// Excluding the terminating null byte | ||
return scope_snprintf(buf, len, "GetAttach"); | ||
} | ||
|
||
static size_t | ||
cmdUnknown(char *buf, size_t len) { | ||
// Excluding the terminating null byte | ||
return scope_snprintf(buf, len, "Unknown"); | ||
} | ||
|
||
typedef struct { | ||
const char *name; // command name string [in] | ||
size_t nameLen; // command name string length [in] | ||
ipc_cmd_t cmd; // command id [out] | ||
} input_cmd_table_t; | ||
|
||
typedef struct { | ||
ipc_cmd_t cmd; // command id [in] | ||
out_msg_func_t func; // output func [out] | ||
} output_cmd_table_t; | ||
|
||
static int | ||
ipcSend(mqd_t mqdes, const char *data, size_t len) { | ||
return scope_mq_send(mqdes, data, len, 0); | ||
} | ||
|
||
static ssize_t | ||
ipcRecv(mqd_t mqdes, char *buf, size_t len) { | ||
return scope_mq_receive(mqdes, buf, len, 0); | ||
} | ||
|
||
mqd_t | ||
ipcCreateNonBlockReadConnection(const char *name) { | ||
struct mq_attr attr = {.mq_flags = 0, | ||
.mq_maxmsg = QUEUE_MSG_MAX, | ||
.mq_msgsize = QUEUE_MSG_SIZE, | ||
.mq_curmsgs = 0}; | ||
return scope_mq_open(name, O_RDONLY | O_CREAT | O_CLOEXEC | O_NONBLOCK, 0666, &attr); | ||
} | ||
|
||
mqd_t | ||
ipcOpenWriteConnection(const char *name) { | ||
return scope_mq_open(name, O_WRONLY | O_NONBLOCK); | ||
} | ||
|
||
int | ||
ipcCloseConnection(mqd_t mqdes) { | ||
return scope_mq_close(mqdes); | ||
} | ||
|
||
int | ||
ipcDestroyConnection(const char *name) { | ||
return scope_mq_unlink(name); | ||
} | ||
|
||
long | ||
ipcInfoMsgCount(mqd_t mqdes) { | ||
struct mq_attr attr; | ||
int res = scope_mq_getattr(mqdes, &attr); | ||
if (res == 0) { | ||
return attr.mq_curmsgs; | ||
} | ||
return -1; | ||
} | ||
|
||
bool | ||
ipcIsActive(mqd_t mqdes) { | ||
return mqdes != (mqd_t) -1; | ||
} | ||
|
||
bool | ||
ipcRequestMsgHandler(mqd_t mqdes, ipc_cmd_t *cmdRes) { | ||
// TODO do this dynamically based on mqdes | ||
char buf[QUEUE_MSG_SIZE] = {0}; | ||
|
||
ssize_t recvLen = ipcRecv(mqdes, buf, sizeof(buf)); | ||
if (recvLen == -1) { | ||
return FALSE; | ||
} | ||
|
||
// The length should be inline with outputCmdTable | ||
input_cmd_table_t inputCmdTable[] = { | ||
{"getStatus", INPUT_CMD_LEN("getStatus"), IPC_CMD_GET_ATTACH_STATUS} | ||
}; | ||
|
||
for (int i = 0; i < CMD_TABLE_SIZE(inputCmdTable); ++i) { | ||
if ((recvLen == inputCmdTable[i].nameLen) && !scope_memcmp(inputCmdTable[i].name, buf, recvLen)) { | ||
*cmdRes = inputCmdTable[i].cmd; | ||
return TRUE; | ||
} | ||
} | ||
*cmdRes = IPC_CMD_UNKNOWN; | ||
return TRUE; | ||
} | ||
|
||
bool | ||
ipcResponseMsgHandler(mqd_t mqdes, ipc_cmd_t cmd) { | ||
// TODO do this dynamically based on mqdes | ||
char buf[QUEUE_MSG_SIZE] = {0}; | ||
size_t len; | ||
if (cmd > IPC_CMD_UNKNOWN) { | ||
return FALSE; | ||
} | ||
|
||
// The length should be inline with inputCmdTable | ||
output_cmd_table_t outputCmdTable[] = { | ||
{IPC_CMD_GET_ATTACH_STATUS, cmdGetAttach}, | ||
{IPC_CMD_UNKNOWN, cmdUnknown} | ||
}; | ||
|
||
for (int i = 0; i < CMD_TABLE_SIZE(outputCmdTable); ++i) { | ||
if (cmd == outputCmdTable[i].cmd) { | ||
len = outputCmdTable[i].func(buf, sizeof(buf)); | ||
break; | ||
} | ||
} | ||
|
||
int sendRes = ipcSend(mqdes, buf, len); | ||
if (sendRes == -1) { | ||
return FALSE; | ||
} | ||
|
||
return TRUE; | ||
} |
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,25 @@ | ||
#ifndef __IPC_H__ | ||
#define __IPC_H__ | ||
|
||
#include "scopestdlib.h" | ||
#include "scopetypes.h" | ||
|
||
|
||
typedef enum { | ||
IPC_CMD_GET_ATTACH_STATUS, // Retrieve attach status | ||
IPC_CMD_UNKNOWN, // Unknown command (Must be last) | ||
} ipc_cmd_t; | ||
|
||
// Manage Inter-process connection | ||
mqd_t ipcCreateNonBlockReadConnection(const char *); | ||
mqd_t ipcOpenWriteConnection(const char *); | ||
int ipcCloseConnection(mqd_t); | ||
int ipcDestroyConnection(const char *); | ||
long ipcInfoMsgCount(mqd_t); | ||
bool ipcIsActive(mqd_t); | ||
|
||
// IPC Request/Response Handler | ||
bool ipcRequestMsgHandler(mqd_t, ipc_cmd_t *); | ||
bool ipcResponseMsgHandler(mqd_t, ipc_cmd_t); | ||
|
||
#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
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.