Related
I'm trying to use named pipe in Linux, using mkfifo.
Below are codes for sender / reader of named pipe :
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define MSG_SIZE 100
//SENDER
int main() {
char msg[MSG_SIZE] = "FIFO is for first-in, first-out.";
int fd;
int cnt;
if(fd = open("./hello", O_RDWR) < 0) { //fifo acts as a typical file
perror("Failed to make fifo : ");
printf("Error : %d\n", errno);
exit(1);
}
int len = strlen(msg) + 1;
while(1) {
if(write(fd, msg, len) == -1) {
perror("Failed to write into fifo : ");
exit(1);
}
else{
printf("Succeed to write msg!");
break;
}
}
sleep(1);
return 0;
}
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MSG_SIZE 100
//READER
int main() {
char msg_rc[MSG_SIZE];
int fd;
if(access("./hello",F_OK) == 0) {
fd = open("./hello", O_RDWR);
if(fd < 0) {
perror("Failed to open fifo : ");
exit(1);
}
}
else {
if(fd = mkfifo("./hello", O_RDWR) < 0) {
perror("Failed to open fifo : ");
exit(1);
}
}
printf("Waiting for fifo update");
while(1) {
if(read(fd, msg_rc, MSG_SIZE) == -1) {
perror("Failed to read from fifo : ");
exit(1);
}
printf("Read msg : %s\n", msg_rc);
break;
}
return 0;
}
When I used these codes,
% ./fifoSender
FIFO is for first-in, first-out.Succeed to write msg!%
% ./fifoReader
fifoSender shows msg, and fifoReader shows no msg, which are not desirable behavior.
Any advice will be helpful. Thanks in advance
The main problem is these two lines
if(fd = open("./hello", O_RDWR) < 0) {
if(fd = mkfifo("./hello", O_RDWR) < 0) {
They don't do what you think they do.
Firstly, O_RDWR is a wrong argument for mkfifo. It expects Unix file permissions mask, such as S_IRWU"S_IRWG|S_IRWO, or just 0666.
Secondly and most importantly, since the priority of = is lower than that of <, they are parsed like this:
if(fd = (open("./hello", O_RDWR) < 0)) {
Now, when open("./hello", O_RDWR) is less than 0 (which is highly likely, given that it is either never created or created with an incorrect mode), fd becomes 1, which is the standard output file descriptor.
The correct incantation is
if((fd = open("./hello", O_RDWR)) < 0) { // note () around assignment
if((fd = mkfifo("./hello", 0666)) < 0) {
There are several more subtle shortcomings.
Here is the working code,
// sender
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define PIPE_NAME "./hello"
int main(int argc, char *argv[])
{
const char *message = "FIFO is for first-in, first-out\n";
// Open the pipe for writing
int pipe_fd = open(PIPE_NAME, O_WRONLY);
if (pipe_fd < 0) {
perror("open");
exit(1);
}
// Write data to the pipe
while(1){
int n = write(pipe_fd, message, strlen(message));
if (n < 0) {
perror("write");
exit(1);
}
sleep(1);
}
// Close the pipe
close(pipe_fd);
return 0;
}
//receiver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define PIPE_NAME "./hello"
int main(int argc, char *argv[])
{
// Create the named pipe
mknod(PIPE_NAME, S_IFIFO | 0666, 0);
// Open the pipe for reading
int pipe_fd = open(PIPE_NAME, O_RDONLY);
if (pipe_fd < 0) {
perror("open");
exit(1);
}
// Read data from the pipe
char buffer[1024];
while(1){
int n = read(pipe_fd, buffer, sizeof(buffer));
if (n < 0) {
perror("read");
exit(1);
}
// Print the data that was read
printf("Received: %.*s\n", n, buffer);
}
// Close the pipe
close(pipe_fd);
return 0;
}
Is it possible to change TCP congestion control algorithm from Cubic to Reno or vice versa using setsockopt call from C++ code in linux?
I am looking for an example code of doing so.
You can use the TCP_CONGESTION socket option to get or set the congestion control algorithm for a socket to one of the values listed in /proc/sys/net/ipv4/tcp_allowed_congestion_control or to any one of the values in /proc/sys/net/ipv4/tcp_available_congestion_control if your process is privileged.
C example:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char buf[256];
socklen_t len;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
len = sizeof(buf);
if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
{
perror("getsockopt");
return -1;
}
printf("Current: %s\n", buf);
strcpy(buf, "reno");
len = strlen(buf);
if (setsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, len) != 0)
{
perror("setsockopt");
return -1;
}
len = sizeof(buf);
if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
{
perror("getsockopt");
return -1;
}
printf("New: %s\n", buf);
close(sock);
return 0;
}
For me outputs:
Current: cubic
New: reno
My platform:
Ubuntu 17.10 32-bit (Vbox VM)
Qt Creator 3.5.1 (opensource)
Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 32 bit
I am trying to invoke a multithreaded program (with arguments) using QProcess.start().
My program runs fine on terminal, i.e. periodically prints on stdout.
Using a TextEdit to log the stdout/stderr of the program I have connected QProcess readyReadStandardOutput/Error signals.
The stdout/stderr that comes from the main thread of the program is correctly shown on the TextEdit, the rest of the output (the one from all the other threads) is not shown.
EDIT
On the main thread an HTTP server is listening.
If a HTTP request is performed by browser at the url "127.0.0.1:32001" (port 32001 is hard coded in the QT code), when the HTTP request is received the program appends the HTTP packet and all the pending output from the other thread (moduleThread) to the TextEdit, so it could be a problem of flushing.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "http_server.h"
static pthread_t moduleThr;
static pthread_attr_t moduleThread_attr;
static bool one_second_elapsed;
static sem_t oneSecondSem;
void *moduleThread(void *arg0)
{
bool one_second_elapsed_local;
while (1)
{
sem_wait(&oneSecondSem);
one_second_elapsed_local = one_second_elapsed;
one_second_elapsed = false;
sem_post(&oneSecondSem);
if (one_second_elapsed_local)
fprintf(stdout, "Hello world each second!\r\n");
usleep(50000);
}
}
static void oneSecElapsed(int signum)
{
sem_wait(&oneSecondSem);
one_second_elapsed = true;
sem_post(&oneSecondSem);
}
static void TIMER_1sec_init(void)
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &oneSecElapsed;
sigaction (SIGALRM, &sa, NULL);
/* Configure the timer to expire after 1 sec... */
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
/* ... and every 1 sec after that... */
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, &timer, NULL);
}
/*
* ======== main ========
*/
int main(int argc, char *argv[])
{
const char *tcpport;
if (argc > 2)
{
fprintf(stdout, "id = %s\r\n", argv[1]);
fprintf(stdout, "tcpport = %s\r\n", argv[2]);
tcpport = argv[2];
}
else
exit(-1);
sem_init(&oneSecondSem, 0, 1);
one_second_elapsed = false;
/* Create thread quadro manager */
pthread_attr_init(&moduleThread_attr);
pthread_attr_setstacksize(&moduleThread_attr, 2048);
pthread_create(&moduleThr, &moduleThread_attr, moduleThread, NULL);
TIMER_1sec_init();
HTTPSERVER_init(tcpport);
/* should never return */
return (0);
}
http_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memset() */
#include <stdbool.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include "module.h"
typedef struct { char *name, *value; } header_t;
static header_t reqhdr[17] = { {"\0", "\0"} };
//#define PORT "32001" /* Port to listen on */
#define BACKLOG 10 /* Passed to listen() */
static char *buf;
static char *method; // "GET" or "POST"
static char *uri; // "/index.html" things before '?'
static char *qs; // "a=1&b=2" things after '?'
static char *prot; // "HTTP/1.1"
static char *payload; // for POST
static int payload_size;
// get request header
char *request_header(const char* name)
{
header_t *h = reqhdr;
while(h->name) {
if (strcmp(h->name, name) == 0) return h->value;
h++;
}
return NULL;
}
void handle(int newsock)
{
//static int count = 0;
/* recv(), send(), close() */
int rcvd;
buf = malloc(65535);
rcvd=recv(newsock, buf, 65535, 0);
if (rcvd<0) // receive error
fprintf(stderr,("recv() error\n"));
else if (rcvd==0) // receive socket closed
fprintf(stderr,"Client disconnected unexpectedly.\n");
else // message received
{
buf[rcvd] = '\0';
//fputs(buf, stdout);
//fprintf(stdout, "count = %d\n", count);
method = strtok(buf, " \t\r\n");
uri = strtok(NULL, " \t");
prot = strtok(NULL, " \t\r\n");
fprintf(stderr, "\x1b[32m + [%s] %s\x1b[0m\n", method, uri);
if ((qs = strchr(uri, '?')))
{
*qs++ = '\0'; //split URI
} else {
qs = uri - 1; //use an empty string
}
header_t *h = reqhdr;
char *t = (char *)reqhdr;
char *t2;
while(h < reqhdr+16) {
char *k,*v;
k = strtok(NULL, "\r\n: \t");
if (!k)
break;
v = strtok(NULL, "\r\n");
while(*v && *v==' ')
v++;
h->name = k;
h->value = v;
h++;
fprintf(stderr, "[H] %s: %s\n", k, v);
t = v + 1 + strlen(v);
if (t[1] == '\r' && t[2] == '\n')
break;
}
t+=2;
t++; // now the *t shall be the beginning of user payload
t2 = request_header("content-length"); // and the related header if there is
payload = t;
payload_size = t2 ? atol(t2) : (rcvd-(t-buf));
fprintf(stdout, "-- payload len = %d >", payload_size);
fputs(payload, stdout);
fprintf(stdout, "<\r\n");
if (strcmp(method, "GET") == 0)
{
fprintf(stdout, "\nit's a GET!\r\n");
}
else if (strcmp(method, "POST") == 0)
{
fprintf(stdout, "\nit's a POST!\r\n");
}
sprintf(buf, "HTTP/1.1 200 OK\r\n\r\n");
send(newsock, buf, strlen(buf), 0);
close(newsock);
free(buf);
}
}
int HTTPSERVER_init(const char *tcpport)
{
int sock;
struct addrinfo hints, *res;
int reuseaddr = 1; /* True */
sigset_t sigset, oldset;
sigfillset(&sigset);
pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
/* Get the address info */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(NULL, tcpport, &hints, &res) != 0) {
perror("getaddrinfo");
return 1;
}
/* Create the socket */
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == -1) {
perror("socket");
return 1;
}
/* Enable the socket to reuse the address */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
perror("setsockopt");
return 1;
}
/* Bind to the address */
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
perror("bind");
return 1;
}
/* Listen */
if (listen(sock, BACKLOG) == -1) {
perror("listen");
return 1;
}
freeaddrinfo(res);
/* Main loop */
while (1) {
printf("waiting on accept\n");
fflush(stdout);
socklen_t size = sizeof(struct sockaddr_in);
struct sockaddr_in their_addr;
int newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
if (newsock == -1) {
perror("accept");
}
else {
printf("Got a connection from %s on port %d\n",
inet_ntoa(their_addr.sin_addr), htons(their_addr.sin_port));
handle(newsock);
}
}
close(sock);
return 0;
}
As pointed out by #eyllanesc, the pending strings "Hello world each second!\r\n" come out eventually after several seconds.
The moduleThread shoud be a thread and not a different process: this is verified by commadn "ps aux" that shows only "../program/testApp test 32001".
Could any of you kindly give me a hint on what is going on here please?
Thanks for your time,
Francesco
My operating system is Centos 6.4.
For select the linux programmer's manual says: "select may update the timeout parameter to indicate how much time was left...."
I wonder why it is 'may' rather than 'must'? Is there a system version or a kernel version?
I make a test.
test_code01.cpp
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
struct timeval g_timeout;
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Becasue select can update timeout, we need to set value for timeout.
// g_timeout.tv_sec = 3;
// g_timeout.tv_usec = 0;
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
I execute this test_code01.cpp, and the result select wait for 3 seconds for only first times.In my opinion, the select modify timeout.So I modify code follow:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
printf("fdset add fd start+++\n");
for(int i = 0; i < 2000; i++)
{
FD_SET(1025+i, &readfds);
}
printf("%d--%s\n", errno, strerror(errno));
printf("fdset add 2000 fd finish...\n");
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
I think we should see a change in timeout in thredproc_looktimeout.But no change.I don't know why?
The manual page means that on return the timeout might be updated to indicate the amount of time left. It absolutely doesn't mean that the timeout will be updated while the wait is still in progress.
And even if it was, there is no possible way you could detect it. Your attempt to do with threads won't work because it has no synchronization of any kind. Among other problems, the compiler could optimize the while loop to read g_timeout only once. And there is no conceivable synchronization mechanism you could use.
Because a valid timeout may also be
null in the case you want to wait forever
both fields of the timeval are set to 0, select returns immediately
In both those cases subtraction doesn't make sense, hence no "must."
Your addition of code makes this a duplicate of https://stackoverflow.com/a/7382740/4080377. You're seeing a copy of the variable, not the real thing
I get a answer.In my system, select actualy modify timeout value.I don't fully understand select may update timeout before.
Now,
(1)all_time:it is initial value of timeout.For example, timeout.tv_sec is equal to three and timeout.tv_usec is equal to zero.
(2)consume_time:it is the time value that from start to return of select to spended.
(3)left_time:it does't use time for select.
Linux manual actually implies the meaning that left_time add consume_time is equal to all_time.
Why it "may update".I think Tom Scanlan's answer is right.
The follow code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
if(g_timeout.tv_sec != 3 && g_timeout.tv_usec != 0)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
sleep(3);
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
This code can get no-zero value in thredproc_looktimeout thread function.
In addition, such is in case, As David Schwartz said: it absolutely doesn't mean that the timeout will be updated while the wait is still in progress.
You can try this code. I added some code and some comment.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
if(g_timeout.tv_sec != 3 && g_timeout.tv_usec != 0)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
/*new code added*/
FD_ZERO(&readfs);
/* before you set any fd in readfs, you should set 0 all fds in reads. For security, because some times
readfs can alloced some old trash data place, and a chance in readfs may some data.*/
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
if(ret_select == -1){
/*return some error or what you want here. Because if ret_select is equal to 1. That is a problem*/
}
// Recover sock_clienfd state
/* FD_SET(sock_clientfd, &readfds);*/
/* you don't need to same fd set again. That shuold be always some data ready to read()*/
if(ret_select == 0)
{
printf("select wait timeout.\n");
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
continue;
}
/*bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}*/
/*Here you can check just sock_clienfd. If that is true, some data ready to read on socket.*/
if(FD_ISSET(sock_clientfd,&readfs){
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
}
/*sleep(3);*/ /*your select() function already wait 3 second before go down*/
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
Hi am trying to implement net link user code and kernel code every thing works fine for unicast (src_addr.nl_groups = 0;). For mulicast, user code bind call always fails for non zero src_addr.nl_groups value. Really am not sure what value to put for multicast and how to proceed further. I checked the usage of netlink_broadcast in kernel source tree, so I put the same group Id value (RTMGRP_LINK) here. For unicast I found good number of help in internet but for multicast I don't think so . So Please help me to proceed further.
Error am getting is:
bind: No such file or directory
./a.out: can't bind socket (3)and err : -1: No such file or directory
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define NETLINK_TEST 28
#define GROUP_IB 1
#define MAX_PAYLOAD 1024
struct sockaddr_nl src_addr, dst_addr;
struct nlmsghdr *nlh = NULL;
struct msghdr msg;
struct iovec iov;
int sock_fd;
int main(int argc, char ** argv)
{
int err;
sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TEST);
if (sock_fd<0) {
char s[BUFSIZ];
sprintf( s, "%s: can't assign fd for socket", argv[0] );
perror(s);
return -1;
}
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = 0; // Unicast
//src_addr.nl_groups = RTMGRP_LINK; /* Multicast, bind call always fails for non zero values */
err = bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
perror("bind");
if (err<0) {
char s[BUFSIZ];
sprintf( s, "%s: can't bind socket (%d)and err : %d", argv[0], sock_fd,err );
perror(s);
return -1;
}
memset(&dst_addr, 0, sizeof(dst_addr));
nlh = (struct nlhmsghdr *) malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
iov.iov_base = (void *)nlh;
iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
msg.msg_name = (void *)&dst_addr;
msg.msg_namelen = sizeof(dst_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("pid : %d\n Waiting for messages from kernel...\n",getpid());
recvmsg(sock_fd, &msg, 0);
printf("Message : %s\n", NLMSG_DATA(nlh));
close(sock_fd);
return 0;
}
Netlink socket binds are sensitive to what USER you are- I've seen them reliably fail if you are not running the program in question as 'root', at least on RedHat 6.
Try running as root 1st, before changing your logic. If you get the same failure as you do in normal operation, then you know it isn't (necessarily) a permissions issue.
The issue is
sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TEST);
Does you kernel module define the NETLINK_TEST family? your own family might must be supported at kernel module and it should post the message in the proper group using nlmsg_multicast()
RTMGRP_LINK is group defined in NETLINK_ROUTE.
This sample code is example for multicast
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <unistd.h>
#define MYPROTO NETLINK_USERSOCK
#define MYMGRP 21
int open_netlink(void)
{
int sock;
struct sockaddr_nl addr;
int group = MYMGRP;
sock = socket(AF_NETLINK, SOCK_RAW, MYPROTO);
if (sock < 0) {
printf("sock < 0.\n");
return sock;
}
memset((void *) &addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
/* addr.nl_groups = MYMGRP; */
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
printf("bind < 0.\n");
return -1;
}
if (setsockopt(sock, 270, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) < 0) {
printf("setsockopt < 0\n");
return -1;
}
return sock;
}
void read_event(int sock)
{
struct sockaddr_nl nladdr;
struct msghdr msg;
struct iovec iov;
char buffer[65536];
int ret;
iov.iov_base = (void *) buffer;
iov.iov_len = sizeof(buffer);
msg.msg_name = (void *) &(nladdr);
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("Ok, listening.\n");
ret = recvmsg(sock, &msg, 0);
if (ret < 0)
printf("ret < 0.\n");
else
printf("Received message payload: %s\n", NLMSG_DATA((struct nlmsghdr *) &buffer));
}
int main(int argc, char *argv[])
{
int nls;
nls = open_netlink();
if (nls < 0)
return nls;
while (1)
read_event(nls);
return 0;
}
kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#define MYPROTO NETLINK_USERSOCK
#define MYGRP 21
static struct sock *nl_sk = NULL;
static void send_to_user(void)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
char *msg = "Hello from kernel";
int msg_size = strlen(msg) + 1;
int res;
pr_info("Creating skb.\n");
skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_KERNEL);
if (!skb) {
pr_err("Allocation failure.\n");
return;
}
nlh = nlmsg_put(skb, 0, 1, NLMSG_DONE, msg_size + 1, 0);
strcpy(nlmsg_data(nlh), msg);
pr_info("Sending skb.\n");
res = nlmsg_multicast(nl_sk, skb, 0, MYGRP, GFP_KERNEL);
if (res < 0)
pr_info("nlmsg_multicast() error: %d\n", res);
else
pr_info("Success.\n");
}
static int __init hello_init(void)
{
pr_info("Inserting hello module.\n");
nl_sk = netlink_kernel_create(&init_net, MYPROTO, NULL);
if (!nl_sk) {
pr_err("Error creating socket.\n");
return -10;
}
send_to_user();
netlink_kernel_release(nl_sk);
return 0;
}
static void __exit hello_exit(void)
{
pr_info("Exiting hello module.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");