Ich habe die NetLink -API in Docker nicht ausgeführtLinux

Linux verstehen
Anonymous
 Ich habe die NetLink -API in Docker nicht ausgeführt

Post by Anonymous »

Kann mir jemand helfen, das Problem anzugehen, warum sich die Verbindung abgelehnt hat, wenn ich das Programm in Docker -Container ausführe? Wenn ich beim Ausführen von Docker-Containern-netz = host-privilegiert wurde, funktioniert es, ansonsten wurde er zurückgeführt, wenn es in "(send (sk_nl, nl_hdr, nl_hdr-> nlmsg_len, 0)". Es ist immer noch fehlgeschlagen.

Code: Select all

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

#define SEND_MESSAGE_LEN        (NLMSG_LENGTH(sizeof(struct cn_msg) + \
sizeof(enum proc_cn_mcast_op)))
#define RECV_MESSAGE_LEN        (NLMSG_LENGTH(sizeof(struct cn_msg) + \
sizeof(struct proc_event)))
#define SEND_MESSAGE_SIZE       (NLMSG_SPACE(SEND_MESSAGE_LEN))
#define RECV_MESSAGE_SIZE       (NLMSG_SPACE(RECV_MESSAGE_LEN))
#define MAX(x,y)                ((x > y) ? (x) : (y))
#define BUFF_SIZE               (MAX(MAX(SEND_MESSAGE_SIZE, RECV_MESSAGE_SIZE), 1024))
#define PROC_CN_MCAST_LISTEN    1
#define HAM_MAX_CLNT            2048
#define HAM_MAX_FD              (HAM_MAX_CLNT + 256)
#define NETLINK_PRIO            49
#define DOMAIN_PRIO             49
#define HAM_EPOLL_TIMEOUT       (10 * 1000)

/* HAM需要监控的线程 */
typedef enum ham_thread_num {
HAM_THREAD_CNMSG,
HAM_THREAD_NETLINK,
HAM_THREAD_DOMAIN,
HAM_THREAD_DELAY_RESTART,
#if defined(PRODUCT_TYPE_CU) || defined(PRODUCT_TYPE_DU)
HAM_THREAD_CLI_CMD,
#endif
HAM_THREAD_FAST_HANG_DTCT,
HAM_THREAD_NUM
} ham_thread_num_e;

typedef enum HAM_LOG_TYPE{
HAM_LOG_KILL = 0,
HAM_LOG_RESTART,
HAM_LOG_RUN_EVENT,
HAM_LOG_ERROR,
HAM_LOG_REG_ALIVE,
HAM_LOG_REG_RESTART,
HAM_LOG_TYPE_NUM,
} ham_log_type;

static int g_ham_quit;

#define ham_debug(fmt, ...) do {                                                    \
if (true) {                           \
printf("[HAM:%s:%d]"fmt, __FILE__, __LINE__, ##__VA_ARGS__);   \
}                                                                               \
} while (0)

#define HAM_ZLOG_ERROR ham_debug

#define HAM_LOG_EMERGENCY(type, fmt, ...)  HAM_ZLOG_ERROR(fmt, ##__VA_ARGS__);

void ham_thread_alive_set(ham_thread_num_e thread)
{

}

void ham_request_exit(const char *msg)
{
g_ham_quit = 1;
}

int ham_set_priority(int prio)
{
return 0;
}

bool ham_will_exit(void)
{
return g_ham_quit ? true : false;
}

int ham_os_set_fd_close_on_exec(int fd)
{
int flags;

if (fd < 0) {
return -1;
}

flags = fcntl(fd, F_GETFD);
if (flags == -1) {
return -1;
}
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1) {
return -1;
}

return 0;
}

static void ham_dispatch_netlink_msg(struct cn_msg *cn_hdr)
{

}

static int ham_recv_netlink_msg(int sk_nl)
{
char buff[BUFF_SIZE];
int recv_len;
struct sockaddr_nl from_nla;
struct nlmsghdr *nlh;
struct cn_msg *cn_hdr;
socklen_t from_nla_len;

from_nla_len = sizeof(from_nla);
recv_len = recvfrom(sk_nl, buff, sizeof(buff), 0,
(struct sockaddr *)&from_nla, &from_nla_len);

if (recv_len < 0) {
ham_debug("%s: netlink recvfrom fail: %s\n", __func__, strerror(errno));

if(errno == EBADF || errno == ECONNREFUSED || errno == ENOTCONN || errno == ENOTSOCK){
return -1;
}
return 0;
}
if (from_nla_len != sizeof(from_nla)) {
ham_debug("%s: bad address size reading netlink socket: %s\n",
__func__, strerror(errno));
return 0;
}
if (from_nla.nl_groups != CN_IDX_PROC || from_nla.nl_pid != 0) {
/* ham_debug("%s: nl_groups != CN_IDX_PROC: %s\n",
__func__, strerror(errno));  */
return 0;
}

nlh = (struct nlmsghdr *)buff;
while (NLMSG_OK(nlh, recv_len)) {
cn_hdr = NLMSG_DATA(nlh);
if (nlh->nlmsg_type == NLMSG_NOOP) {
nlh = NLMSG_NEXT(nlh, recv_len);
continue;
}
if ((nlh->nlmsg_type == NLMSG_ERROR) || (nlh->nlmsg_type == NLMSG_OVERRUN)) {
break;
}
ham_dispatch_netlink_msg(cn_hdr);
if (nlh->nlmsg_type == NLMSG_DONE) {
break;
}
nlh = NLMSG_NEXT(nlh, recv_len);
}
return 0;
}

void *ham_process_netlink()
{
struct sockaddr_nl my_nla;
struct nlmsghdr *nl_hdr;
struct cn_msg *cn_hdr;
enum proc_cn_mcast_op *mcop_msg;
char buff[BUFF_SIZE];
int sk_nl;
int size, ret;
int err_no = 0;

sk_nl = -1;
if (prctl(PR_SET_NAME, "ham_process_netlink")) {
ham_debug("%s: %s", __func__, strerror(errno));
return NULL;
}
if (ham_set_priority(NETLINK_PRIO) != 0) {
ret = -1;
err_no = errno;
ham_debug("%s: set priority fail: %s\n", __func__, strerror(errno));
goto fail;
}

/* Setup netlink connector */
sk_nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
if (sk_nl < 0) {
ret = -2;
err_no = errno;
ham_debug("%s: socket sk_nl fail: %s\n", __func__, strerror(errno));
goto fail;
}
(void)ham_os_set_fd_close_on_exec(sk_nl);

size = 4*1024*1024;
ret = setsockopt(sk_nl, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
if (ret != 0) {
ham_debug ("%s: socket set recvbuffer error: %s", __func__, strerror (errno));
}

struct timeval tv = {5, 0};
ret = setsockopt(sk_nl, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
if (ret != 0) {
ham_debug ("%s: socket set recv timeout error: %s", __func__, strerror (errno));
}

my_nla.nl_family = AF_NETLINK;
my_nla.nl_groups = CN_IDX_PROC;
my_nla.nl_pid = getpid();
my_nla.nl_pad = 0;
if (bind(sk_nl, (struct sockaddr *)&my_nla, sizeof(my_nla)) < 0) {
ret = -3;
err_no = errno;
ham_debug("%s: binding sk_nl fail: %s\n", __func__, strerror(errno));
goto fail;
}
(void)memset(buff, 0, sizeof(buff));
nl_hdr = (struct nlmsghdr *)buff;
cn_hdr = (struct cn_msg *)NLMSG_DATA(nl_hdr);
mcop_msg = (enum proc_cn_mcast_op *)&cn_hdr->data[0];
*mcop_msg = PROC_CN_MCAST_LISTEN;
/* Fill the netlink header */
nl_hdr->nlmsg_len = SEND_MESSAGE_LEN;
nl_hdr->nlmsg_type = NLMSG_DONE;
nl_hdr->nlmsg_flags = 0;
nl_hdr->nlmsg_seq = 0;
nl_hdr->nlmsg_pid = getpid();
/* Fill the connector header */
cn_hdr->id.idx = CN_IDX_PROC;
cn_hdr->id.val = CN_VAL_PROC;
cn_hdr->seq = 0;
cn_hdr->ack = 0;
cn_hdr->len = sizeof(enum proc_cn_mcast_op);

if (send(sk_nl, nl_hdr, nl_hdr->nlmsg_len, 0) != nl_hdr->nlmsg_len) {
ret = -4;
err_no = errno;
ham_debug("%s: send proc connector msg fail: %s\n", __func__, strerror(errno));
goto fail;
}
ham_debug("%s\n", "started ham_process_netlink");

while (!ham_will_exit()) {
if(ham_recv_netlink_msg(sk_nl) < 0){
ret = -5;
err_no = errno;
goto fail;
}

/* set thread alive */
ham_thread_alive_set(HAM_THREAD_NETLINK);
}

fail:
if (sk_nl >= 0) {
(void)close(sk_nl);
}
ham_request_exit(NULL);
HAM_LOG_EMERGENCY(HAM_LOG_ERROR, "Thread \"%s\" exit, ham will be restart. res=%d, %s(%d)\r\n",
__func__, ret, strerror(err_no), err_no);
return NULL;
}

int main()
{
ham_process_netlink(0);
pause();
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post