X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_tool.c;h=037894085141c1a3c54d5cc18e19c28061f25274;hb=4223ec6dd69776ac5bb8c7fc945191e589bdcf39;hp=9a1f795dd08753bc36d6f7a9f3eeaf4ce94f90a1;hpb=23be36896d8cf89dd9dbcdb2bb62677ca01810f8;p=rrdtool.git diff --git a/src/rrd_tool.c b/src/rrd_tool.c index 9a1f795..0378940 100644 --- a/src/rrd_tool.c +++ b/src/rrd_tool.c @@ -11,6 +11,8 @@ void PrintUsage(char *cmd); int CountArgs(char *aLine); int CreateArgs(char *, char *, int, char **); int HandleInputLine(int, char **, FILE*); +int RemoteMode=0; +int ChangeRoot=0; #define TRUE 1 #define FALSE 0 #define MAX_LENGTH 10000 @@ -20,13 +22,18 @@ void PrintUsage(char *cmd) { char help_main[] = - "RRDtool 1.1.x Copyright 1997-2001 by Tobias Oetiker \n\n" + "RRDtool 1.1.x Copyright 1997-2003 by Tobias Oetiker \n" + " Compiled " MAKE_TIMESTAMP "\n\n" "Usage: rrdtool [options] command command_options\n\n"; char help_list[] = "Valid commands: create, update, graph, dump, restore,\n" "\t\tlast, info, fetch, tune, resize, xport\n\n"; + char help_listremote[] = + "Valid remote commands: quit, ls, cd, mkdir\n\n"; + + char help_create[] = "* create - create a new RRD\n\n" "\trrdtool create filename [--start|-b start time]\n" @@ -123,6 +130,22 @@ void PrintUsage(char *cmd) "\t\t[CDEF:vname=rpn-expression]\n" "\t\t[XPORT:vname:legend]\n\n"; + char help_quit[] = + " * quit - closeing a session in remote mode\n\n" + "\trrdtool quit\n\n"; + + char help_ls[] = + " * ls - lists all *.rrd files in current directory\n\n" + "\trrdtool ls\n\n"; + + char help_cd[] = + " * cd - changes the current directory\n\n" + "\trrdtool cd new direcotry\n\n"; + + char help_mkdir[] = + " * mkdir - creates a new direcotry\n\n" + "\trrdtool mkdir newdirecotryname\n\n"; + char help_lic[] = "RRDtool is distributed under the Terms of the GNU General\n" "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n" @@ -130,7 +153,8 @@ void PrintUsage(char *cmd) "For more information read the RRD manpages\n\n"; enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST, - C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE, C_XPORT }; + C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE, C_XPORT, + C_QUIT, C_LS, C_CD, C_MKDIR }; int help_cmd = C_NONE; @@ -158,12 +182,23 @@ void PrintUsage(char *cmd) help_cmd = C_RESIZE; else if (!strcmp(cmd,"xport")) help_cmd = C_XPORT; + else if (!strcmp(cmd,"quit")) + help_cmd = C_QUIT; + else if (!strcmp(cmd,"ls")) + help_cmd = C_LS; + else if (!strcmp(cmd,"cd")) + help_cmd = C_CD; + else if (!strcmp(cmd,"mkdir")) + help_cmd = C_MKDIR; } fputs(help_main, stdout); switch (help_cmd) { case C_NONE: fputs(help_list, stdout); + if (RemoteMode){ + fputs(help_listremote, stdout); + } break; case C_CREATE: fputs(help_create, stdout); @@ -198,6 +233,18 @@ void PrintUsage(char *cmd) case C_XPORT: fputs(help_xport, stdout); break; + case C_QUIT: + fputs(help_quit, stdout); + break; + case C_LS: + fputs(help_ls, stdout); + break; + case C_CD: + fputs(help_cd, stdout); + break; + case C_MKDIR: + fputs(help_mkdir, stdout); + break; } fputs(help_lic, stdout); } @@ -207,6 +254,9 @@ int main(int argc, char *argv[]) { char **myargv; char aLine[MAX_LENGTH]; +#ifdef HAVE_CHROOT + char *firstdir=""; +#endif #ifdef MUST_DISABLE_SIGFPE signal(SIGFPE,SIG_IGN); #endif @@ -219,7 +269,7 @@ int main(int argc, char *argv[]) return 0; } - if ((argc == 2) && !strcmp("-",argv[1])) + if (((argc == 2)||(argc == 3)) && !strcmp("-",argv[1])) { #if HAVE_GETRUSAGE struct rusage myusage; @@ -231,6 +281,35 @@ int main(int argc, char *argv[]) tz.tz_dsttime=0; gettimeofday(&starttime,&tz); #endif + RemoteMode=1; +#ifdef HAVE_CHROOT + if ((argc == 3) && strcmp("",argv[2])){ + if (getuid()==0){ + chroot(argv[2]); + if (errno!=0){ + fprintf(stderr,"ERROR: can't change root to '%s' errno=%d\n", + argv[2],errno); + exit(errno); + } + ChangeRoot=1; + firstdir="/"; + } + else{ + firstdir=argv[2]; + } + } + if (strcmp(firstdir,"")){ + chdir(firstdir); + if (errno!=0){ + fprintf(stderr,"ERROR: %s\n",strerror(errno)); + exit(errno); + } + } +#else + fprintf(stderr,"ERROR: change root is not supported by your OS " + "or at least by this copy of rrdtool\n"); + exit(1); +#endif while (fgets(aLine, sizeof(aLine)-1, stdin)){ if ((argc = CountArgs(aLine)) == 0) { @@ -272,16 +351,104 @@ int main(int argc, char *argv[]) PrintUsage(argv[1]); exit(0); } + else if (argc == 3 && !strcmp(argv[1],"help")) + { + PrintUsage(argv[2]); + exit(0); + } else HandleInputLine(argc, argv, stderr); return 0; } +/* HandleInputLine is NOT thread safe - due to readdir issues, + resolving them portably is not really simple. */ int HandleInputLine(int argc, char **argv, FILE* out) { +#if defined(HAVE_OPENDIR) && defined (HAVE_READDIR) + DIR *curdir; /* to read current dir with ls */ + struct dirent *dent; +#endif +#if defined(HAVE_SYS_STAT_H) + struct stat st; +#endif optind=0; /* reset gnu getopt */ opterr=0; /* no error messages */ + if (RemoteMode){ + if (argc>1 && strcmp("quit", argv[1]) == 0){ + if (argc>2){ + printf("ERROR: invalid parameter count for quit\n"); + return(0); + } + exit(0); + } +#if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR) + if (argc>1 && strcmp("cd", argv[1]) == 0){ + if (argc>3){ + printf("ERROR: invalid parameter count for cd\n"); + return(0); + } +#if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) + if (getuid()==0 && ! ChangeRoot){ + printf("ERROR: chdir security problem - rrdtool is runnig as " + "root an no chroot!\n"); + return(0); + } +#endif + chdir(argv[2]); + if (errno!=0){ + printf("ERROR: %s\n",strerror(errno)); + } + return(0); + } + if (argc>1 && strcmp("mkdir", argv[1]) == 0){ + if (argc>3){ + printf("ERROR: invalid parameter count for mkdir\n"); + return(0); + } +#if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID) + if (getuid()==0 && ! ChangeRoot){ + printf("ERROR: mkdir security problem - rrdtool is runnig as " + "root an no chroot!\n"); + return(0); + } +#endif + mkdir(argv[2],0777); + if (errno!=0){ + printf("ERROR: %s\n",strerror(errno)); + } + return(0); + } + if (argc>1 && strcmp("ls", argv[1]) == 0){ + if (argc>2){ + printf("ERROR: invalid parameter count for ls\n"); + return(0); + } + if ((curdir=opendir("."))!=NULL){ + while((dent=readdir(curdir))!=NULL){ + if (!stat(dent->d_name,&st)){ + if (S_ISDIR(st.st_mode)){ + printf("d %s\n",dent->d_name); + } + if (strlen(dent->d_name)>4 && S_ISREG(st.st_mode)){ + if (!strcmp(dent->d_name+NAMLEN(dent)-4,".rrd") || + !strcmp(dent->d_name+NAMLEN(dent)-4,".RRD")){ + printf("- %s\n",dent->d_name); + } + } + } + } + } + else{ + printf("ERROR: %s\n",strerror(errno)); + return(errno); + } + return(0); + } +#endif /* opendir and readdir */ + + } if (argc < 3 || strcmp("help", argv[1]) == 0 || strcmp("--help", argv[1]) == 0 @@ -331,7 +498,7 @@ int HandleInputLine(int argc, char **argv, FILE* out) strcmp("v", argv[1]) == 0 || strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0 ) - printf("RRDtool 1.1.x Copyright (C) 1997-2001 by Tobias Oetiker \n"); + printf("RRDtool 1.1.x Copyright (C) 1997-2003 by Tobias Oetiker \n"); else if (strcmp("restore", argv[1]) == 0) rrd_restore(argc-1, &argv[1]); else if (strcmp("resize", argv[1]) == 0) @@ -341,7 +508,7 @@ int HandleInputLine(int argc, char **argv, FILE* out) else if (strcmp("update", argv[1]) == 0) rrd_update(argc-1, &argv[1]); else if (strcmp("fetch", argv[1]) == 0) { - time_t start,end; + time_t start,end, ti; unsigned long step, ds_cnt,i,ii; rrd_value_t *data,*datai; char **ds_namv; @@ -351,8 +518,8 @@ int HandleInputLine(int argc, char **argv, FILE* out) for (i = 0; i\n", LEGEND_TAG); printf(" \n", META_TAG); printf(" <%s>\n", DATA_TAG); - for (i = start+step; i <= end; i += step) { + for (ti = start+step; ti <= end; ti += step) { printf (" <%s>", DATA_ROW_TAG); - printf ("<%s>%lu", COL_TIME_TAG, i, COL_TIME_TAG); + printf ("<%s>%lu", COL_TIME_TAG, ti, COL_TIME_TAG); for (j = 0; j < col_cnt; j++) { rrd_value_t newval = DNAN; newval = *ptr; @@ -416,12 +583,13 @@ int HandleInputLine(int argc, char **argv, FILE* out) const char *imgfile = argv[2]; /* rrd_graph changes argv pointer */ int xsize, ysize; int i; + int tostdout = (strcmp(argv[2],"-") == 0); if( rrd_graph(argc-1, &argv[1], &calcpr, &xsize, &ysize) != -1 ) { - if (strcmp(imgfile,"-") != 0) + if (!tostdout) printf ("%dx%d\n",xsize,ysize); if (calcpr) { for(i=0;calcpr[i];i++){ - if (strcmp(imgfile,"-") != 0) + if (!tostdout) printf("%s\n",calcpr[i]); free(calcpr[i]); }