-
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 1ba6384
Showing
10 changed files
with
290 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,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,78 @@ | ||
#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 | ||
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); | ||
} | ||
|
||
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,22 @@ | ||
#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 ipcCreateNonBlockReadConnection(const char *); | ||
mqd_t ipcOpenWriteConnection(const char *); | ||
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
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,96 @@ | ||
/* | ||
* ipcclient - IPC client | ||
* | ||
* A simple program to test communication between scoped process using | ||
* gcc -g test/manual/ipcclient.c -lrt -o ipcclient | ||
* ./ipcclient <scoped_PID> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <mqueue.h> | ||
#include <string.h> | ||
#include <sys/stat.h> | ||
|
||
#define MAX_MESSAGES 10 | ||
#define MSG_BUFFER_SIZE 8192 | ||
|
||
static mqd_t readMqDesc; | ||
#define READ_MQ_NAME "/ScopeCLI" | ||
|
||
void cleanupReadDesc(void) { | ||
mq_close(readMqDesc); | ||
mq_unlink(READ_MQ_NAME); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
struct mq_attr attr = {.mq_flags = 0, | ||
.mq_maxmsg = MAX_MESSAGES, | ||
.mq_msgsize = MSG_BUFFER_SIZE, | ||
.mq_curmsgs = 0}; | ||
|
||
int res = EXIT_FAILURE; | ||
char WriteMqName[4096] = {0}; | ||
if (argc != 2) { | ||
printf("Usage: %s <pid_scope_process>\n", argv[0]); | ||
return res; | ||
} | ||
|
||
snprintf(WriteMqName, sizeof(WriteMqName), "/ScopeIPC.%s", argv[1]); | ||
|
||
mqd_t writeMqDesc; | ||
|
||
// Ugly hack disable umask to handle run as a root | ||
mode_t oldMask = umask(0); | ||
readMqDesc = mq_open(READ_MQ_NAME, O_RDONLY | O_CREAT, 0666, &attr); | ||
if (readMqDesc == (mqd_t)-1) { | ||
perror("!mq_open readMqDesc failed"); | ||
return res; | ||
} | ||
umask(oldMask); | ||
|
||
atexit(cleanupReadDesc); | ||
|
||
writeMqDesc = mq_open(WriteMqName, O_WRONLY); | ||
if (writeMqDesc == (mqd_t)-1) { | ||
perror("!mq_open writeMqDesc failed"); | ||
return res; | ||
} | ||
|
||
char Txbuf[MSG_BUFFER_SIZE] = {0}; | ||
char RxBuf[MSG_BUFFER_SIZE] = {0}; | ||
|
||
printf("\nPass example message to stdin [type 'quit' to stop]\n"); | ||
while (fgets(Txbuf, MSG_BUFFER_SIZE, stdin) != NULL) { | ||
struct mq_attr readAttr = {0}; | ||
if(strcmp("quit\n", Txbuf) == 0) { | ||
break; | ||
} | ||
|
||
// Send message to scoped process | ||
if (mq_send(writeMqDesc, Txbuf, strlen(Txbuf) + 1, 0) == -1) { | ||
perror("!mq_send writeMqDesc failed"); | ||
goto end_iteration; | ||
} | ||
|
||
|
||
// Read response | ||
if (mq_receive(readMqDesc, RxBuf, MSG_BUFFER_SIZE, NULL) == -1) { | ||
perror("!mq_receive readMqDesc failed"); | ||
goto end_iteration; | ||
} | ||
|
||
printf("Response from pid process %s : %s", argv[1], RxBuf); | ||
|
||
end_iteration: | ||
printf("\nPass example message to stdin [type 'quit' to stop]\n"); | ||
memset(Txbuf, 0, MSG_BUFFER_SIZE); | ||
memset(RxBuf, 0, MSG_BUFFER_SIZE); | ||
} | ||
|
||
if (mq_close(writeMqDesc) == -1) { | ||
perror ("!mq_close writeMqDesc failed"); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |