im trying to read data from pipe using poll, here is code:
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#define MAX_BUFF_SIZE 128
int main(void){
char buff[MAX_BUFF_SIZE];
char *buf;
int fd,ret,len;
struct pollfd fdinfo[1];
if((fd = open("/home/s/pipe", O_RDONLY)) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
fdinfo[0].fd = fd;
fdinfo[0].events = POLLIN|POLLPRI ;
while(1){
ret = poll(fdinfo,1,-1);
if (ret < 0) printf("\n\r error");
if ( ((fdinfo[0].revents&POLLIN) == POLLI开发者_如何学编程N) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) ){
len = read(fdinfo[0].fd, buff, MAX_BUFF_SIZE);
if (len > 0){
buff[len] = 0;
buf = (char *) malloc(len);
memcpy(buf, buff, len);
printf("\n read len %d\n %s",len, buf);
free(buf);
}
}
}
return 0;
}
It does not seems to work fine - after each properly data i got a lot of garbage in output, but i dont know where it comes frome. So im asking for help.
thx in advance
If there is at least MAX_BUFF_SIZE bytes available, len will be set to MAX_BUFF_SIZE, and buff[len] will write past the end of buff. After that, all bets are off. You can fix this by passing MAX_BUFF_SIZE - 1 to read().
However, more worryingly, you're not copying the \0 in your memcpy() call, as the number of bytes including the \0 is len + 1.
While we're at it:
- don't cast the return from
malloc() - check the return from
malloc()isn'tNULL - if
poll()errors, you're still checking thereventsfield, which won't be applicable any more - your test
if ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) )could better be writtenif (fdinfo[0].revents & (POLLIN | POLLPRI))but shouldn't be necessary anyway, as you've askedpoll()to look for these two things only
加载中,请稍侯......
精彩评论