3 * Copyright (C) 2002 Florin Malita <mali@go.ro>
5 * This file is part of LUFS, a free userspace filesystem implementation.
6 * See http://lufs.sourceforge.net/ for updates.
8 * LUFS is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * LUFS is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <lufs/proto.h>
37 lu_check_to(int rd_fd, int wr_fd, int time_out){
47 maxfd = rd_fd > maxfd ? rd_fd : maxfd;
52 maxfd = wr_fd > maxfd ? wr_fd : maxfd;
59 res = select(maxfd + 1, &rd, &wr, NULL, &tv);
61 }while((res < 0) && (errno == EINTR));
67 WARN("select call failed: %s", strerror(errno));
71 WARN("operation timed out!");
77 lu_atomic_read(int fd, char *buf, int len, int time_out){
81 if((time_out) && ((res = lu_check_to(fd, 0, time_out)) < 0))
85 res = read(fd, buf + offset, len - offset);
86 }while((res < 0) && (errno == EINTR));
89 WARN("read call failed: %s", strerror(errno));
90 return (res < 0) ? -errno : (offset > 0 ? offset : -EPIPE);
101 lu_atomic_write(int fd, char *buf, int len, int time_out){
105 if((time_out) && ((res = lu_check_to(0, fd, time_out)) < 0))
109 res = write(fd, buf + offset, len - offset);
110 }while((res < 0) && (errno == EINTR));
113 WARN("write call failed: %s", strerror(errno));
114 return (res < 0) ? -errno : (offset > 0 ? offset : -EPIPE);
119 }while(offset < len);
125 lu_recvmsg(struct message *msg, int sock){
127 msg->m_data = msg->m_buffer;
129 if(lu_atomic_read(sock, (char*)&msg->m_hdr, sizeof(struct lu_msg), 0) < 0){
134 if(msg->m_hdr.msg_datalen){
135 if(msg->m_hdr.msg_datalen > MAX_DATA){
136 ERROR("message too long: %u bytes", msg->m_hdr.msg_datalen);
140 if(lu_atomic_read(sock, msg->m_data, msg->m_hdr.msg_datalen, 0) < 0){
150 lu_sendmsg(struct message *msg, int sock){
152 if(lu_atomic_write(sock, (char*)&msg->m_hdr, sizeof(struct lu_msg), 0) < 0){
157 if((msg->m_hdr.msg_datalen) && (lu_atomic_write(sock, msg->m_data, msg->m_hdr.msg_datalen, 0) < 0)){
166 lu_makemsg(struct message *msg, unsigned short type, char *data, unsigned short len){
168 msg->m_hdr.msg_type = type;
169 msg->m_hdr.msg_version = PVERSION;
170 msg->m_hdr.msg_datalen = len;
171 msg->m_hdr.msg_pid = getpid();
176 msg->m_data = msg->m_buffer;