Add missing closing tag for para element
[nbd.git] / cliserv.c
blob4e57cbb76e862753106f1c396a3c8e7d8f2b94f7
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>
8 #include <cliserv.h>
9 #include <nbd-debug.h>
11 const u64 cliserv_magic = 0x00420281861253LL;
12 const u64 opts_magic = 0x49484156454F5054LL;
13 const u64 rep_magic = 0x3e889045565a9LL;
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
33 void err_nonfatal(const char *s) {
34 char s1[150], *s2;
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);
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);
49 #endif
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);
59 void err(const char *s) {
60 err_nonfatal(s);
61 exit(EXIT_FAILURE);
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);
72 #ifndef ntohll
73 #ifdef WORDS_BIGENDIAN
74 uint64_t ntohll(uint64_t a) {
75 return a;
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;
85 #endif
86 #endif
88 /**
89 * Read data from a file descriptor into a buffer
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");
107 } else {
108 err("Read failed: End of file");