2 * tclrrd.c -- A TCL interpreter extension to access the RRD library.
4 * Copyright (c) 1999,2000 Frank Strauss, Technical University of Braunschweig.
6 * See the file "COPYING" for information on usage and redistribution
7 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
20 #include <rrd_format.h>
22 extern int Tclrrd_Init(Tcl_Interp *interp);
23 extern int Tclrrd_SafeInit(Tcl_Interp *interp);
27 * some rrd_XXX() functions might modify the argv strings passed to it.
28 * Hence, we need to do some preparation before
29 * calling the rrd library functions.
31 static char ** getopt_init(int argc, CONST84 char *argv[])
36 argv2 = calloc(argc, sizeof(char *));
37 for (i = 0; i < argc; i++) {
38 argv2[i] = strdup(argv[i]);
43 static void getopt_cleanup(int argc, char **argv2)
47 for (i = 0; i < argc; i++) {
56 Rrd_Create(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
60 argv2 = getopt_init(argc, argv);
61 rrd_create(argc, argv2);
62 getopt_cleanup(argc, argv2);
64 if (rrd_test_error()) {
65 Tcl_AppendResult(interp, "RRD Error: ",
66 rrd_get_error(), (char *) NULL);
77 Rrd_Dump(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
81 argv2 = getopt_init(argc, argv);
82 rrd_dump(argc, argv2);
83 getopt_cleanup(argc, argv2);
85 /* NOTE: rrd_dump() writes to stdout. No interaction with TCL. */
87 if (rrd_test_error()) {
88 Tcl_AppendResult(interp, "RRD Error: ",
89 rrd_get_error(), (char *) NULL);
100 Rrd_Last(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
105 argv2 = getopt_init(argc, argv);
106 t = rrd_last(argc, argv2);
107 getopt_cleanup(argc, argv2);
110 if (rrd_test_error()) {
111 Tcl_AppendResult(interp, "RRD Error: ",
112 rrd_get_error(), (char *) NULL);
117 Tcl_SetIntObj(Tcl_GetObjResult(interp), t);
125 Rrd_Update(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
129 argv2 = getopt_init(argc, argv);
130 rrd_update(argc, argv2);
131 getopt_cleanup(argc, argv2);
133 if (rrd_test_error()) {
134 Tcl_AppendResult(interp, "RRD Error: ",
135 rrd_get_error(), (char *) NULL);
146 Rrd_Fetch(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
148 time_t start, end, j;
149 unsigned long step, ds_cnt, i, ii;
150 rrd_value_t *data, *datai;
156 argv2 = getopt_init(argc, argv);
157 if (rrd_fetch(argc, argv2, &start, &end, &step,
158 &ds_cnt, &ds_namv, &data) != -1) {
160 listPtr = Tcl_GetObjResult(interp);
161 for (j = start; j <= end; j += step) {
162 for (ii = 0; ii < ds_cnt; ii++) {
163 sprintf(s, "%.2f", *(datai++));
164 Tcl_ListObjAppendElement(interp, listPtr,
165 Tcl_NewStringObj(s, -1));
168 for (i=0; i<ds_cnt; i++) free(ds_namv[i]);
172 getopt_cleanup(argc, argv2);
174 if (rrd_test_error()) {
175 Tcl_AppendResult(interp, "RRD Error: ",
176 rrd_get_error(), (char *) NULL);
187 Rrd_Graph(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
193 char **calcpr = NULL;
194 int rc, xsize, ysize;
201 * If the "filename" is a Tcl fileID, then arrange for rrd_graph() to write to
202 * that file descriptor. Will this work with windoze? I have no idea.
204 if ((channel = Tcl_GetChannel(interp, argv[1], &mode)) != NULL) {
206 * It >is< a Tcl fileID
208 if (!(mode & TCL_WRITABLE)) {
209 Tcl_AppendResult(interp, "channel \"", argv[1],
210 "\" wasn't opened for writing", (char *) NULL);
214 * Must flush channel to make sure any buffered data is written before
215 * rrd_graph() writes to the stream
217 if (Tcl_Flush(channel) != TCL_OK) {
218 Tcl_AppendResult(interp, "flush failed for \"", argv[1], "\": ",
219 strerror(Tcl_GetErrno()), (char *) NULL);
222 if (Tcl_GetChannelHandle(channel, TCL_WRITABLE, &fd1) != TCL_OK) {
223 Tcl_AppendResult(interp, "cannot get file descriptor associated with \"",
224 argv[1], "\"", (char *) NULL);
228 * Must dup() file descriptor so we can fclose(stream), otherwise the fclose()
229 * would close Tcl's file descriptor
231 if ((fd2 = dup((int)fd1)) == -1) {
232 Tcl_AppendResult(interp, "dup() failed for file descriptor associated with \"",
233 argv[1], "\": ", strerror(errno), (char *) NULL);
237 * rrd_graph() wants a FILE*
239 if ((stream = fdopen(fd2, "wb")) == NULL) {
240 Tcl_AppendResult(interp, "fdopen() failed for file descriptor associated with \"",
241 argv[1], "\": ", strerror(errno), (char *) NULL);
242 close(fd2); /* plug potential file descriptor leak */
248 argv2 = getopt_init(argc, argv);
251 Tcl_ResetResult(interp); /* clear error from Tcl_GetChannel() */
252 argv2 = getopt_init(argc, argv);
255 rc = rrd_graph(argc, argv2, &calcpr, &xsize, &ysize, stream, &ymin, &ymax);
256 getopt_cleanup(argc, argv2);
259 fclose(stream); /* plug potential malloc & file descriptor leak */
262 sprintf(dimensions, "%d %d", xsize, ysize);
263 Tcl_AppendResult(interp, dimensions, (char *) NULL);
268 for(i = 0; calcpr[i]; i++){
269 printf("%s\n", calcpr[i]);
277 if (rrd_test_error()) {
278 Tcl_AppendResult(interp, "RRD Error: ",
279 rrd_get_error(), (char *) NULL);
290 Rrd_Tune(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
294 argv2 = getopt_init(argc, argv);
295 rrd_tune(argc, argv2);
296 getopt_cleanup(argc, argv2);
298 if (rrd_test_error()) {
299 Tcl_AppendResult(interp, "RRD Error: ",
300 rrd_get_error(), (char *) NULL);
311 Rrd_Resize(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
315 argv2 = getopt_init(argc, argv);
316 rrd_resize(argc, argv2);
317 getopt_cleanup(argc, argv2);
319 if (rrd_test_error()) {
320 Tcl_AppendResult(interp, "RRD Error: ",
321 rrd_get_error(), (char *) NULL);
332 Rrd_Restore(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
336 argv2 = getopt_init(argc, argv);
337 rrd_restore(argc, argv2);
338 getopt_cleanup(argc, argv2);
340 if (rrd_test_error()) {
341 Tcl_AppendResult(interp, "RRD Error: ",
342 rrd_get_error(), (char *) NULL);
353 * The following structure defines the commands in the Rrd extension.
357 char *name; /* Name of the command. */
358 Tcl_CmdProc *proc; /* Procedure for command. */
359 int hide; /* Hide if safe interpreter */
362 static CmdInfo rrdCmds[] = {
363 { "Rrd::create", Rrd_Create, 1 },
364 { "Rrd::dump", Rrd_Dump, 0 },
365 { "Rrd::last", Rrd_Last, 0 },
366 { "Rrd::update", Rrd_Update, 1 },
367 { "Rrd::fetch", Rrd_Fetch, 0 },
368 { "Rrd::graph", Rrd_Graph, 1 }, /* Due to RRD's API, a safe
369 interpreter cannot create
370 a graph since it writes to
371 a filename supplied by the
373 { "Rrd::tune", Rrd_Tune, 1 },
374 { "Rrd::resize", Rrd_Resize, 1 },
375 { "Rrd::restore", Rrd_Restore, 1 },
376 { (char *) NULL, (Tcl_CmdProc *) NULL, 0 }
382 init(Tcl_Interp *interp, int safe)
387 if ( Tcl_InitStubs(interp,TCL_VERSION,0) == NULL )
390 if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 1) == NULL) {
395 * Why a global array? In keeping with the Rrd:: namespace, why
396 * not simply create a normal variable Rrd::version and set it?
398 Tcl_SetVar2(interp, "rrd", "version", VERSION, TCL_GLOBAL_ONLY);
400 for (cmdInfoPtr = rrdCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
402 * Check if the command already exists and return an error
403 * to ensure we detect name clashes while loading the Rrd
406 if (Tcl_GetCommandInfo(interp, cmdInfoPtr->name, &info)) {
407 Tcl_AppendResult(interp, "command \"", cmdInfoPtr->name,
408 "\" already exists", (char *) NULL);
411 if (safe && cmdInfoPtr->hide) {
414 * Turns out the one cannot hide a command in a namespace
415 * due to a limitation of Tcl, one can only hide global
416 * commands. Thus, if we created the commands without
417 * the Rrd:: namespace in a safe interpreter, then the
418 * "unsafe" commands could be hidden -- which would allow
419 * an owning interpreter either un-hiding them or doing
420 * an "interp invokehidden". If the Rrd:: namespace is
421 * used, then it's still possible for the owning interpreter
422 * to fake out the missing commands:
424 * # Make all Rrd::* commands available in master interperter
425 * package require Rrd
426 * set safe [interp create -safe]
427 * # Make safe Rrd::* commands available in safe interperter
428 * interp invokehidden $safe -global load ./tclrrd1.2.11.so
429 * # Provide the safe interpreter with the missing commands
430 * $safe alias Rrd::update do_update $safe
431 * proc do_update {which_interp $args} {
432 * # Do some checking maybe...
434 * return [eval Rrd::update $args]
437 * Our solution for now is to just not create the "unsafe"
438 * commands in a safe interpreter.
440 if (Tcl_HideCommand(interp, cmdInfoPtr->name, cmdInfoPtr->name) != TCL_OK)
445 Tcl_CreateCommand(interp, cmdInfoPtr->name, cmdInfoPtr->proc,
446 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
449 if (Tcl_PkgProvide(interp, "Rrd", VERSION) != TCL_OK) {
457 Tclrrd_Init(Tcl_Interp *interp)
459 return init(interp, 0);
463 * See the comments above and note how few commands are considered "safe"...
464 * Using rrdtool in a safe interpreter has very limited functionality. It's
465 * tempting to just return TCL_ERROR and forget about it.
468 Tclrrd_SafeInit(Tcl_Interp *interp)
470 return init(interp, 1);