|
Network Block Device @PACKAGE_VERSION@
|
00001 #include <config.h> 00002 #include <stdio.h> 00003 #include <syslog.h> 00004 #include <unistd.h> 00005 #include <sys/types.h> 00006 #include <sys/socket.h> 00007 00008 #include <cliserv.h> 00009 #include <nbd-debug.h> 00010 00011 const u64 cliserv_magic = 0x00420281861253LL; 00012 const u64 opts_magic = 0x49484156454F5054LL; 00013 const u64 rep_magic = 0x3e889045565a9LL; 00014 00015 void setmysockopt(int sock) { 00016 int size = 1; 00017 #if 0 00018 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0) 00019 INFO("(no sockopt/1: %m)"); 00020 #endif 00021 #ifdef IPPROTO_TCP 00022 size = 1; 00023 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0) 00024 INFO("(no sockopt/2: %m)"); 00025 #endif 00026 #if 0 00027 size = 1024; 00028 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0) 00029 INFO("(no sockopt/3: %m)"); 00030 #endif 00031 } 00032 00033 void err_nonfatal(const char *s) { 00034 char s1[150], *s2; 00035 00036 strncpy(s1, s, sizeof(s1)); 00037 if ((s2 = strstr(s, "%m"))) { 00038 strcpy(s1 + (s2 - s), strerror(errno)); 00039 s2 += 2; 00040 strcpy(s1 + strlen(s1), s2); 00041 } 00042 #ifndef sun 00043 /* Solaris doesn't have %h in syslog */ 00044 else if ((s2 = strstr(s, "%h"))) { 00045 strcpy(s1 + (s2 - s), hstrerror(h_errno)); 00046 s2 += 2; 00047 strcpy(s1 + strlen(s1), s2); 00048 } 00049 #endif 00050 00051 s1[sizeof(s1)-1] = '\0'; 00052 #ifdef ISSERVER 00053 syslog(LOG_ERR, "%s", s1); 00054 syslog(LOG_ERR, "Exiting."); 00055 #endif 00056 fprintf(stderr, "Error: %s\nExiting.\n", s1); 00057 } 00058 00059 void err(const char *s) { 00060 err_nonfatal(s); 00061 exit(EXIT_FAILURE); 00062 } 00063 00064 void logging(const char* name) { 00065 #ifdef ISSERVER 00066 openlog(name, LOG_PID, LOG_DAEMON); 00067 #endif 00068 setvbuf(stdout, NULL, _IONBF, 0); 00069 setvbuf(stderr, NULL, _IONBF, 0); 00070 } 00071 00072 #ifndef ntohll 00073 #ifdef WORDS_BIGENDIAN 00074 uint64_t ntohll(uint64_t a) { 00075 return a; 00076 } 00077 #else 00078 uint64_t ntohll(uint64_t a) { 00079 u32 lo = a & 0xffffffff; 00080 u32 hi = a >> 32U; 00081 lo = ntohl(lo); 00082 hi = ntohl(hi); 00083 return ((uint64_t) lo) << 32U | hi; 00084 } 00085 #endif 00086 #endif 00087 00088 /** 00089 * Read data from a file descriptor into a buffer 00090 * 00091 * @param f a file descriptor 00092 * @param buf a buffer 00093 * @param len the number of bytes to be read 00094 **/ 00095 void readit(int f, void *buf, size_t len) { 00096 ssize_t res; 00097 while (len > 0) { 00098 DEBUG("*"); 00099 res = read(f, buf, len); 00100 if (res > 0) { 00101 len -= res; 00102 buf += res; 00103 } else if (res < 0) { 00104 if(errno != EAGAIN) { 00105 err("Read failed: %m"); 00106 } 00107 } else { 00108 err("Read failed: End of file"); 00109 } 00110 } 00111 }
1.7.3