Network Block Device  @PACKAGE_VERSION@
cliserv.c
Go to the documentation of this file.
1 #include <config.h>
2 #include <cliserv.h>
3 #include <stdio.h>
4 #include <syslog.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 
8 const u64 cliserv_magic = 0x00420281861253LL;
9 const u64 opts_magic = 0x49484156454F5054LL;
10 const u64 rep_magic = 0x3e889045565a9LL;
11 
12 void setmysockopt(int sock) {
13  int size = 1;
14 #if 0
15  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
16  INFO("(no sockopt/1: %m)");
17 #endif
18 #ifdef IPPROTO_TCP
19  size = 1;
20  if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
21  INFO("(no sockopt/2: %m)");
22 #endif
23 #if 0
24  size = 1024;
25  if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
26  INFO("(no sockopt/3: %m)");
27 #endif
28 }
29 
30 void err_nonfatal(const char *s) {
31  char s1[150], *s2;
32 
33  strncpy(s1, s, sizeof(s1));
34  if ((s2 = strstr(s, "%m"))) {
35  strcpy(s1 + (s2 - s), strerror(errno));
36  s2 += 2;
37  strcpy(s1 + strlen(s1), s2);
38  }
39 #ifndef sun
40  /* Solaris doesn't have %h in syslog */
41  else if ((s2 = strstr(s, "%h"))) {
42  strcpy(s1 + (s2 - s), hstrerror(h_errno));
43  s2 += 2;
44  strcpy(s1 + strlen(s1), s2);
45  }
46 #endif
47 
48  s1[sizeof(s1)-1] = '\0';
49 #ifdef ISSERVER
50  syslog(LOG_ERR, "%s", s1);
51  syslog(LOG_ERR, "Exiting.");
52 #endif
53  fprintf(stderr, "Error: %s\nExiting.\n", s1);
54 }
55 
56 void err(const char *s) {
57  err_nonfatal(s);
58  exit(EXIT_FAILURE);
59 }
60 
61 void logging(const char* name) {
62 #ifdef ISSERVER
63  openlog(name, LOG_PID, LOG_DAEMON);
64 #endif
65  setvbuf(stdout, NULL, _IONBF, 0);
66  setvbuf(stderr, NULL, _IONBF, 0);
67 }
68 
69 #ifdef WORDS_BIGENDIAN
70 u64 ntohll(u64 a) {
71  return a;
72 }
73 #else
74 u64 ntohll(u64 a) {
75  u32 lo = a & 0xffffffff;
76  u32 hi = a >> 32U;
77  lo = ntohl(lo);
78  hi = ntohl(hi);
79  return ((u64) lo) << 32U | hi;
80 }
81 #endif
u64 ntohll(u64 a)
Definition: cliserv.c:74
void err(const char *s)
Definition: cliserv.c:56
void setmysockopt(int sock)
Definition: cliserv.c:12
void err_nonfatal(const char *s)
Definition: cliserv.c:30
const u64 cliserv_magic
Definition: cliserv.c:8
#define INFO(a)
Definition: cliserv.h:73
const u64 rep_magic
Definition: cliserv.c:10
const u64 opts_magic
Definition: cliserv.c:9
void logging(const char *name)
Definition: cliserv.c:61