/* read arg for '-c' option */ int get_arg(char ***args, char *str) { char *tmp; int i,n = 0; /* allocate some pointers */ for(i=0;i<strlen(str);i++) { if(str[i] == ' ') n++; } if((*args = (char **) calloc(n+2,sizeof(char *))) == NULL) clean_and_exit("calloc",1); /* first token */ i = 0; tmp = strtok(str," "); while(tmp != NULL) { /* allocate some RAM */ if(((*args)[i] = (char *) calloc(strlen(tmp),sizeof(char))) == NULL) clean_and_exit("calloc",1); strcpy((*args)[i],tmp); /* next token */ tmp = strtok(NULL," "); i++; } /* last pointer should be NULL */ (*args)[i] = NULL; return i; }
/* signal handler */ static void got_signal(int signum) { char buf[BUF_SIZE]; int lu; /* child quit => dnet client quit */ if(signum == SIGCHLD) { wait(NULL); clean_and_exit(NULL,0); } /* else we stop dnet client */ if(kill(fils,SIGQUIT) == -1) clean_and_exit("kill",1); if(!qflag) { while((lu = read(fd,buf,BUF_SIZE)) > 0) { buf[lu] = '\0'; printf("%s",buf); } } clean_and_exit(NULL,0); }
static void usage(char *pname) { fprintf(stderr,"Distributed.net client wrapper v%s\n",GKRELLDNET_VERSION); printf("Usage: %s [OPTION]...\n",pname); printf("Options:\n"); printf(" -h, --help print this help.\n"); printf(" -V, --version print version.\n"); printf(" -q, --quiet disable all terminal output and run in background.\n"); printf(" -l, --log=<file> redirect the client output to <file>.\n"); printf(" -c, --command=<cmd> use <cmd> to start the dnetc client (default: 'dnetc').\n"); #ifdef HAVE_LIBMICROHTTPD printf(" -p, --port=<n> listen on port <n>.\n"); #endif clean_and_exit(NULL,0); }
int main(int argc, char** argv) { setlocale(LC_ALL, ""); if (argc < 4) { printf("Usage: %s <username> <password> <out directory>\n", argv[0]); return 1; } if (!despotify_init()) { wrapper_printf("despotify_init() failed"); return 1; } struct despotify_session* ds = despotify_init_client(callback, NULL, true, true); if (!ds) { wrapper_printf("despotify_init_client() failed"); return 1; } /* listen ? */ if (!despotify_authenticate(ds, argv[1], argv[2])) { printf("Authentication failed: %s\n", despotify_get_error(ds)); despotify_exit(ds); return 1; } (void)print_playlists(ds, argv[3]); clean_and_exit(ds, EXIT_SUCCESS); return 0; }
void usage(void) { last_do_help(last_argc - 1, last_argv + 1); clean_and_exit(-1); }
int main(int argc, char **argv) { static const struct option options[] = { { "json", no_argument, NULL, 'j' }, { "help", no_argument, NULL, 'h' }, { "pretty", no_argument, NULL, 'p' }, { "version", no_argument, NULL, 'V' }, { "bpffs", no_argument, NULL, 'f' }, { "mapcompat", no_argument, NULL, 'm' }, { "nomount", no_argument, NULL, 'n' }, { 0 } }; int opt, ret; last_do_help = do_help; pretty_output = false; json_output = false; show_pinned = false; block_mount = false; bin_name = argv[0]; hash_init(prog_table.table); hash_init(map_table.table); opterr = 0; while ((opt = getopt_long(argc, argv, "Vhpjfmn", options, NULL)) >= 0) { switch (opt) { case 'V': return do_version(argc, argv); case 'h': return do_help(argc, argv); case 'p': pretty_output = true; /* fall through */ case 'j': if (!json_output) { json_wtr = jsonw_new(stdout); if (!json_wtr) { p_err("failed to create JSON writer"); return -1; } json_output = true; } jsonw_pretty(json_wtr, pretty_output); break; case 'f': show_pinned = true; break; case 'm': bpf_flags = MAPS_RELAX_COMPAT; break; case 'n': block_mount = true; break; default: p_err("unrecognized option '%s'", argv[optind - 1]); if (json_output) clean_and_exit(-1); else usage(); } } argc -= optind; argv += optind; if (argc < 0) usage(); ret = cmd_select(cmds, argc, argv, do_help); if (json_output) jsonw_destroy(&json_wtr); if (show_pinned) { delete_pinned_obj_table(&prog_table); delete_pinned_obj_table(&map_table); } return ret; }
int main(int argc,char *argv[]) { extern char *optarg; extern int optind; int ch; struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"debug", no_argument, 0, 'd'}, {"quiet", no_argument, 0, 'q'}, {"log", required_argument, 0, 'l'}, {"command", required_argument, 0, 'c'}, #ifdef HAVE_LIBMICROHTTPD {"port", required_argument, 0, 'p'}, #endif {0, 0, 0, 0} }; int contest_offset; char *tmp; char buf[BUF_SIZE]; int ttylog,lu,i,q; struct winsize ws; #ifdef HAVE_LIBMICROHTTPD unsigned int port = 0; #endif regmatch_t pmatch[2]; char defcmd[] = "dnetc", logfile[128] = "stdout"; int pos_cpu[MAX_CPU]; char p[] = "a"; int log_fd; /* check arguments */ #ifdef HAVE_LIBMICROHTTPD while ((ch = getopt_long(argc, argv, "hdVql:c:p:", long_options, NULL)) != -1) #else while ((ch = getopt_long(argc, argv, "hdVql:c:", long_options, NULL)) != -1) #endif { switch(ch) { case 'd': dflag = 1; break; case 'V': printf("%s\n",GKRELLDNET_VERSION); exit(0); break; #ifdef HAVE_LIBMICROHTTPD case 'p': port = atoi(optarg); break; #endif case 'q': qflag = 1; break; case 'c': nargc = get_arg(&nargv,optarg); break; case 'l': strcpy(logfile,optarg); break; case 'h': default: usage(argv[0]); } } if(argc - optind != 0) usage(argv[0]); /* continue in background if in quiet mode */ if(qflag == 1 && ((new_pid = fork()) != 0)) { if(new_pid < 0) clean_and_exit("forking daemon",1); else exit(0); } /* default command line */ if(nargv == NULL) nargc = get_arg(&nargv,defcmd); /* change output to logfile */ if(strcmp(logfile,"stdout") != 0) { if((log_fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) clean_and_exit("opening logfile",1); if(dup2(log_fd,1) == -1) clean_and_exit("dup2",1); } ttylog = isatty(1); /* creat shared memory segment */ if((shmid = my_shmcreate(sizeof(struct dnetc_values),IPC_CREAT|IPC_EXCL|0644)) == -1) clean_and_exit("shmget",1); if((int) (shmem = shmat(shmid,0,0)) == -1) clean_and_exit("shmat",1); /* init shared memory content */ shmem->running = TRUE; strcpy(shmem->contest,"???"); shmem->cmode = CRUNCH_RELATIVE; shmem->wu_in = shmem->wu_out = 0; shmem->n_cpu = 1; for(i=0;i<MAX_CPU;i++) shmem->val_cpu[i] = 0; /* precompile regex */ if(regcomp(&preg_in,"[0-9]+.packets?.+remains?.in",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_out,"[0-9]+.packets?(.+in.buff-out|.\\(.+stats?.units?\\).(are|is).in)",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_contest,"[A-Z0-9-]{3,6}(.#[a-z])?:.Loaded",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_proxy,"((Retrieved|Sent).+(stat..unit|packet)|Attempting.to.resolve|Connect(ing|ed).to)",REG_EXTENDED) !=0) clean_and_exit(NULL,1); contest_offset = 0; if(regcomp(&preg_absolute,"(#[0-9]+: [A-Z0-9-]{3,6}|[A-Z0-9-]{3,6}(.#[a-z])?):.+\\[[,0-9]+\\]",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_cruncher,"[0-9]+.cruncher.*started",REG_EXTENDED) != 0) clean_and_exit(NULL,1); regex_flag = 1; /* obtain a pseudo-terminal */ ws.ws_col = 132; ws.ws_row = 10; ws.ws_xpixel = ws.ws_ypixel = 0; if((openpty(&fd,&tty_fd,NULL,NULL,&ws)) == -1) clean_and_exit("openpty",1); /* start dnet client and start reading tty */ if((fils = fork()) == -1) clean_and_exit("fork",1); if(fils == 0) { /* change to dnetc directory */ change_dir(); /* start dnet client */ if(dup2(tty_fd,1) == -1) clean_and_exit("dup2",1); if(execvp(nargv[0],nargv) == -1) clean_and_exit("execvp",1); } #ifdef HAVE_LIBMICROHTTPD /* start http server */ if(port > 0) { struct sockaddr_in so; so.sin_family = AF_INET; so.sin_port = htons(port); so.sin_addr.s_addr = htonl(INADDR_LOOPBACK); http_daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, &answer_to_connection, shmem, MHD_OPTION_SOCK_ADDR, &so, MHD_OPTION_END); if(http_daemon == NULL) clean_and_exit("MHD_start_daemon",1); } #endif /* set signal handler */ if(signal(SIGHUP,got_signal) == SIG_ERR || signal(SIGINT,got_signal) == SIG_ERR || signal(SIGQUIT,got_signal) == SIG_ERR || signal(SIGCHLD,got_signal) == SIG_ERR || signal(SIGTERM,got_signal) == SIG_ERR) clean_and_exit("signal",1); /* some more init */ for(i=0;i<MAX_CPU;i++) shmem->val_cpu[i] = 0; /* main loop */ while((lu = mygets(fd,buf,BUF_SIZE)) > 0) { if(dflag) fprintf(stderr,"buf[0] = %x\n<--\n%s\n-->\n",buf[0],buf); if(buf[1] == '.' || buf[lu-1] != '\n') { if(dflag) fprintf(stderr,"lu: %02d, ",lu); /* fix line with two 0x0d char */ if((tmp = strchr(&buf[1],0x0d)) != NULL) lu = tmp-buf; /* skip line with proxy comm. */ q = regexec(&preg_proxy,buf,1,pmatch,0); if(q != 0) { /* check if line match absolute crunch-o-meter */ if(regexec(&preg_absolute,buf,1,pmatch,0) == 0) { /* set crunch-o-meter mode */ shmem->cmode = CRUNCH_ABSOLUTE; /* read CPU num */ tmp = strchr(&buf[pmatch[0].rm_so],'#'); if(tmp != NULL) { if(isdigit(tmp[1])) i = strtol(&buf[pmatch[0].rm_so+1],(char **) NULL,10) - 1; else if(islower(tmp[1])) i = tmp[1] - 'a'; /* avoid core dump */ i %= MAX_CPU; } else i = 0; /* read k(keys|nodes) */ shmem->val_cpu[i] = extract_val(&buf[pmatch[0].rm_so]); if(dflag) { fprintf(stderr,"\ncpu = %d, %llu nodes|keys\n",i,shmem->val_cpu[i]); fprintf(stderr,"found: %s\n",&buf[pmatch[0].rm_so]); } } else { /* set crunch-o-meter mode */ shmem->cmode = CRUNCH_RELATIVE; for(i=0;i<shmem->n_cpu;i++) { if(shmem->n_cpu != 1) { p[0] = 'a' + i; if((tmp = strstr(buf,p)) != NULL) pos_cpu[i] = tmp - buf; } else pos_cpu[i] = lu - 1; shmem->val_cpu[i] = (pos_cpu[i] - (pos_cpu[i]/8)*3) * 2; if(shmem->val_cpu[i] > 100) shmem->val_cpu[i] = 100; if(dflag) fprintf(stderr,"cpu%d: %llu,",i,shmem->val_cpu[i]); } if(dflag) fprintf(stderr,"\n"); } } if(!qflag && (ttylog || q == 0)) { printf("%s",buf); fflush(stdout); } } else { if(regexec(&preg_in,buf,1,pmatch,0) == 0) shmem->wu_in = strtol(&buf[pmatch[0].rm_so],NULL,10); if(regexec(&preg_out,buf,1,pmatch,0) == 0) shmem->wu_out = strtol(&buf[pmatch[0].rm_so],NULL,10); if(regexec(&preg_contest,buf,1,pmatch,0) == 0) strncpy(shmem->contest,&buf[pmatch[0].rm_so+contest_offset],3); if(regexec(&preg_cruncher,buf,1,pmatch,0) == 0) { shmem->n_cpu = strtol(&buf[pmatch[0].rm_so],NULL,10); /* too many crunchers */ if(shmem->n_cpu > MAX_CPU) { fprintf(stderr,"dnetw: too many crunchers\n"); clean_and_exit(NULL,1); } } if(dflag) fprintf(stderr,"contest = %s, in = %d, out = %d, n_cpu = %d\n",shmem->contest, shmem->wu_in, shmem->wu_out, shmem->n_cpu); if(!qflag) { printf("%s",buf); fflush(stdout); } } } clean_and_exit(NULL,0); return 0; }
int print_playlists(struct despotify_session* ds, char *out_dir) { struct playlist* rootlist = NULL; int err_mkdir = mkdir(out_dir, 01755); if (!err_mkdir) { wrapper_printf("mkdir() failed", out_dir); goto exit_app; } FILE *file = fopen("out/index.html", "w"); if (!file) { wrapper_printf("fopen() failed"); goto exit_app; } rootlist = despotify_get_stored_playlists(ds); if (!rootlist) { printf("No stored playlists!\n"); } else { fprintf(file, "<html>\n" " <head>\n" " <title>Spotify playlists</title>\n" " </head>\n" " <body>\n"); fprintf(file, " <table border=0>\n" " <tr><th>ID</th><th>Name</th><th>Tracks</th></tr>\n"); int count = 1; for (struct playlist* p = rootlist; p; p = p->next) { int plop = count++; fprintf(file, " <tr>" "<td>%2d</td>" " <td><a href='%2d.html'>%-40s</a></td>" " <td>%3d</td>" "</tr>\n", plop, plop, p->name, p->num_tracks); print_playlist_tracks(ds, p->tracks, p->name, plop); } fprintf(file, " </table>" " <br><br><small>Generated using <a href='https://github.com/rhaamo/spotify2html'>spotify2html</a></small>\n" " </body>\n" "</html>\n"); } fclose(file); printf("Done!\n"); return 0; exit_app: clean_and_exit(ds, EXIT_FAILURE); return -1; /* NEVER REACHED */ }
int print_playlist_tracks(struct despotify_session* ds, struct track* head, char *playlist_name, int id) { char *fname; int i = asprintf(&fname, "out/%i.html", id); if (!i) { wrapper_printf("asprintf() failed"); goto exit_app; } FILE *file = fopen(fname, "w"); if (!file) { wrapper_printf("fopen() failed"); goto exit_app; } fprintf(file, "<html>\n" " <head>\n" " <title>Playlist: %s</title>\n" " </head>\n" " <body>\n", playlist_name); fprintf(file, " <a href='index.html'>Back to playlists list</a>\n"); if (!head) { fprintf(file, "Empty playlist!"); } else { fprintf(file, " <table border=0>\n" " <tr><th>ID</th><th>Name</th><th>Tracks</th></tr>\n"); int count = 1; for (struct track* t = head; t; t = t->next) { if (t->has_meta_data) { fprintf(file, " <tr>\n" " <td>%3d</td>" " <td>%-40s</td>" " <td>%2d:%02d</td>", count++, t->title, t->length / 60000, t->length % 60000 / 1000); } else { fprintf(file, "<tr><td>%3d</td><td>N/A</td><td></td><td></td>", count++); } } fprintf(file, " </table>" " <br><br><small>Generated using <a href='https://github.com/rhaamo/spotify2html'>spotify2html</a></small>\n" " </body>\n" "</html>\n"); } return 0; exit_app: clean_and_exit(ds, EXIT_FAILURE); return -1; /* NEVER REACHED */ }