]> arthur.barton.de Git - netdata.git/blob - src/common.c
6747382070cb2258ba24910efcaaff3326661ada
[netdata.git] / src / common.c
1 // enable O_NOATIME
2 #define _GNU_SOURCE
3
4 #include <string.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/mman.h>
12 #include <malloc.h>
13
14
15 #include "log.h"
16 #include "common.h"
17 #include "web_client.h"
18
19 // http://stackoverflow.com/questions/7666509/hash-function-for-string
20 unsigned long simple_hash(const char *name)
21 {
22         const char *s = name;
23         unsigned long hash = 5381;
24         int i;
25
26         while((i = *s++)) hash = ((hash << 5) + hash) + i;
27
28         // fprintf(stderr, "HASH: %lu %s\n", hash, name);
29
30         return hash;
31 }
32
33 void strreverse(char* begin, char* end)
34 {
35     char aux;
36     while (end > begin)
37         aux = *end, *end-- = *begin, *begin++ = aux;
38 }
39
40 char *mystrsep(char **ptr, char *s)
41 {
42         char *p = "";
43         while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
44         return(p);
45 }
46
47 // like strsep() but:
48 // it trims spaces before and after each value
49 // it accepts quoted values in single or double quotes
50 char *qstrsep(char **ptr)
51 {
52         if(!*ptr || !**ptr) return NULL;
53         
54         char *s, *p = *ptr;
55
56         // skip leading spaces
57         while(isspace(*p)) p++;
58
59         // if the first char is a quote, assume quoted
60         if(*p == '"' || *p == '\'') {
61                 char q = *p;
62                 s = ++p;
63                 while(*p && *p != q) p++;
64
65                 if(*p == q) {
66                         *p = '\0';
67                         p++;
68                 }
69
70                 *ptr = p;
71                 return s;
72         }
73
74         s = p;
75         while(*p && !isspace(*p)) p++;
76         if(!*p) *ptr = NULL;
77         else {
78                 *p = '\0';
79                 *ptr = ++p;
80         }
81
82         return s;
83 }
84
85 char *trim(char *s)
86 {
87         // skip leading spaces
88         while(*s && isspace(*s)) s++;
89         if(!*s || *s == '#') return NULL;
90
91         // skip tailing spaces
92         int c = strlen(s) - 1;
93         while(c >= 0 && isspace(s[c])) {
94                 s[c] = '\0';
95                 c--;
96         }
97         if(c < 0) return NULL;
98         if(!*s) return NULL;
99         return s;
100 }
101
102 void *mymmap(const char *filename, unsigned long size, int flags)
103 {
104         int fd;
105         void *mem = NULL;
106
107         errno = 0;
108         fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
109         if(fd != -1) {
110                 if(lseek(fd, size, SEEK_SET) == (long)size) {
111                         if(write(fd, "", 1) == 1) {
112
113                                 if(ftruncate(fd, size))
114                                         error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
115
116                                 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
117                                 if(mem) {
118                                         if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
119                                                 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
120                                 }
121                         }
122                         else error("Cannot write to file '%s' at position %ld.", filename, size);
123                 }
124                 else error("Cannot seek file '%s' to size %ld.", filename, size);
125
126                 close(fd);
127         }
128         else error("Cannot create/open file '%s'.", filename);
129
130         return mem;
131 }
132
133 int savememory(const char *filename, void *mem, unsigned long size)
134 {
135         char tmpfilename[FILENAME_MAX + 1];
136
137         snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
138
139         int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
140         if(fd < 0) {
141                 error("Cannot create/open file '%s'.", filename);
142                 return -1;
143         }
144
145         if(write(fd, mem, size) != (long)size) {
146                 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
147                 close(fd);
148                 return -1;
149         }
150
151         close(fd);
152
153         int ret = 0;
154         if(rename(tmpfilename, filename)) {
155                 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
156                 ret = -1;
157         }
158
159         return ret;
160 }
161
162 void log_allocations(void)
163 {
164         static int mem = 0;
165
166         struct mallinfo mi;
167
168         mi = mallinfo();
169         if(mi.uordblks > mem) {
170                 int clients = 0;
171                 struct web_client *w;
172                 for(w = web_clients; w ; w = w->next) clients++;
173
174                 info("Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients);
175                 mem = mi.uordblks;
176         }
177 }
178
179 int fd_is_valid(int fd) {
180     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
181 }
182