idl: add to_null property
[Samba/gbeck.git] / examples / perfcounter / perf_writer_util.c
blob6f1eb16d3e12d4ded3c4da770be7a0011dbfbda4
1 /*
2 * Unix SMB/CIFS implementation.
3 * Performance Counter Daemon
5 * Copyright (C) Marcin Krzysztof Porwit 2005
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "perf.h"
23 extern sig_atomic_t keep_running;
25 void fatal(char *msg)
27 perror(msg);
28 exit(1);
31 void add_key_raw(TDB_CONTEXT *db, char *keystring, void *databuf, size_t datasize, int flags)
33 TDB_DATA key, data;
35 key.dptr = keystring;
36 key.dsize = strlen(keystring);
37 data.dptr = databuf;
38 data.dsize = datasize;
40 tdb_store(db, key, data, flags);
43 void add_key(TDB_CONTEXT *db, char *keystring, char *datastring, int flags)
45 TDB_DATA key, data;
47 key.dptr = keystring;
48 key.dsize = strlen(keystring);
49 data.dptr = datastring;
50 data.dsize = strlen(datastring);
52 tdb_store(db, key, data, flags);
55 void make_key(char *buf, int buflen, int key_part1, char *key_part2)
57 memset(buf, 0, buflen);
58 if(key_part2 != NULL)
59 sprintf(buf, "%d%s", key_part1, key_part2);
60 else
61 sprintf(buf, "%d", key_part1);
63 return;
66 void usage(char *progname)
68 fprintf(stderr, "Usage: %s [-d] [-f <file_path>].\n", progname);
69 fprintf(stderr, "\t-d: run as a daemon.\n");
70 fprintf(stderr, "\t-f <file_path>: path where the TDB files reside.\n");
71 fprintf(stderr, "\t\tDEFAULT is /var/lib/samba/perfmon\n");
72 exit(1);
75 void parse_flags(RuntimeSettings *rt, int argc, char **argv)
77 int flag;
79 while((flag = getopt(argc, argv, "df:")) != -1)
81 switch(flag)
83 case 'd':
85 rt->dflag = TRUE;
86 break;
88 case 'f':
90 memcpy(rt->dbDir, optarg, strlen(optarg));
91 break;
93 default:
95 usage(argv[0]);
100 return;
103 void setup_file_paths(RuntimeSettings *rt)
105 int status;
107 if(strlen(rt->dbDir) == 0)
109 /* No file path was passed in, use default */
110 sprintf(rt->dbDir, "/var/lib/samba/perfmon");
113 sprintf(rt->nameFile, "%s/names.tdb", rt->dbDir);
114 sprintf(rt->counterFile, "%s/data.tdb", rt->dbDir);
116 mkdir(rt->dbDir, 0755);
117 rt->cnames = tdb_open(rt->nameFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
118 rt->cdata = tdb_open(rt->counterFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
120 if(rt->cnames == NULL || rt->cdata == NULL)
122 perror("setup_file_paths");
123 exit(1);
126 return;
129 void sigterm_handler()
131 keep_running = FALSE;
132 return;
135 void daemonize(RuntimeSettings *rt)
137 pid_t pid;
138 int i;
139 int fd;
141 /* Check if we're already a daemon */
142 if(getppid() == 1)
143 return;
144 pid = fork();
145 if(pid < 0)
146 /* can't fork */
147 exit(1);
148 else if(pid > 0)
150 /* we're the parent */
151 tdb_close(rt->cnames);
152 tdb_close(rt->cdata);
153 exit(0);
156 /* get a new session */
157 if(setsid() == -1)
158 exit(2);
160 /* Change CWD */
161 chdir("/");
163 /* close file descriptors */
164 close(STDIN_FILENO);
165 close(STDOUT_FILENO);
166 close(STDERR_FILENO);
168 /* And reopen them as safe defaults */
169 fd = open("/dev/null", O_RDONLY);
170 if(fd != 0)
172 dup2(fd, 0);
173 close(fd);
175 fd = open("/dev/null", O_WRONLY);
176 if(fd != 1)
178 dup2(fd, 1);
179 close(fd);
181 fd = open("/dev/null", O_WRONLY);
182 if(fd != 2)
184 dup2(fd, 2);
185 close(fd);
188 /* handle signals */
189 signal(SIGINT, SIG_IGN);
190 signal(SIGHUP, SIG_IGN);
191 signal(SIGTERM, sigterm_handler);
193 return;
196 int get_counter_id(PERF_DATA_BLOCK *data)
198 data->counter_id += 2;
199 data->num_counters++;
201 return data->counter_id;
204 void init_perf_counter(PerfCounter *counter,
205 PerfCounter *parent,
206 unsigned int index,
207 char *name,
208 char *help,
209 int counter_type,
210 int record_type)
212 counter->index = index;
213 memcpy(counter->name, name, strlen(name));
214 memcpy(counter->help, help, strlen(help));
215 counter->counter_type = counter_type;
216 counter->record_type = record_type;
218 switch(record_type)
220 case PERF_OBJECT:
221 sprintf(counter->relationships, "p");
222 break;
223 case PERF_COUNTER:
224 sprintf(counter->relationships, "c[%d]", parent->index);
225 break;
226 case PERF_INSTANCE:
227 sprintf(counter->relationships, "i[%d]", parent->index);
228 break;
229 default:
230 perror("init_perf_counter: unknown record type");
231 exit(1);
234 return;