本文整理汇总了C++中sendfile函数的典型用法代码示例。如果您正苦于以下问题:C++ sendfile函数的具体用法?C++ sendfile怎么用?C++ sendfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sendfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: client_func
//.........这里部分代码省略.........
struct put *petition = (struct put *) &bytes;
// Create file from received data
// receive size
char size[10];
ssize_t bytes_read;
if((bytes_read = read(client_sd, size, 10)) == -1) {
perror("c> Error receiving data from client.\n");
break;
} else if(bytes_read == 0) {
printf("c> Client has closed the connection.\n");
close(client_sd);
break;
}
int filesize = atoi(size);
printf("c> File size - %d\n", filesize);
//receive actual file data
char file_data[filesize];
if((bytes_read = read(client_sd, file_data, filesize)) == -1) {
perror("c> Error receiving data from client.\n");
break;
} else if(bytes_read == 0) {
printf("c> Client has closed the connection.\n");
close(client_sd);
break;
}
//create file
FILE *file = fopen(petition->src, "wb");
fwrite(file_data, filesize, 1, file);
fclose(file);
// Create graphic
generate_graphics(petition->title,
petition->xLabel,
petition->yLabel,
petition->style,
petition->src);
printf("s> %s:%d OK gnuplot %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->title);
// Send ACK to client.
char byte[1];
byte[0] = PUT_CODE;
if((bytes_written = write(client_sd, byte, 1)) == -1) {
perror("s> Could not send data to client.\n");
break;
}
} else if (bytes[0] == GET_CODE) {
struct get *petition = (struct get *) &bytes;
printf("s> %s:%d init get graphic %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file);
//Open file
FILE *file;
char *file_data;
if ((file = fopen(petition->file, "rb")) != 0){
fseek(file, 0L, SEEK_END);
int size = ftell(file);
fseek(file, 0L, SEEK_SET);
//Allocate memory
file_data=(char *)malloc(size+1);
//Read file contents into buffer
fread(file_data, size, 1, file);
fclose(file);
char filesize[10];
sprintf(filesize, "%d", size);
// SEND size
if((bytes_written = send(client_sd, filesize, 10, 0)) == -1) {
perror("s> Could not send file size to client.\n");
break;
}
//SEND file
if((bytes_written = sendfile(client_sd, open(petition->file, 0), 0, size)) == -1) {
perror("s> Could not send data to client.\n");
break;
}
printf("s> %s:%d end get graphic %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file);
} else {
printf("s> %s:%d error sending file %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file);
}
}
memset(bytes, 0, sizeof(bytes));
}
close(client_sd);
pthread_exit(0);
}
示例2: do_sendfile
static int do_sendfile(const int out_fd, const int in_fd,
unsigned int num_send, filesize_t start_pos)
{
/* Probably should one day be shared with instance in ftpdataio.c */
static char* p_recvbuf;
unsigned int total_written = 0;
int retval;
enum EVSFSysUtilError error;
(void) start_pos;
(void) error;
#if defined(VSF_SYSDEP_HAVE_LINUX_SENDFILE) || \
defined(VSF_SYSDEP_HAVE_FREEBSD_SENDFILE) || \
defined(VSF_SYSDEP_HAVE_HPUX_SENDFILE) || \
defined(VSF_SYSDEP_HAVE_AIX_SENDFILE) || \
defined(VSF_SYSDEP_HAVE_SOLARIS_SENDFILE)
if (tunable_use_sendfile)
{
static int s_sendfile_checked;
static int s_runtime_sendfile_works;
if (!s_sendfile_checked || s_runtime_sendfile_works)
{
do
{
#ifdef VSF_SYSDEP_HAVE_LINUX_SENDFILE
retval = sendfile(out_fd, in_fd, NULL, num_send);
#elif defined(VSF_SYSDEP_HAVE_FREEBSD_SENDFILE)
{
/* XXX - start_pos will truncate on 32-bit machines - can we
* say "start from current pos"?
*/
off_t written = 0;
retval = sendfile(in_fd, out_fd, start_pos, num_send, NULL,
&written, 0);
/* Translate to Linux-like retval */
if (written > 0)
{
retval = (int) written;
}
}
#elif defined(VSF_SYSDEP_HAVE_SOLARIS_SENDFILE)
{
size_t written = 0;
struct sendfilevec the_vec;
vsf_sysutil_memclr(&the_vec, sizeof(the_vec));
the_vec.sfv_fd = in_fd;
the_vec.sfv_off = start_pos;
the_vec.sfv_len = num_send;
retval = sendfilev(out_fd, &the_vec, 1, &written);
/* Translate to Linux-like retval */
if (written > 0)
{
retval = (int) written;
}
}
#elif defined(VSF_SYSDEP_HAVE_AIX_SENDFILE)
{
struct sf_parms sf_iobuf;
vsf_sysutil_memclr(&sf_iobuf, sizeof(sf_iobuf));
sf_iobuf.header_data = NULL;
sf_iobuf.header_length = 0;
sf_iobuf.trailer_data = NULL;
sf_iobuf.trailer_length = 0;
sf_iobuf.file_descriptor = in_fd;
sf_iobuf.file_offset = start_pos;
sf_iobuf.file_bytes = num_send;
retval = send_file((int*)&out_fd, &sf_iobuf, 0);
if (retval >= 0)
{
retval = sf_iobuf.bytes_sent;
}
}
#else /* must be VSF_SYSDEP_HAVE_HPUX_SENDFILE */
{
retval = sendfile(out_fd, in_fd, start_pos, num_send, NULL, 0);
}
#endif /* VSF_SYSDEP_HAVE_LINUX_SENDFILE */
error = vsf_sysutil_get_error();
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, out_fd);
}
while (vsf_sysutil_retval_is_error(retval) &&
error == kVSFSysUtilErrINTR);
if (!s_sendfile_checked)
{
s_sendfile_checked = 1;
if (!vsf_sysutil_retval_is_error(retval) ||
error != kVSFSysUtilErrNOSYS)
{
s_runtime_sendfile_works = 1;
}
}
if (!vsf_sysutil_retval_is_error(retval))
{
return retval;
}
if (s_runtime_sendfile_works && error != kVSFSysUtilErrINVAL &&
error != kVSFSysUtilErrOPNOTSUPP)
{
return retval;
}
//.........这里部分代码省略.........
示例3: http_response_static_proc
//response static resource
void http_response_static_proc( httpHeader* reqHeader )
{
int len, cllen , ctlen ;
char path[1024] = {0};
header_out_t header_out;
memset( &header_out , 0 , sizeof( header_out ));
header_out.req = reqHeader;
get_file_path( reqHeader->uri , path );
struct stat stat_file;
int ret = stat( path , &stat_file );
if ( ret < 0 )
{
//printf( "not found page=%s \n" , path );
resp_error_page( &header_out , 404 );
return;
}
create_common_header( &header_out , 200 );
header_append_length( &header_out , stat_file.st_size );
resp_append_header( &header_out , HEADER_END_LINE );
int nwritten = write( reqHeader->connfd , header_out.data , header_out.length );
if (nwritten <= 0)
{
printf( "I/O error writing to client connfd=%d,len=%d: %s \n", reqHeader->connfd , header_out.length , strerror(errno));
return;
}
int fd = open( path , O_RDONLY );
if ( fd < 0 )
{
printf( "Open file Error:%s,errno=%d \n" , strerror(errno) , errno );
return;
}
//优化
//setsockopt (fd, SOL_TCP, TCP_CORK, &on, sizeof (on));
off_t offset = 0;
int force_close = 0;
while ( offset < stat_file.st_size )
{
int sendn = sendfile( reqHeader->connfd , fd , &offset , stat_file.st_size - offset );
if ( sendn < 0 )
{
//如果socket缓冲区不可用,则挂起等待可用
if (errno == EAGAIN || errno == EINTR )
{
if( anetHandup( reqHeader->connfd , 1000 , AE_WRITABLE ) < 0 )
{
//如果超时,退出
printf( "Sendfile anetHandup timeout.......\n" );
force_close = 1;
break;
}
else
{
//否则继续发送
continue;
}
}
else
{
break;
}
}
//printf( "Response uri=%s, connfd=%d,len=%d,send=%d \n", reqHeader->uri , reqHeader->connfd ,stat_file.st_size , sendn );
}
close( fd );
http_close( reqHeader , force_close );
}
示例4: cherokee_socket_sendfile
ret_t
cherokee_socket_sendfile (cherokee_socket_t *socket,
int fd,
size_t size,
off_t *offset,
ssize_t *sent)
{
static cherokee_boolean_t no_sys = false;
#if DARWIN_SENDFILE_API || FREEBSD_SENDFILE_API
int re;
#endif
#if DARWIN_SENDFILE_API
off_t _sent = size;
#endif
/* Exit if there is no sendfile() function in the system
*/
if (unlikely (no_sys))
return ret_no_sys;
/* If there is nothing to send then return now, this may be
* needed in some systems (i.e. *BSD) because value 0 may have
* special meanings or trigger occasional hidden bugs.
*/
if (unlikely (size == 0))
return ret_ok;
/* Limit size of data that has to be sent.
*/
if (size > MAX_SF_BLK_SIZE2)
size = MAX_SF_BLK_SIZE;
#if defined(LINUX_BROKEN_SENDFILE_API)
UNUSED(socket);
UNUSED(fd);
UNUSED(offset);
UNUSED(sent);
/* Large file support is set but native Linux 2.2 or 2.4 sendfile()
* does not support _FILE_OFFSET_BITS 64
*/
return ret_no_sys;
#elif defined(LINUX_SENDFILE_API)
/* Linux sendfile
*
* ssize_t
* sendfile (int out_fd, int in_fd, off_t *offset, size_t *count);
*
* ssize_t
* sendfile64 (int out_fd, int in_fd, off64_t *offset, size_t *count);
*/
*sent = sendfile (SOCKET_FD(socket), /* int out_fd */
fd, /* int in_fd */
offset, /* off_t *offset */
size); /* size_t count */
if (*sent < 0) {
switch (errno) {
case EINTR:
case EAGAIN:
#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
case EWOULDBLOCK:
#endif
return ret_eagain;
case EINVAL:
/* maybe sendfile is not supported by this FS (no mmap available),
* since more than one FS can be used (ext2, ext3, ntfs, etc.)
* we should retry with emulated sendfile (read+write).
*/
return ret_no_sys;
case ENOSYS:
/* This kernel does not support sendfile at all.
*/
no_sys = true;
return ret_no_sys;
}
return ret_error;
} else if (*sent == 0) {
/* It isn't an error, but it wrote nothing */
return ret_error;
}
#elif DARWIN_SENDFILE_API
/* MacOS X: BSD-like System Call
*
* int
* sendfile (int fd, int s, off_t offset, off_t *len,
* struct sf_hdtr *hdtr, int flags);
*/
re = sendfile (fd, /* int fd */
SOCKET_FD(socket), /* int s */
*offset, /* off_t offset */
&_sent, /* off_t *len */
NULL, /* struct sf_hdtr *hdtr */
0); /* int flags */
//.........这里部分代码省略.........
示例5: cacheopen
static void* cacheopen(char const* url) {
const char* basename = strrchr(url,'/');
assert(basename);
static char dest[508];
snprintf(dest,508,"%s/%s",cacheDir,basename);
bool didtemp = false;
int doopen(void) {
int result = open(dest,O_RDONLY);
if(result<0) {
char temp[512];
snprintf(temp,512,"%s.temp",dest);
if(xmlIOHTTPMatch(url)) {
void* ctx = NULL;
#if W3_ARE_NOT_MORONS
ctx = xmlNanoHTTPOpen(url, NULL);
if(xmlNanoHTTPReturnCode(ctx) != 200)
#else
if(true)
#endif
{
// XXX: it always is... w3.org dies on HTTP/1.0
#if W3_ARE_NOT_MORONS
xmlNanoHTTPClose(ctx);
#endif
fprintf(stderr,"Curl fallback for %s\n",url);
int pid = fork();
if(pid == 0) {
execlp("curl","curl","-o",temp,url,NULL);
abort();
}
int status = 0;
waitpid(pid,&status,0);
if(!(WIFEXITED(status) && (0 == WEXITSTATUS(status)))) {
fprintf(stderr,"CUrl failed! %x %d\n",status,WEXITSTATUS(status));
abort();
}
} else {
assert(0==xmlNanoHTTPSave(ctx,temp));
}
} else if(xmlIOFTPMatch(url)) {
void* ftp = xmlNanoFTPOpen(url);
int out = open(temp,O_WRONLY|O_TRUNC|O_CREAT,0644);
assert(out>0);
char buf[0x1000];
for(;;) {
int amt = xmlNanoFTPRead(ftp, buf, 0x1000);
if(amt==0) break;
assert(amt>0);
write(out,buf,amt);
}
close(out);
} else {
FILE* fp = xmlFileOpen(url);
struct stat fpstat;
if(!fp) {
fprintf(stderr,"No idea what to do with url %s\n",url);
abort();
}
int inp = fileno(fp);
assert(0==fstat(inp,&fpstat));
off_t left = fpstat.st_size;
int out = open(temp,O_WRONLY|O_TRUNC|O_CREAT,0644);
assert(out>0);
do {
ssize_t amt = sendfile(out,inp,NULL,left);
if(amt<0) {
perror(url);
}
assert(amt>=0);
left -= amt;
} while(left > 0);
fclose(fp);
close(out);
}
rename(temp,dest); // doesn't matter if fails
unlink(temp); // in case it failed
return doopen();
}
return result;
}
示例6: sendfile_full
ssize_t sendfile_full(int out_fd, const char *fn) {
_cleanup_fclose_ FILE *f;
struct stat st;
int r;
ssize_t s;
size_t n, l;
_cleanup_free_ char *buf = NULL;
assert(out_fd > 0);
assert(fn);
f = fopen(fn, "re");
if (!f)
return -errno;
r = fstat(fileno(f), &st);
if (r < 0)
return -errno;
s = sendfile(out_fd, fileno(f), NULL, st.st_size);
if (s < 0)
if (errno == EINVAL || errno == ENOSYS) {
/* continue below */
} else
return -errno;
else
return s;
/* sendfile() failed, fall back to read/write */
/* Safety check */
if (st.st_size > 4*1024*1024)
return -E2BIG;
n = st.st_size > 0 ? st.st_size : LINE_MAX;
l = 0;
while (true) {
char *t;
size_t k;
t = realloc(buf, n);
if (!t)
return -ENOMEM;
buf = t;
k = fread(buf + l, 1, n - l, f);
if (k <= 0) {
if (ferror(f))
return -errno;
break;
}
l += k;
n *= 2;
/* Safety check */
if (n > 4*1024*1024)
return -E2BIG;
}
r = write(out_fd, buf, l);
if (r < 0)
return -errno;
return (ssize_t) l;
}
示例7: client_test
//.........这里部分代码省略.........
fdin = sockfd = -1;
write_count = 0;
size = getsize(filename);
/* Save errno, as writing to a stream can change it in the
* interim.
*/
tmperr = errno;
if(size <= 0)
{
switch(size)
{
case 0:
die(err, EXIT_FAILURE,
"%s: %s: Cowardly refusing to send an empty file!\n",
program, filename);
break;
case -1:
die(err, tmperr,
"%s: Could not open \"%s\": %s\n",
program, filename, strerror(errno));
break;
default:
die(err, EXIT_FAILURE,
"%s: Could not open \"%s\"\n",
program, filename);
break;
}
}
if(!tcp_connect(host, atoi(port), &sockfd))
{
tmperr = errno;
switch(sockfd)
{
case -1:
die(err, EXIT_FAILURE,
"Error opening socket.\n");
break;
case -2:
die(err, EXIT_FAILURE,
"%s: Hostname lookup failure: %s\n",
program, host);
break;
case -3:
die(err, tmperr,
"%s: error connecting to %s, port %s.\n",
program, host, port);
break;
default: /* If we get here, the API is busted. */
die(err, EXIT_FAILURE,
"%s: Unknown error!\n", program);
break;
}
}
/* Read file and send to server */
if((fdin = open(filename, O_RDONLY)) == -1)
{
die(err, errno,
"%s: couldn't open %s for reading: %s\n",
program, filename, strerror(errno));
}
else
{
/* Toss file to server. Linux syscall, but its fast. :D*/
sendfile(sockfd, fdin, &offset, size);
if(wait_for_ack)
{
ack = tcp_recv(sockfd, 5000, MAX_READ, &req);
if(ack==NULL)
return false;
if((ack!=NULL) &&
(strlen(ack) > 0))
{
fprintf(stderr, "%s\n", ack);
check = parse(ack);
}
free(ack);
ack = NULL;
close(sockfd);
return check;
} /* if waiting for ack ... */
close(sockfd);
} /* if able to open filestream... */
return true;
}
示例8: do_sendfile
void do_sendfile(OFF_T offset, int i)
{
int in_fd;
struct stat sb;
int wait_status;
int wait_stat;
off_t before_pos, after_pos;
out_fd = create_server();
if ((in_fd = open(in_file, O_RDONLY)) < 0) {
tst_brkm(TBROK, cleanup, "open failed: %d", errno);
/*NOTREACHED*/}
if (stat(in_file, &sb) < 0) {
tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
/*NOTREACHED*/}
if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
tst_brkm(TBROK, cleanup,
"lseek before invoking sendfile failed: %d", errno);
/*NOTREACHED*/}
TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
tst_brkm(TBROK, cleanup,
"lseek after invoking sendfile failed: %d", errno);
/*NOTREACHED*/}
if (STD_FUNCTIONAL_TEST) {
/* Close the sockets */
shutdown(sockfd, SHUT_RDWR);
shutdown(s, SHUT_RDWR);
if (TEST_RETURN != testcases[i].exp_retval) {
tst_resm(TFAIL, "sendfile(2) failed to return "
"expected value, expected: %d, "
"got: %ld", testcases[i].exp_retval,
TEST_RETURN);
kill(child_pid, SIGKILL);
} else if (offset != testcases[i].exp_updated_offset) {
tst_resm(TFAIL, "sendfile(2) failed to update "
"OFFSET parameter to expected value, "
"expected: %d, got: %"PRId64,
testcases[i].exp_updated_offset, (int64_t)offset);
} else if (before_pos != after_pos) {
tst_resm(TFAIL, "sendfile(2) updated the file position "
" of in_fd unexpectedly, expected file position: %"PRId64", "
" actual file position %"PRId64,
(int64_t)before_pos, (int64_t)after_pos);
} else {
tst_resm(TPASS, "functionality of sendfile() is "
"correct");
wait_status = waitpid(-1, &wait_stat, 0);
}
} else {
tst_resm(TPASS, "call succeeded");
/* Close the sockets */
shutdown(sockfd, SHUT_RDWR);
shutdown(s, SHUT_RDWR);
if (TEST_RETURN != testcases[i].exp_retval) {
kill(child_pid, SIGKILL);
} else {
wait_status = waitpid(-1, &wait_stat, 0);
}
}
close(in_fd);
}
示例9: network_write_chunkqueue_linuxsendfile
int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes) {
chunk *c;
for(c = cq->first; (max_bytes > 0) && (NULL != c); c = c->next) {
int chunk_finished = 0;
switch(c->type) {
case MEM_CHUNK: {
char * offset;
off_t toSend;
ssize_t r;
size_t num_chunks, i;
struct iovec chunks[UIO_MAXIOV];
chunk *tc;
size_t num_bytes = 0;
/* build writev list
*
* 1. limit: num_chunks < UIO_MAXIOV
* 2. limit: num_bytes < max_bytes
*/
for (num_chunks = 0, tc = c;
tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV;
tc = tc->next, num_chunks++);
for (tc = c, i = 0; i < num_chunks; tc = tc->next, i++) {
if (buffer_string_is_empty(tc->mem)) {
chunks[i].iov_base = tc->mem->ptr;
chunks[i].iov_len = 0;
} else {
offset = tc->mem->ptr + tc->offset;
toSend = buffer_string_length(tc->mem) - tc->offset;
chunks[i].iov_base = offset;
/* protect the return value of writev() */
if (toSend > max_bytes ||
(off_t) num_bytes + toSend > max_bytes) {
chunks[i].iov_len = max_bytes - num_bytes;
num_chunks = i + 1;
break;
} else {
chunks[i].iov_len = toSend;
}
num_bytes += toSend;
}
}
if ((r = writev(fd, chunks, num_chunks)) < 0) {
switch (errno) {
case EAGAIN:
case EINTR:
r = 0;
break;
case EPIPE:
case ECONNRESET:
return -2;
default:
log_error_write(srv, __FILE__, __LINE__, "ssd",
"writev failed:", strerror(errno), fd);
return -1;
}
}
/* check which chunks have been written */
cq->bytes_out += r;
max_bytes -= r;
for(i = 0, tc = c; i < num_chunks; i++, tc = tc->next) {
if (r >= (ssize_t)chunks[i].iov_len) {
/* written */
r -= chunks[i].iov_len;
tc->offset += chunks[i].iov_len;
if (chunk_finished) {
/* skip the chunks from further touches */
c = c->next;
} else {
/* chunks_written + c = c->next is done in the for()*/
chunk_finished = 1;
}
} else {
/* partially written */
tc->offset += r;
chunk_finished = 0;
break;
}
}
break;
}
case FILE_CHUNK: {
ssize_t r;
off_t offset;
//.........这里部分代码省略.........
示例10: portable_sendfile
ssize_t portable_sendfile(int out_fd, int in_fd) {
off_t len = SENDFILE_CHUNK_SIZE;
if(sendfile(in_fd, out_fd, 0, &len, NULL, 0) == -1)
return -1;
return len;
}
示例11: main
int main(int argc, char *argv[]){
int listenfd,connfd,n;
int epoll_fd,cur_fd,newfd,number,stop;
struct epoll_event ev;
struct epoll_event events[size];
struct sockaddr_in server_address,client_address;
struct stat stat_buf;
const char* file_name=argv[1];
char buffer[BUF];
int file_ptr=open(file_name,O_RDONLY);
fstat(file_ptr,&stat_buf);
close(file_ptr);
bzero(&server_address,sizeof(server_address));
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(PORT);
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){
exit(0);
}
setnonblocking(listenfd);
int opt=1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
if(bind(listenfd,(struct sockaddr*)&server_address,sizeof(server_address))==-1){
exit(0);
}
if(listen(listenfd,BACKLOG)==-1){
exit(0);
}
epoll_fd=epoll_create(10);
ev.data.fd=listenfd;
ev.events=EPOLLIN | EPOLLET;
epoll_ctl(epoll_fd,EPOLL_CTL_ADD,listenfd,&ev);
cur_fd=1;stop=FALSE;
socklen_t len=sizeof(struct sockaddr_in);
while(!stop){
number=epoll_wait(epoll_fd,events,cur_fd,-1);
assert(number!=-1);
for(n=0;n<number;n++){
connfd=events[n].data.fd;
if(connfd==listenfd){
while((newfd=accept(listenfd,(struct sockaddr*)&client_address,&len))>0){
printf("connect with %s ,socket index %d\n",\
inet_ntoa(client_address.sin_addr),newfd);
setnonblocking(newfd);
ev.data.fd=newfd;
ev.events=EPOLLIN|EPOLLET|EPOLLHUP;
if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,newfd,&ev)==-1){
perror("epoll_ctl:add");
stop=TRUE;
}
cur_fd++;
}
if(newfd==-1){
if(errno!=EAGAIN&&errno!=ECONNABORTED\
&&errno!=EPROTO&&errno!=EINTR){
perror("accept");
}
}
continue;
}else if(events[n].events & EPOLLOUT){
printf("start to sendfile !\n");
int ret=0,left=stat_buf.st_size;
file_ptr=open(file_name,O_RDONLY);
while(left>0){
ret=sendfile(connfd,file_ptr,NULL,BUF);
if(ret<0 && errno==EAGAIN){
continue;
}else if(ret==0){
break;
}else{
left-=ret;
}
}
printf("sendfile over !\n");
close(file_ptr);
ev.data.fd=connfd;
epoll_ctl(epoll_fd,EPOLL_CTL_DEL,connfd,&ev);
cur_fd--;
close(connfd);
}else if(events[n].events & EPOLLIN){
char msg[100];
//.........这里部分代码省略.........
示例12: spamdscan_socket
static int
spamdscan_socket(const char *file, const struct spamd_server *srv, struct config_file *cfg, rspamd_result_t *res)
{
#ifdef HAVE_PATH_MAX
char buf[PATH_MAX + 10];
#elif defined(HAVE_MAXPATHLEN)
char buf[MAXPATHLEN + 10];
#else
#error "neither PATH_MAX nor MAXPATHEN defined"
#endif
char *c, *err;
struct sockaddr_un server_un;
struct sockaddr_in server_in;
int s, r, fd, ofl, size = 0;
struct stat sb;
struct rspamd_metric_result *cur = NULL;
struct rspamd_symbol *cur_symbol;
/* somebody doesn't need reply... */
if (!srv)
return 0;
if (srv->sock_type == AF_LOCAL) {
memset(&server_un, 0, sizeof(server_un));
server_un.sun_family = AF_UNIX;
strncpy(server_un.sun_path, srv->sock.unix_path, sizeof(server_un.sun_path));
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
msg_warn("spamd: socket %s, %s", srv->sock.unix_path,
strerror (errno));
return -1;
}
if (connect_t(s, (struct sockaddr *) & server_un, sizeof(server_un), cfg->spamd_connect_timeout) < 0) {
msg_warn("spamd: connect %s, %s", srv->sock.unix_path,
strerror (errno));
close(s);
return -1;
}
} else {
/* inet hostname, send stream over tcp/ip */
memset(&server_in, 0, sizeof(server_in));
server_in.sin_family = AF_INET;
server_in.sin_port = srv->sock.inet.port;
memcpy((char *)&server_in.sin_addr, &srv->sock.inet.addr, sizeof(struct in_addr));
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
msg_warn("spamd: socket %s", strerror (errno));
return -1;
}
if (connect_t(s, (struct sockaddr *) & server_in, sizeof(server_in), cfg->spamd_connect_timeout) < 0) {
msg_warn("spamd: connect %s, %s", srv->name, strerror (errno));
close(s);
return -1;
}
}
/* Get file size */
fd = open(file, O_RDONLY);
if (fstat (fd, &sb) == -1) {
msg_warn ("spamd: stat failed: %s", strerror (errno));
close(s);
return -1;
}
if (poll_fd(s, cfg->spamd_connect_timeout, POLLOUT) < 1) {
msg_warn ("spamd: timeout waiting writing, %s", srv->name);
close (s);
return -1;
}
/* Set blocking again */
ofl = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, ofl & (~O_NONBLOCK));
r = snprintf (buf, sizeof (buf), "SYMBOLS SPAMC/1.2\r\nContent-length: %ld\r\n\r\n", (long int)sb.st_size);
if (write (s, buf, r) == -1) {
msg_warn("spamd: write (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#ifdef HAVE_SENDFILE
#if defined(FREEBSD)
if (sendfile(fd, s, 0, 0, 0, 0, 0) != 0) {
msg_warn("spamd: sendfile (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#elif defined(LINUX)
off_t off = 0;
if (sendfile(s, fd, &off, sb.st_size) == -1) {
msg_warn("spamd: sendfile (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#endif
#else
//.........这里部分代码省略.........
示例13: rspamdscan_socket
//.........这里部分代码省略.........
}
if (priv->priv_user[0] != '\0') {
to_write = sizeof (buf) - r;
written = snprintf (buf + r, to_write, "User: %s\r\n", priv->priv_user);
if (written > to_write) {
msg_warn("rspamd: buffer overflow while filling buffer (%s)", srv->name);
close(fd);
close(s);
return -1;
}
r += written;
}
to_write = sizeof (buf) - r;
written = snprintf (buf + r, to_write, "Queue-ID: %s\r\n\r\n", priv->mlfi_id);
if (written > to_write) {
msg_warn("rspamd: buffer overflow while filling buffer (%s)", srv->name);
close(fd);
close(s);
return -1;
}
r += written;
if (write (s, buf, r) == -1) {
msg_warn("rspamd: write (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#ifdef HAVE_SENDFILE
#if defined(FREEBSD)
if (sendfile(fd, s, 0, 0, 0, 0, 0) != 0) {
msg_warn("rspamd: sendfile (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#elif defined(LINUX)
off_t off = 0;
if (sendfile(s, fd, &off, sb.st_size) == -1) {
msg_warn("rspamd: sendfile (%s), %s", srv->name, strerror (errno));
close(fd);
close(s);
return -1;
}
#endif
#else
while ((r = read (fd, buf, sizeof (buf))) > 0) {
write (s, buf, r);
}
#endif
fcntl(s, F_SETFL, ofl);
close(fd);
/* wait for reply */
if (poll_fd(s, cfg->spamd_results_timeout, POLLIN) < 1) {
msg_warn("rspamd: timeout waiting results %s", srv->name);
close(s);
return -1;
}
/*
示例14: file_read_checks
static void file_read_checks(const char *path)
{
char buf1[16],buf2[8],buf3[32];
struct iovec vec[3];
int rc, f, cnt;
off_t i;
mlog("+open");
f = open(path, O_RDONLY);
if (f < 0)
return;
// first look at the file's attributes
mlog("+fstat");
rc = fstat(f, &sbuf);
#ifdef CHECK_XATTR
mlog("+flistxattr");
flistxattr(f, lbuf, MAXLISTBUF);
mlog("+fgetxattr");
fgetxattr(f, "selinux", lbuf, MAXLISTBUF);
#endif
// readahead(f, NULL
// Check reading
mlog("+read");
cnt = 0;
while((read(f, buf3, sizeof(buf3)) > 0) && cnt < 1000)
cnt++;
// lseek around
mlog("+lseek");
if (rc == 0)
lseek(f, sbuf.st_size, SEEK_SET);
lseek(f, 100000, SEEK_SET);
lseek(f, 0, SEEK_SET);
// vectored reads
vec[0].iov_base = (void*)&buf1;
vec[0].iov_len = sizeof(buf1);
vec[1].iov_base = (void*)&buf2;
vec[1].iov_len = sizeof(buf2);
vec[2].iov_base = (void*)&buf3;
vec[2].iov_len = sizeof(buf3);
mlog("+readv");
cnt = 0;
while((readv(f, vec, 3) > 0) && cnt < 1000)
cnt++;
// check out pread syscall
i = 0;
mlog("+pread");
cnt = 0;
while ((pread(f, buf1, sizeof(buf1), i) > 0) && cnt < 1000) {
i += sizeof(buf1)*2;
cnt++;
}
// flock
mlog("+flock");
flock(f, LOCK_SH|LOCK_NB);
flock(f, LOCK_UN);
// fcntl ?
// sendfile to localhost:discard
#if defined(__linux__)
setup_socket();
if (sfd >= 0) {
mlog("+sendfile");
lseek(f, 0, SEEK_SET);
sendfile(sfd, f, NULL, rc ? 100000 : sbuf.st_size);
close(sfd);
sfd = -1;
}
#endif
// mmap each file
if (rc == 0) {
char *src;
mlog("+mmap");
src = mmap(0, sbuf.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
f, 0);
if (src != (char *)-1) {
// Don't touch memory...or Mr. SIGBUS will visit you
mlog("+munmap");
munmap(src, sbuf.st_size);
}
}
close(f);
}
示例15: do_retr
static void do_retr(session_t *sess)
{
if(get_transfer_fd(sess) == 0)
return;
int fd = open(sess->arg, O_RDONLY);
if(fd == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "1Failed to open file.");
return;
}
int ret;
ret = lock_file_read(fd);
if(ret == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
//device file can not be downloaded
struct stat sbuf;
ret = fstat(fd, &sbuf);
if(!S_ISREG(sbuf.st_mode))
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
long long offset = sess->restart_pos;
sess->restart_pos = 0;
if(offset != 0)
{
ret = lseek(fd, offset, SEEK_SET);
if(ret == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
}
char text[1024] = {0};
if(sess->is_ascii)
{
sprintf(text, "Opening ASCII mode data conection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
else
{
sprintf(text, "Opening BINARY mode data conection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
ftp_reply(sess, FTP_DATACONN, text);
int flag = 0;
long long bytes_to_send = sbuf.st_size;
if(offset > bytes_to_send)
bytes_to_send = 0;
else
bytes_to_send -= offset;
sess->bw_transfer_start_sec = get_time_sec();
sess->bw_transfer_start_usec = get_time_usec();
while(bytes_to_send)
{
int num_this_time = bytes_to_send > 65536 ? 65536 : bytes_to_send;
ret = sendfile(sess->data_fd, fd, NULL, num_this_time);
if(ret == -1)
{
flag = 2;
break;
}
limit_rate(sess, ret, 0);
if(sess->abor_received)
{
flag = 2;
break;
}
bytes_to_send -= ret;
}
close(sess->data_fd);
sess->data_fd = -1;
close(fd);
if(flag == 0)
ftp_reply(sess, FTP_TRANSFEROK, "Transfer complete.");
else if(flag == 1)
ftp_reply(sess, FTP_BADSENDFILE, "Failure reading from local file.");
else if(flag == 2)
ftp_reply(sess, FTP_BADSENDNET, "Failure writing to network stream.");
//.........这里部分代码省略.........