2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/syscall.h>
33 #include <netinet/in.h>
35 #include <elliptics/packet.h>
36 #include <elliptics/interface.h>
40 #define DNET_CONF_COMMENT '#'
41 #define DNET_CONF_DELIM '='
42 #define DNET_CONF_TIME_DELIM '.'
44 int dnet_parse_groups(char *value
, int **groupsp
)
46 int len
= strlen(value
), i
, num
= 0, start
= 0, pos
= 0;
50 if (sscanf(value
, "auto%d", &num
) == 1) {
55 for (i
=0; i
<len
; ++i
) {
56 if (value
[i
] == DNET_CONF_ADDR_DELIM
)
65 fprintf(stderr
, "no groups found\n");
69 groups
= malloc(sizeof(int) * num
);
73 memset(groups
, 0, num
* sizeof(int));
76 for (i
=0; i
<len
; ++i
) {
77 if (value
[i
] == DNET_CONF_ADDR_DELIM
) {
80 groups
[pos
] = atoi(ptr
);
91 groups
[pos
] = atoi(ptr
);
99 int dnet_parse_numeric_id(char *value
, unsigned char *id
)
102 unsigned int i
, len
= strlen(value
);
104 memset(id
, 0, DNET_ID_SIZE
);
106 if (len
/2 > DNET_ID_SIZE
)
107 len
= DNET_ID_SIZE
* 2;
112 for (i
=0; i
<len
/ 2; i
++) {
113 ch
[2] = value
[2*i
+ 0];
114 ch
[3] = value
[2*i
+ 1];
116 id
[i
] = (unsigned char)strtol((const char *)ch
, NULL
, 16);
120 ch
[2] = value
[2*i
+ 0];
123 id
[i
] = (unsigned char)strtol((const char *)ch
, NULL
, 16);
129 void dnet_common_log(void *priv
, int level
, const char *msg
)
139 gettimeofday(&tv
, NULL
);
140 localtime_r((time_t *)&tv
.tv_sec
, &tm
);
141 strftime(str
, sizeof(str
), "%F %R:%S", &tm
);
143 fprintf(stream
, "%s.%06lu %ld/%4d %1d: %s", str
, tv
.tv_usec
, dnet_get_id(), getpid(), level
, msg
);
147 void dnet_syslog(void *priv
__attribute__ ((unused
)), int level
, const char *msg
)
149 int prio
= LOG_DEBUG
;
154 if (level
== DNET_LOG_ERROR
)
156 if (level
== DNET_LOG_INFO
)
159 gettimeofday(&tv
, NULL
);
160 localtime_r((time_t *)&tv
.tv_sec
, &tm
);
161 strftime(str
, sizeof(str
), "%F %R:%S", &tm
);
163 syslog(prio
, "%s.%06lu %ld/%4d %1x: %s", str
, tv
.tv_usec
, dnet_get_id(), getpid(), level
, msg
);
166 int dnet_common_add_remote_addr(struct dnet_node
*n
, struct dnet_config
*main_cfg
, char *orig_addr
)
171 struct dnet_config cfg
;
172 char auto_str
[] = "autodiscovery:";
173 int auto_len
= strlen(auto_str
);
178 a
= strdup(orig_addr
);
187 int autodescovery
= 0;
189 p
= strchr(addr
, ' ');
193 memcpy(&cfg
, main_cfg
, sizeof(struct dnet_config
));
195 if (!strncmp(addr
, auto_str
, auto_len
)) {
196 addr
[auto_len
- 1] = '\0';
201 err
= dnet_parse_addr(addr
, &cfg
);
203 dnet_log_raw(n
, DNET_LOG_ERROR
, "Failed to parse addr '%s': %d.\n", addr
, err
);
208 err
= dnet_discovery_add(n
, &cfg
);
212 err
= dnet_add_state(n
, &cfg
);
225 while (addr
&& *addr
&& isspace(*addr
))
233 dnet_log_raw(n
, DNET_LOG_ERROR
, "No remote addresses added. Continue to work though.\n");
243 int dnet_common_prepend_data(struct timespec
*ts
, uint64_t size
, void *buf
, int *bufsize
)
246 struct dnet_common_embed
*e
= buf
;
247 uint64_t *edata
= (uint64_t *)e
->data
;
249 if (*bufsize
< (int)(sizeof(struct dnet_common_embed
) + sizeof(uint64_t)) * 2)
252 e
->size
= sizeof(uint64_t) * 2;
253 e
->type
= DNET_FCGI_EMBED_TIMESTAMP
;
255 dnet_common_convert_embedded(e
);
257 edata
[0] = dnet_bswap64(ts
->tv_sec
);
258 edata
[1] = dnet_bswap64(ts
->tv_nsec
);
260 buf
+= sizeof(struct dnet_common_embed
) + sizeof(uint64_t) * 2;
264 e
->type
= DNET_FCGI_EMBED_DATA
;
266 dnet_common_convert_embedded(e
);
268 buf
+= sizeof(struct dnet_common_embed
);
270 *bufsize
= buf
- orig
;
274 #define dnet_map_log(n, level, fmt, a...) do { if ((n)) dnet_log_raw((n), level, fmt, ##a); else fprintf(stderr, fmt, ##a); } while (0)
276 int dnet_map_history(struct dnet_node
*n
, char *file
, struct dnet_history_map
*map
)
281 map
->fd
= open(file
, O_RDWR
| O_CLOEXEC
);
284 dnet_map_log(n
, DNET_LOG_ERROR
, "Failed to open history file '%s': %s [%d].\n",
285 file
, strerror(errno
), errno
);
289 err
= fstat(map
->fd
, &st
);
292 dnet_map_log(n
, DNET_LOG_ERROR
, "Failed to stat history file '%s': %s [%d].\n",
293 file
, strerror(errno
), errno
);
297 if (st
.st_size
% (int)sizeof(struct dnet_history_entry
)) {
298 dnet_map_log(n
, DNET_LOG_ERROR
, "Corrupted history file '%s', "
299 "its size %llu must be multiple of %zu.\n",
300 file
, (unsigned long long)st
.st_size
,
301 sizeof(struct dnet_history_entry
));
305 map
->size
= st
.st_size
;
307 map
->ent
= mmap(NULL
, map
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, map
->fd
, 0);
308 if (map
->ent
== MAP_FAILED
) {
310 dnet_map_log(n
, DNET_LOG_ERROR
, "Failed to mmap history file '%s': %s [%d].\n",
311 file
, strerror(errno
), errno
);
315 map
->num
= map
->size
/ sizeof(struct dnet_history_entry
);
317 dnet_map_log(n
, DNET_LOG_NOTICE
, "Mapped %ld entries in '%s'.\n", map
->num
, file
);
327 void dnet_unmap_history(struct dnet_node
*n
__attribute((unused
)), struct dnet_history_map
*map
)
329 munmap(map
->ent
, map
->size
);
333 struct dnet_meta
* dnet_meta_search_cust(struct dnet_meta_container
*mc
, uint32_t type
)
335 void *data
= mc
->data
;
336 uint32_t size
= mc
->size
;
337 struct dnet_meta m
, *found
= NULL
;
340 if (size
< sizeof(struct dnet_meta
)) {
341 fprintf(stderr
, "Metadata size %u is too small, min %zu, searching for 0x%x.\n",
342 size
, sizeof(struct dnet_meta
), type
);
346 m
= *(struct dnet_meta
*)data
;
347 dnet_convert_meta(&m
);
349 if (m
.size
+ sizeof(struct dnet_meta
) > size
) {
350 fprintf(stderr
, "Metadata entry broken: entry size %u, type: 0x%x, struct size: %zu, "
351 "total size left: %u, searching for 0x%x.\n",
352 m
.size
, m
.type
, sizeof(struct dnet_meta
), size
, type
);
356 if (m
.type
== type
) {
361 data
+= m
.size
+ sizeof(struct dnet_meta
);
362 size
-= m
.size
+ sizeof(struct dnet_meta
);
368 int dnet_background(void)
375 fprintf(stderr
, "Failed to fork to background: %s.\n", strerror(errno
));
380 printf("Daemon pid: %d.\n", pid
);
390 fd
= open("/dev/null", O_RDWR
);
393 fprintf(stderr
, "Can not open /dev/null: %d\n", fd
);