Network Block Device  @PACKAGE_VERSION@
cliserv.c
Go to the documentation of this file.
1 #include <config.h>
2 #include <stdio.h>
3 #include <syslog.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 
8 #include <cliserv.h>
9 #include <nbd-debug.h>
10 
11 const u64 cliserv_magic = 0x00420281861253LL;
12 const u64 opts_magic = 0x49484156454F5054LL;
13 const u64 rep_magic = 0x3e889045565a9LL;
14 
15 void setmysockopt(int sock) {
16  int size = 1;
17 #if 0
18  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
19  INFO("(no sockopt/1: %m)");
20 #endif
21 #ifdef IPPROTO_TCP
22  size = 1;
23  if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
24  INFO("(no sockopt/2: %m)");
25 #endif
26 #if 0
27  size = 1024;
28  if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
29  INFO("(no sockopt/3: %m)");
30 #endif
31 }
32 
33 void err_nonfatal(const char *s) {
34  char s1[150], *s2;
35 
36  strncpy(s1, s, sizeof(s1));
37  if ((s2 = strstr(s, "%m"))) {
38  strcpy(s1 + (s2 - s), strerror(errno));
39  s2 += 2;
40  strcpy(s1 + strlen(s1), s2);
41  }
42 #ifndef sun
43  /* Solaris doesn't have %h in syslog */
44  else if ((s2 = strstr(s, "%h"))) {
45  strcpy(s1 + (s2 - s), hstrerror(h_errno));
46  s2 += 2;
47  strcpy(s1 + strlen(s1), s2);
48  }
49 #endif
50 
51  s1[sizeof(s1)-1] = '\0';
52 #ifdef ISSERVER
53  syslog(LOG_ERR, "%s", s1);
54  syslog(LOG_ERR, "Exiting.");
55 #endif
56  fprintf(stderr, "Error: %s\nExiting.\n", s1);
57 }
58 
59 void err(const char *s) {
60  err_nonfatal(s);
61  exit(EXIT_FAILURE);
62 }
63 
64 void logging(const char* name) {
65 #ifdef ISSERVER
66  openlog(name, LOG_PID, LOG_DAEMON);
67 #endif
68  setvbuf(stdout, NULL, _IONBF, 0);
69  setvbuf(stderr, NULL, _IONBF, 0);
70 }
71 
72 #ifndef ntohll
73 #ifdef WORDS_BIGENDIAN
74 uint64_t ntohll(uint64_t a) {
75  return a;
76 }
77 #else
78 uint64_t ntohll(uint64_t a) {
79  u32 lo = a & 0xffffffff;
80  u32 hi = a >> 32U;
81  lo = ntohl(lo);
82  hi = ntohl(hi);
83  return ((uint64_t) lo) << 32U | hi;
84 }
85 #endif
86 #endif
87 
88 /**
89  * Read data from a file descriptor into a buffer
90  *
91  * @param f a file descriptor
92  * @param buf a buffer
93  * @param len the number of bytes to be read
94  **/
95 void readit(int f, void *buf, size_t len) {
96  ssize_t res;
97  while (len > 0) {
98  DEBUG("*");
99  res = read(f, buf, len);
100  if (res > 0) {
101  len -= res;
102  buf += res;
103  } else if (res < 0) {
104  if(errno != EAGAIN) {
105  err("Read failed: %m");
106  }
107  } else {
108  err("Read failed: End of file");
109  }
110  }
111 }
void err(const char *s)
Definition: cliserv.c:59
uint32_t len
Definition: nbd.h:41
void setmysockopt(int sock)
Definition: cliserv.c:15
void err_nonfatal(const char *s)
Definition: cliserv.c:33
const u64 cliserv_magic
Definition: cliserv.c:11
#define INFO(a)
Definition: cliserv.h:73
const u64 rep_magic
Definition: cliserv.c:13
void readit(int f, void *buf, size_t len)
Read data from a file descriptor into a buffer.
Definition: cliserv.c:95
const u64 opts_magic
Definition: cliserv.c:12
uint64_t ntohll(uint64_t a)
Definition: cliserv.c:78
#define DEBUG(...)
Definition: nbd-debug.h:8
void logging(const char *name)
Definition: cliserv.c:64