int main(void) { if (!is_root()) { printf("Root privileges needed to get the hard disk info.\n"); return EXIT_SUCCESS; } char *df = which("df"); char *fdisk = which("fdisk"); char *cmd = NULL; char *path = NULL; cmd = calloc(55, sizeof(char)); sprintf(cmd, "%s -l | grep /dev/ | awk '{print $%d}'", fdisk, 2); path = get_cmd_output(cmd, 12); path = strip_end(path, ":"); hd = new_device(path); delete(&cmd); delete(&path); cmd = calloc(45, sizeof(char)); sprintf(cmd, "%s -P | awk '{print $%d}' | grep %s", df, 1, hd->name); path = get_cmd_output(cmd, 13); part = new_device(path); delete(&cmd); delete(&path); int fails = 0; Suite *s = hd_suite(); SRunner *sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); fails = srunner_ntests_failed(sr); srunner_free(sr); return (fails == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }
void strip(char **str) { if (!str || !(*str)) return; while (isspace(**str)) (*str)++; strip_end(*str); }
std::vector< std::string > split(std::string const &val, const char c, const int flags) { std::vector< std::string > res; std::string::const_iterator i1 = val.begin(); std::string::const_iterator i2; if (flags & STRIP_SPACES) { while (i1 != val.end() && portable_isspace(*i1)) ++i1; } i2=i1; while (i2 != val.end()) { if (*i2 == c) { std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip_end(new_val); if (!(flags & REMOVE_EMPTY) || !new_val.empty()) res.push_back(new_val); ++i2; if (flags & STRIP_SPACES) { while (i2 != val.end() && portable_isspace(*i2)) ++i2; } i1 = i2; } else { ++i2; } } std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip_end(new_val); if (!(flags & REMOVE_EMPTY) || !new_val.empty()) res.push_back(new_val); return res; }
char *get_cmd_output(const char *command, size_t buf_size) { if (command == NULL || buf_size < 1) return NULL; FILE *fp = NULL; char *output = NULL; char *temp = NULL; char *buffer = NULL; int status = 0; if ((fp = open_cmd_stream(command)) == NULL) return NULL; buffer = calloc(buf_size, sizeof(*buffer)); if (buffer == NULL) return NULL; if (fgets(buffer, buf_size, fp) != NULL) { temp = strip_end(buffer, ""); } delete(&buffer); status = close_cmd_stream(&fp); if (status == SUCCESS && !is_blank(temp)) { output = temp; } else { delete(&temp); output = calloc(100, sizeof(*output)); if (output == NULL) return NULL; copy_string(output, UNDEFINED_INFO, 100); } return output; }
/* * Read a line of a configuration file and process continuation lines. * Return buf, or NULL if EOF. * Blank at the end of line are always stripped. * Everything on a line following comchar is a comment. * * Continuation character is \ * Comment character is # */ char *fgets_strip(char **p_buf, int *sizebuf, FILE * fin, int *lineno) { char *ret = NULL; char *buf; char *buf_prev = NULL; char comchar = '#'; char contchar = '\\'; while ((buf = fgets_local(p_buf, sizebuf, fin)) != NULL) { char *start = buf; char *end = strip_end(buf); char *pt = strchr(buf, comchar); if (pt != NULL) { *pt = '\0'; end = strip_end(buf); } if (lineno != NULL) (*lineno)++; ret = start; if (buf_prev) { /* this is a continuation line, append to the previous * buffer which becomes the current buffer. White space * at the start of the continuation line is replaced by * a single space. */ int buf_prev_len = strlen(buf_prev); buf_prev = xrealloc(buf_prev, buf_prev_len + *sizebuf); memcpy(buf_prev + buf_prev_len, *p_buf, *sizebuf); *sizebuf += buf_prev_len; free(*p_buf); *p_buf = buf_prev; buf_prev = NULL; buf = *p_buf + buf_prev_len; pt = buf; start = buf; end = strip_end(buf); while (isspace(*pt)) pt++; if (pt > buf + 1) { strcpy(buf + 1, pt); /* safe, backward copy */ buf[0] = ' '; end -= (int) (pt - buf) - 1; } else if (pt == buf + 1) { buf[0] = ' '; } } if (end > buf && *(end - 1) == contchar) { if (end == buf + 1 || *(end - 2) != contchar) { /* Continuation */ end--; *end = '\0'; buf_prev = *p_buf; *p_buf = NULL; } else { *(end - 1) = '\0'; break; } } else { break; } } return ret; }
char *pr_ident_lookup(pool *p, conn_t *c) { char *ret = "UNKNOWN"; pool *tmp_pool = NULL; conn_t *ident_conn = NULL, *ident_io = NULL; char buf[256] = {'\0'}, *tok = NULL, *tmp = NULL; int timerno, i = 0; int ident_port = pr_inet_getservport(p, "ident", "tcp"); tmp_pool = make_sub_pool(p); ident_timeout = 0; nstrm = NULL; if (ident_port == -1) { destroy_pool(tmp_pool); return pstrdup(p, ret); } /* Set up our timer before going any further. */ timerno = pr_timer_add(PR_TUNABLE_TIMEOUTIDENT, -1, NULL, (callback_t) ident_timeout_cb, "ident lookup"); if (timerno <= 0) { destroy_pool(tmp_pool); return pstrdup(p, ret); } ident_conn = pr_inet_create_connection(tmp_pool, NULL, -1, c->local_addr, INPORT_ANY, FALSE); pr_inet_set_nonblock(tmp_pool, ident_conn); i = pr_inet_connect_nowait(tmp_pool, ident_conn, c->remote_addr, ident_port); if (i < 0) { int xerrno = errno; pr_timer_remove(timerno, ANY_MODULE); pr_inet_close(tmp_pool, ident_conn); pr_trace_msg(trace_channel, 5, "connection to %s, port %d failed: %s", pr_netaddr_get_ipstr(c->remote_addr), ident_port, strerror(xerrno)); destroy_pool(tmp_pool); errno = xerrno; return pstrdup(p, ret); } if (!i) { /* Not yet connected. */ nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, ident_conn->listen_fd, PR_NETIO_IO_RD); pr_netio_set_poll_interval(nstrm, 1); switch (pr_netio_poll(nstrm)) { /* Aborted, timed out */ case 1: { if (ident_timeout) { pr_timer_remove(timerno, ANY_MODULE); pr_netio_close(nstrm); pr_inet_close(tmp_pool, ident_conn); pr_trace_msg(trace_channel, 5, "lookup timed out, returning '%s'", ret); destroy_pool(tmp_pool); return pstrdup(p, ret); } break; } /* Error. */ case -1: { int xerrno = errno; pr_timer_remove(timerno, ANY_MODULE); pr_netio_close(nstrm); pr_inet_close(tmp_pool, ident_conn); pr_trace_msg(trace_channel, 6, "lookup failed (%s), returning '%s'", strerror(xerrno), ret); destroy_pool(tmp_pool); errno = xerrno; return pstrdup(p, ret); } /* Connected. */ default: { ident_conn->mode = CM_OPEN; if (pr_inet_get_conn_info(ident_conn, ident_conn->listen_fd) < 0) { int xerrno = errno; pr_timer_remove(timerno, ANY_MODULE); pr_netio_close(nstrm); pr_inet_close(tmp_pool, ident_conn); pr_trace_msg(trace_channel, 2, "lookup timed out (%s), returning '%s'", strerror(xerrno), ret); destroy_pool(tmp_pool); errno = xerrno; return pstrdup(p, ret); } break; } } } ident_io = pr_inet_openrw(tmp_pool, ident_conn, NULL, PR_NETIO_STRM_OTHR, -1, -1, -1, FALSE); if (ident_io == NULL) { int xerrno = errno; pr_timer_remove(timerno, ANY_MODULE); pr_inet_close(tmp_pool, ident_conn); pr_trace_msg(trace_channel, 3, "failed opening read/write connection: %s", strerror(xerrno)); destroy_pool(tmp_pool); errno = xerrno; return pstrdup(p, ret); } nstrm = ident_io->instrm; pr_inet_set_nonblock(tmp_pool, ident_io); pr_netio_set_poll_interval(ident_io->instrm, 1); pr_netio_set_poll_interval(ident_io->outstrm, 1); pr_netio_printf(ident_io->outstrm, "%d, %d\r\n", c->remote_port, c->local_port); /* If the timer fires while in netio_gets(), netio_gets() will simply return * either a partial string, or NULL. This works because ident_timeout_cb * aborts the stream from which we are reading. netio_set_poll_interval() is * used to make sure significant delays don't occur on systems that * automatically restart syscalls after the SIGALRM signal. */ pr_trace_msg(trace_channel, 4, "reading response from remote ident server"); if (pr_netio_gets(buf, sizeof(buf), ident_io->instrm)) { strip_end(buf, "\r\n"); pr_trace_msg(trace_channel, 6, "received '%s' from remote ident server", buf); tmp = buf; tok = get_token(&tmp, ":"); if (tok && (tok = get_token(&tmp, ":"))) { while (*tok && isspace((int) *tok)) { pr_signals_handle(); tok++; } strip_end(tok, " \t"); if (strcasecmp(tok, "ERROR") == 0) { if (tmp) { while (*tmp && isspace((int) *tmp)) { pr_signals_handle(); tmp++; } strip_end(tmp, " \t"); if (strcasecmp(tmp, "HIDDEN-USER") == 0) ret = "HIDDEN-USER"; } } else if (strcasecmp(tok, "USERID") == 0) { if (tmp && (tok = get_token(&tmp, ":"))) { if (tmp) { while (*tmp && isspace((int) *tmp)) { pr_signals_handle(); tmp++; } strip_end(tmp, " \t"); ret = tmp; } } } } } pr_timer_remove(timerno, ANY_MODULE); pr_inet_close(tmp_pool, ident_io); pr_inet_close(tmp_pool, ident_conn); destroy_pool(tmp_pool); return pstrdup(p, ret); }
std::vector< std::string > parenthetical_split(std::string const &val, const char separator, std::string const &left, std::string const &right,const int flags) { std::vector< std::string > res; std::vector<char> part; bool in_parenthesis = false; std::string lp=left; std::string rp=right; std::string::const_iterator i1 = val.begin(); std::string::const_iterator i2; if (flags & STRIP_SPACES) { while (i1 != val.end() && portable_isspace(*i1)) ++i1; } i2=i1; if(left.size()!=right.size()){ ERR_GENERAL << "Left and Right Parenthesis lists not same length\n"; return res; } while (i2 != val.end()) { if(!in_parenthesis && separator && *i2 == separator){ std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip_end(new_val); if (!(flags & REMOVE_EMPTY) || !new_val.empty()) res.push_back(new_val); ++i2; if (flags & STRIP_SPACES) { while (i2 != val.end() && portable_isspace(*i2)) ++i2; } i1=i2; continue; } if(!part.empty() && *i2 == part.back()){ part.pop_back(); if(!separator && part.empty()){ std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip(new_val); res.push_back(new_val); ++i2; i1=i2; }else{ if (part.empty()) in_parenthesis = false; ++i2; } continue; } bool found=false; for(size_t i=0; i < lp.size(); i++){ if (*i2 == lp[i]){ if (!separator && part.empty()){ std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip(new_val); res.push_back(new_val); ++i2; i1=i2; }else{ ++i2; } part.push_back(rp[i]); found=true; break; } } if(!found){ ++i2; } else in_parenthesis = true; } std::string new_val(i1, i2); if (flags & STRIP_SPACES) strip(new_val); if (!(flags & REMOVE_EMPTY) || !new_val.empty()) res.push_back(new_val); if(!part.empty()){ ERR_GENERAL << "Mismatched parenthesis:\n"<<val<<"\n";; } return res; }
std::vector< std::string > square_parenthetical_split(std::string const &val, const char separator, std::string const &left, std::string const &right,const int flags) { std::vector< std::string > res; std::vector<char> part; bool in_parenthesis = false; std::vector<std::string::const_iterator> square_left; std::vector<std::string::const_iterator> square_right; std::vector< std::string > square_expansion; std::string lp=left; std::string rp=right; std::string::const_iterator i1 = val.begin(); std::string::const_iterator i2; std::string::const_iterator j1; if (flags & STRIP_SPACES) { while (i1 != val.end() && portable_isspace(*i1)) ++i1; } i2=i1; j1=i1; if (i1 == val.end()) return res; if (!separator) { ERR_GENERAL << "Separator must be specified for square bracket split funtion.\n"; return res; } if(left.size()!=right.size()){ ERR_GENERAL << "Left and Right Parenthesis lists not same length\n"; return res; } while (true) { if(i2 == val.end() || (!in_parenthesis && *i2 == separator)) { //push back square contents size_t size_square_exp = 0; for (size_t i=0; i < square_left.size(); i++) { std::string tmp_val(square_left[i]+1,square_right[i]); std::vector< std::string > tmp = split(tmp_val); std::vector<std::string>::const_iterator itor = tmp.begin(); for(; itor != tmp.end(); ++itor) { size_t found_tilde = (*itor).find_first_of('~'); if (found_tilde == std::string::npos) { size_t found_asterisk = (*itor).find_first_of('*'); if (found_asterisk == std::string::npos) { std::string tmp = (*itor); square_expansion.push_back(strip(tmp)); } else { //'*' multiple expansion std::string s_begin = (*itor).substr(0,found_asterisk); s_begin = strip(s_begin); std::string s_end = (*itor).substr(found_asterisk+1); s_end = strip(s_end); for (int ast=atoi(s_end.c_str()); ast>0; --ast) square_expansion.push_back(s_begin); } } else { //expand number range std::string s_begin = (*itor).substr(0,found_tilde); s_begin = strip(s_begin); int begin = atoi(s_begin.c_str()); size_t padding = 0; while (padding<s_begin.size() && s_begin[padding]=='0') { padding++; } std::string s_end = (*itor).substr(found_tilde+1); s_end = strip(s_end); int end = atoi(s_end.c_str()); if (padding==0) { while (padding<s_end.size() && s_end[padding]=='0') { padding++; } } int increment = (end >= begin ? 1 : -1); end+=increment; //include end in expansion for (int k=begin; k!=end; k+=increment) { std::string pb = boost::lexical_cast<std::string>(k); for (size_t p=pb.size(); p<=padding; p++) pb = std::string("0") + pb; square_expansion.push_back(pb); } } } if (i*square_expansion.size() != (i+1)*size_square_exp ) { std::string tmp(i1, i2); ERR_GENERAL << "Square bracket lengths do not match up: "+tmp+"\n"; return res; } size_square_exp = square_expansion.size(); } //combine square contents and rest of string for comma zone block size_t j = 0; size_t j_max = 0; if (square_left.size() != 0) j_max = square_expansion.size() / square_left.size(); do { j1 = i1; std::string new_val; for (size_t i=0; i < square_left.size(); i++) { std::string tmp_val(j1, square_left[i]); new_val.append(tmp_val); size_t k = j+i*j_max; if (k < square_expansion.size()) new_val.append(square_expansion[k]); j1 = square_right[i]+1; } std::string tmp_val(j1, i2); new_val.append(tmp_val); if (flags & STRIP_SPACES) strip_end(new_val); if (!(flags & REMOVE_EMPTY) || !new_val.empty()) res.push_back(new_val); j++; } while (j<j_max); if (i2 == val.end()) //escape loop break; ++i2; if (flags & STRIP_SPACES) { //strip leading spaces while (i2 != val.end() && portable_isspace(*i2)) ++i2; } i1=i2; square_left.clear(); square_right.clear(); square_expansion.clear(); continue; } if(!part.empty() && *i2 == part.back()) { part.pop_back(); if (*i2 == ']') square_right.push_back(i2); if (part.empty()) in_parenthesis = false; ++i2; continue; } bool found=false; for(size_t i=0; i < lp.size(); i++) { if (*i2 == lp[i]){ if (*i2 == '[') square_left.push_back(i2); ++i2; part.push_back(rp[i]); found=true; break; } } if(!found){ ++i2; } else in_parenthesis = true; } if(!part.empty()){ ERR_GENERAL << "Mismatched parenthesis:\n"<<val<<"\n";; } return res; }