int main(void) { Bootstrap(); // Need to initialize library printf("============> Start Str Tests\n\n"); printf("=> Test1: copy\n"); { char s3[STRLEN]; printf("\tResult: %s\n", Str_copy(s3, "The abc house", 7)); assert(Str_isEqual(s3, "The abc")); printf("\tTesting for NULL argument\n"); assert(!Str_copy(NULL, NULL, 7)); } printf("=> Test1: OK\n\n"); printf("=> Test2: dup\n"); { char *s4 = Str_dup("abc123"); printf("\tResult: %s\n", s4); assert(Str_isEqual(s4, "abc123")); printf("\tTesting for NULL argument\n"); assert(!Str_dup(NULL)); FREE(s4); } printf("=> Test2: OK\n\n"); printf("=> Test3: ndup\n"); { char *s5 = Str_ndup("abc123", 3); printf("\tResult: %s\n", s5); assert(Str_isEqual(s5, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_ndup(NULL, 3)); FREE(s5); } printf("=> Test3: OK\n\n"); printf("=> Test4: Str_cat & Str_vcat\n"); { char *s6; s6 = Str_cat("%s://%s%s?%s", "https", "foo.bar", "/uri", "abc=123"); printf("\tResult: %s\n", s6); assert(Str_isEqual(s6, "https://foo.bar/uri?abc=123")); FREE(s6); printf("\tTesting for NULL arguments\n"); s6 = Str_cat(NULL); assert(s6==NULL); FREE(s6); } printf("=> Test4: OK\n\n"); printf("=> Test5: chomp\n"); { char s3[] = "abc\r\n123"; printf("\tResult: %s\n", Str_chomp(s3)); assert(Str_isEqual(s3, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_chomp(NULL)); } printf("=> Test5: OK\n\n"); printf("=> Test6: trim\n"); { char e[] = " "; char o[] = " a "; char s[] = " abcdef"; char s4[] = " \t abc \r\n\t "; assert(Str_isEqual(Str_ltrim(s), "abcdef")); printf("\tResult: %s\n", Str_trim(s4)); assert(Str_isEqual(s4, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_trim(NULL)); assert(Str_isEqual(Str_ltrim(e), "")); memcpy(e, " ", sizeof(" ") - 1); assert(Str_isEqual(Str_rtrim(e), "")); memcpy(e, " ", sizeof(" ") - 1); assert(Str_isEqual(Str_trim(e), "")); assert(Str_isEqual(Str_ltrim(o), "a ")); memcpy(o, " a ", sizeof(" a ") - 1); assert(Str_isEqual(Str_rtrim(o), " a")); memcpy(o, " a ", sizeof(" a ") - 1); assert(Str_isEqual(Str_trim(o), "a")); assert(Str_isEqual(Str_trim(o), "a")); } printf("=> Test6: OK\n\n"); printf("=> Test7: trim quotes\n"); { char s5[] = "\"'abc'\""; char s5a[] = "\"'abc"; char s5b[] = "abc'\""; char s5c[] = "'\""; char s5d[] = " \t abc def '\" "; printf("\tResult: %s\n", Str_unquote(s5)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5a)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5b)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5b)); assert(Str_isEqual(s5, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_unquote(NULL)); printf("\tTesting for quotes-only argument\n"); assert(Str_isEqual("", Str_unquote(s5c))); printf("\tTesting for quotes and white-space removal\n"); assert(Str_isEqual("abc def", Str_unquote(s5d))); } printf("=> Test7: OK\n\n"); printf("=> Test8: toLowerCase\n"); { char s6[] = "AbC"; printf("\tResult: %s\n", Str_toLower(s6)); assert(Str_isEqual(s6, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_toLower(NULL)); } printf("=> Test8: OK\n\n"); printf("=> Test9: toUpperCase\n"); { char s7[] = "aBc"; printf("\tResult: %s\n", Str_toUpper(s7)); assert(Str_isEqual(s7, "ABC")); printf("\tTesting for NULL argument\n"); assert(!Str_toUpper(NULL)); } printf("=> Test9: OK\n\n"); printf("=> Test10: parseInt, parseLLong, parseDouble\n"); { char i[STRLEN] = " -2812 bla"; char ll[STRLEN] = " 2147483642 blabla"; char d[STRLEN] = " 2.718281828 this is e"; char de[STRLEN] = "1.495E+08 kilometer = An Astronomical Unit"; char ie[STRLEN] = " 9999999999999999999999999999999999999999"; printf("\tResult:\n"); printf("\tParsed int = %d\n", Str_parseInt(i)); assert(Str_parseInt(i)==-2812); printf("\tParsed long long = %lld\n", Str_parseLLong(ll)); assert(Str_parseLLong(ll)==2147483642); printf("\tParsed double = %.9f\n", Str_parseDouble(d)); assert(Str_parseDouble(d)==2.718281828); printf("\tParsed double exp = %.3e\n", Str_parseDouble(de)); assert(Str_parseDouble(de)==1.495e+08); TRY Str_parseInt(ie); assert(false); CATCH(NumberFormatException) printf("=> Test11: OK\n\n"); END_TRY; } printf("=> Test10: OK\n\n"); printf("=> Test11: replace\n"); { char s9[] = "abccba"; printf("\tResult: %s\n", Str_replaceChar(s9, 'b', 'X')); assert(Str_isEqual(s9, "aXccXa")); printf("\tTesting for NULL argument\n"); assert(!Str_replaceChar(NULL, 'b', 'X')); } printf("=> Test11: OK\n\n"); printf("=> Test12: startsWith\n"); { char *a = "mysql://*****:*****@ ]+@([-a-zA-Z0-9]+\\.)+[a-zA-Z]{2,}$"; char *valid_phone1 = "+4797141255"; char *valid_phone2 = "(47)-97-14-12-55"; char *invalid_phone1 = "141255"; char *invalid_phone2 = "(47)971412551234567890123456789012345678901234567890"; char *invalid_phone3 = ""; char *invalid_phone4 = "abc123"; char *valid_email1 = "*****@*****.**"; char *valid_email2 = "*****@*****.**"; char *invalid_email1 = "hauktildeslash.com"; char *invalid_email2 = ""; char *invalid_email3 = "hauk@tildeslashcom"; char *invalid_email4 = "hauk@æøåtildeslash.com"; // phone printf("\tResult: match(%s, %s)\n", phone_pattern, valid_phone1); assert(Str_match(phone_pattern, valid_phone1)); printf("\tResult: match(%s, %s)\n", phone_pattern, valid_phone2); assert(Str_match(phone_pattern, valid_phone2)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone1); assert(! Str_match(phone_pattern, invalid_phone1)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone2); assert(! Str_match(phone_pattern, invalid_phone2)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone3); assert(! Str_match(phone_pattern, invalid_phone3)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone4); assert(! Str_match(phone_pattern, invalid_phone4)); // email printf("\tResult: match(%s, %s)\n", email_pattern, valid_email1); assert(Str_match(email_pattern, valid_email1)); printf("\tResult: match(%s, %s)\n", email_pattern, valid_email2); assert(Str_match(email_pattern, valid_email2)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email1); assert(! Str_match(email_pattern, invalid_email1)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email2); assert(! Str_match(email_pattern, invalid_email2)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email3); assert(! Str_match(email_pattern, invalid_email3)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email4); assert(! Str_match(email_pattern, invalid_email4)); } printf("=> Test17: OK\n\n"); printf("=> Test18: lim\n"); { char *zero = ""; char *two = "12"; char *ten = "1234567890"; assert(! Str_lim(zero, 0)); assert(!Str_lim(zero, 1)); assert(Str_lim(two, 0)); assert(Str_lim(two, 1)); assert(!Str_lim(two, 2)); assert(Str_lim(ten, 0)); assert(Str_lim(ten, 5)); assert(Str_lim(ten, 9)); assert(!Str_lim(ten, 10)); assert(! Str_lim(ten, 100)); } printf("=> Test18: OK\n\n"); printf("=> Test19: substring\n"); { assert(Str_sub("foo bar baz", "bar")); assert(! Str_sub("foo bar baz", "barx")); assert(Str_isEqual(Str_sub("foo bar baz", "baz"), "baz")); assert(Str_sub("foo bar baz", "foo bar baz")); assert(Str_sub("a", "a")); assert(! Str_sub("a", "b")); assert(! Str_sub("", "")); assert(! Str_sub("foo", "")); assert(! Str_sub("abc", "abcdef")); assert(! Str_sub("foo", "foo bar")); assert(Str_isEqual(Str_sub("foo foo bar", "foo bar"), "foo bar")); assert(Str_sub("foo foo bar foo bar baz fuu", "foo bar baz")); assert(Str_isEqual(Str_sub("abcd abcc", "abcc"), "abcc")); } printf("=> Test19: OK\n\n"); printf("=> Test20: Str_join\n"); { char *p = NULL; char dest[10+1] = "xxxxxxxxx"; char a[] = "abc"; char *b = "def"; char *c = "xxx123"; assert(Str_isEqual(Str_join(dest, 10, a, b, "ghi"), "abcdefghi")); assert(Str_isEqual(Str_join(dest, 10, p), "")); assert(Str_isEqual(Str_join(dest, 10), "")); assert(Str_isEqual(Str_join(dest, 10, "012", "3456789", "0123456789"), "0123456789")); assert(Str_isEqual(Str_join(dest, 4, "a", "b", "cd", "ghi", "jklmnopq"), "abcd")); assert(Str_isEqual(Str_join(dest, 10, a, c + 3), "abc123")); Str_join(dest, 0); assert(dest[0]==0); } printf("=> Test20: OK\n\n"); printf("=> Test21: Str_has\n"); { char *foo = "'bar' (baz)"; assert(Str_has("(')", foo)); assert(! Str_has(",;", foo)); } printf("=> Test21: OK\n\n"); printf("=> Test22: Str_curtail\n"); { char s[] = "<text>Hello World</text>"; assert(Str_isByteEqual(Str_curtail(s, "</text>"), "<text>Hello World")); assert(Str_isByteEqual(Str_curtail(s, ">"), "<text")); assert(Str_isByteEqual(Str_curtail(s, "@"), "<text")); } printf("=> Test22: OK\n\n"); printf("=> Test23: Str_ton\n"); { char str[43]; long l1 = 42, l2 = 0, l3 = -42, l4 = LONG_MAX, l5 = LONG_MIN; assert(Str_isByteEqual("42", Str_ton(42, (char[43]){0}))); assert(Str_isByteEqual("42", Str_ton(l1, str))); assert(Str_isByteEqual("0", Str_ton(l2, str))); assert(Str_isByteEqual("-42", Str_ton(l3, str))); assert(l4 == Str_parseLLong(Str_ton(l4, str))); assert(l5 == Str_parseLLong(Str_ton(l5, str))); }
/** * Read all processes of the proc files system to initialize * the process tree (sysdep version... but should work for * all procfs based unices) * @param reference reference of ProcessTree * @return treesize>0 if succeeded otherwise =0. */ int initprocesstree_sysdep(ProcessTree_T ** reference) { int i = 0, j; int rv, bytes = 0; int treesize = 0; int stat_ppid = 0; char *tmp = NULL; char procname[STRLEN]; char buf[1024]; char stat_item_state; long stat_item_cutime = 0; long stat_item_cstime = 0; long stat_item_rss = 0; glob_t globbuf; unsigned long stat_item_utime = 0; unsigned long stat_item_stime = 0; unsigned long long stat_item_starttime = 0ULL; ProcessTree_T *pt = NULL; ASSERT(reference); /* Find all processes in the /proc directory */ if ((rv = glob("/proc/[0-9]*", GLOB_ONLYDIR, NULL, &globbuf))) { LogError("system statistic error -- glob failed: %d (%s)\n", rv, STRERROR); return FALSE; } treesize = globbuf.gl_pathc; pt = CALLOC(sizeof(ProcessTree_T), treesize); /* Insert data from /proc directory */ for (i = 0; i < treesize; i++) { pt[i].pid = atoi(globbuf.gl_pathv[i] + strlen("/proc/")); if (!read_proc_file(buf, sizeof(buf), "stat", pt[i].pid, NULL)) { DEBUG("system statistic error -- cannot read /proc/%d/stat\n", pt[i].pid); continue; } pt[i].time = get_float_time(); if (!(tmp = strrchr(buf, ')'))) { DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid); continue; } *tmp = 0; if (sscanf(buf, "%*d (%256s", procname) != 1) { DEBUG("system statistic error -- file /proc/%d/stat process name parse error\n", pt[i].pid); continue; } tmp += 2; /* This implementation is done by using fs/procfs/array.c as a basis * it is also worth looking into the source of the procps utils */ if (sscanf(tmp, "%c %d %*d %*d %*d %*d %*u %*u" "%*u %*u %*u %lu %lu %ld %ld %*d %*d %*d " "%*u %llu %*u %ld %*u %*u %*u %*u %*u " "%*u %*u %*u %*u %*u %*u %*u %*u %*d %*d\n", &stat_item_state, &stat_ppid, &stat_item_utime, &stat_item_stime, &stat_item_cutime, &stat_item_cstime, &stat_item_starttime, &stat_item_rss) != 8) { DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid); continue; } pt[i].ppid = stat_ppid; pt[i].starttime = get_starttime() + (time_t)(stat_item_starttime / HZ); /* jiffies -> seconds = 1 / HZ * HZ is defined in "asm/param.h" and it is usually 1/100s but on * alpha system it is 1/1024s */ pt[i].cputime = ((float)(stat_item_utime + stat_item_stime) * 10.0) / HZ; pt[i].cpu_percent = 0; /* State is Zombie -> then we are a Zombie ... clear or? (-: */ if (stat_item_state == 'Z') pt[i].status_flag |= PROCESS_ZOMBIE; if (page_shift_to_kb < 0) pt[i].mem_kbyte = (stat_item_rss >> abs(page_shift_to_kb)); else pt[i].mem_kbyte = (stat_item_rss << abs(page_shift_to_kb)); if (! read_proc_file(buf, sizeof(buf), "cmdline", pt[i].pid, &bytes)) { DEBUG("system statistic error -- cannot read /proc/%d/cmdline\n", pt[i].pid); continue; } /* The cmdline file contains argv elements/strings terminated separated by '\0' => join the string: */ for (j = 0; j < (bytes - 1); j++) if (buf[j] == 0) buf[j] = ' '; pt[i].cmdline = *buf ? Str_dup(buf) : Str_dup(procname); }
/** * Generate a new ssl connection * @return ssl connection container */ ssl_connection *new_ssl_connection(char *clientpemfile, int sslversion) { ssl_connection *ssl; if (!ssl_initialized) start_ssl(); NEW(ssl); ssl->socket_bio = NULL; ssl->handler = NULL; ssl->cert = NULL; ssl->cipher = NULL; ssl->socket = 0; ssl->next = NULL; ssl->accepted = FALSE; ssl->cert_md5 = NULL; ssl->cert_md5_len = 0; ssl->clientpemfile = clientpemfile ? Str_dup(clientpemfile) : NULL; switch (sslversion) { case SSL_VERSION_AUTO: #ifdef OPENSSL_FIPS if (FIPS_mode()) { ssl->method = TLSv1_client_method(); } else #endif ssl->method = SSLv23_client_method(); break; case SSL_VERSION_SSLV2: #ifdef OPENSSL_NO_SSL2 LogError("SSLv2 is not allowed - use either SSLv3 or TLSv1"); goto sslerror; #else #ifdef OPENSSL_FIPS if (FIPS_mode()) { LogError("SSLv2 is not allowed in FIPS mode - use TLSv1"); goto sslerror; } else #endif ssl->method = SSLv2_client_method(); #endif break; case SSL_VERSION_SSLV3: #ifdef OPENSSL_FIPS if (FIPS_mode()) { LogError("SSLv3 is not allowed in FIPS mode - use TLSv1"); goto sslerror; } else #endif ssl->method = SSLv3_client_method(); break; case SSL_VERSION_TLS: /* fall through */ default: ssl->method = TLSv1_client_method(); break; } if (!ssl->method) { LogError("%s: Cannot initialize SSL method -- %s\n", prog, SSLERROR); goto sslerror; } if (!(ssl->ctx = SSL_CTX_new(ssl->method))) { LogError("%s: Cannot initialize SSL server certificate handler -- %s\n", prog, SSLERROR); goto sslerror; } if (ssl->clientpemfile) { if (SSL_CTX_use_certificate_chain_file(ssl->ctx, ssl->clientpemfile) <= 0) { LogError("%s: Cannot initialize SSL server certificate -- %s\n", prog, SSLERROR); goto sslerror; } if (SSL_CTX_use_PrivateKey_file(ssl->ctx, ssl->clientpemfile, SSL_FILETYPE_PEM) <= 0) { LogError("%s: Cannot initialize SSL server private key -- %s\n", prog, SSLERROR); goto sslerror; } if (!SSL_CTX_check_private_key(ssl->ctx)) { LogError("%s: Private key does not match the certificate public key -- %s\n", prog, SSLERROR); goto sslerror; } } return ssl; sslerror: delete_ssl_socket(ssl); return NULL; }
/** * Read all processes to initialize the information tree. * @param reference reference of ProcessTree * @return treesize > 0 if succeeded otherwise = 0. */ int initprocesstree_sysdep(ProcessTree_T **reference) { int treesize; char buf[_POSIX2_LINE_MAX]; size_t size = sizeof(maxslp); int mib_proc[6] = {CTL_KERN, KERN_PROC, KERN_PROC_KTHREAD, 0, sizeof(struct kinfo_proc), 0}; static int mib_maxslp[] = {CTL_VM, VM_MAXSLP}; ProcessTree_T *pt; kvm_t *kvm_handle; static struct kinfo_proc *pinfo; if (sysctl(mib_maxslp, 2, &maxslp, &size, NULL, 0) < 0) { LogError("system statistic error -- vm.maxslp failed"); return FALSE; } if (sysctl(mib_proc, 6, NULL, &size, NULL, 0) == -1) { LogError("system statistic error -- kern.proc #1 failed"); return FALSE; } size *= 2; // Add reserve for new processes which were created between calls of sysctl pinfo = CALLOC(1, size); mib_proc[5] = (int)(size / sizeof(struct kinfo_proc)); if (sysctl(mib_proc, 6, pinfo, &size, NULL, 0) == -1) { FREE(pinfo); LogError("system statistic error -- kern.proc #2 failed"); return FALSE; } treesize = (int)(size / sizeof(struct kinfo_proc)); pt = CALLOC(sizeof(ProcessTree_T), treesize); if (! (kvm_handle = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf))) { LogError("system statistic error -- kvm_openfiles failed: %s", buf); return FALSE; } for (int i = 0; i < treesize; i++) { pt[i].pid = pinfo[i].p_pid; pt[i].ppid = pinfo[i].p_ppid; pt[i].starttime = pinfo[i].p_ustart_sec; pt[i].cputime = (long)((pinfo[i].p_rtime_sec * 10) + (pinfo[i].p_rtime_usec / 100000)); pt[i].cpu_percent = 0; pt[i].mem_kbyte = (unsigned long)(pinfo[i].p_vm_rssize * pagesize_kbyte); if (pinfo[i].p_stat == SZOMB) pt[i].status_flag |= PROCESS_ZOMBIE; //FIXME: save system service flag too (kernel threads) pt[i].time = get_float_time(); char **args; if ((args = kvm_getargv(kvm_handle, &pinfo[i], 0))) { StringBuffer_T cmdline = StringBuffer_create(64);; for (int j = 0; args[j]; j++) StringBuffer_append(cmdline, args[j + 1] ? "%s " : "%s", args[j]); pt[i].cmdline = Str_dup(StringBuffer_toString(StringBuffer_trim(cmdline))); StringBuffer_free(&cmdline); } if (! pt[i].cmdline || ! *pt[i].cmdline) pt[i].cmdline = Str_dup(pinfo[i].p_comm); } FREE(pinfo); kvm_close(kvm_handle); *reference = pt; return treesize; }
static void copy_mail(Mail_T n, Mail_T o) { ASSERT(n && o); n->to = Str_dup(o->to); n->from= o->from? Str_dup(o->from): Run.MailFormat.from? Str_dup(Run.MailFormat.from): Str_dup(ALERT_FROM); n->replyto = o->replyto? Str_dup(o->replyto): Run.MailFormat.replyto? Str_dup(Run.MailFormat.replyto): NULL; n->subject= o->subject? Str_dup(o->subject): Run.MailFormat.subject? Str_dup(Run.MailFormat.subject): Str_dup(ALERT_SUBJECT); n->message= o->message? Str_dup(o->message): Run.MailFormat.message? Str_dup(Run.MailFormat.message): Str_dup(ALERT_MESSAGE); }
/** * Read all processes to initialize the information tree. * @param reference reference of ProcessTree * @param pflags Process engine flags * @return treesize > 0 if succeeded otherwise 0 */ int initprocesstree_sysdep(ProcessTree_T **reference, ProcessEngine_Flags pflags) { size_t pinfo_size = 0; int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; if (sysctl(mib, 4, NULL, &pinfo_size, NULL, 0) < 0) { LogError("system statistic error -- sysctl failed: %s\n", STRERROR); return 0; } struct kinfo_proc *pinfo = CALLOC(1, pinfo_size); if (sysctl(mib, 4, pinfo, &pinfo_size, NULL, 0)) { FREE(pinfo); LogError("system statistic error -- sysctl failed: %s\n", STRERROR); return 0; } size_t treesize = pinfo_size / sizeof(struct kinfo_proc); ProcessTree_T *pt = CALLOC(sizeof(ProcessTree_T), treesize); char *args = NULL; StringBuffer_T cmdline = NULL; if (pflags & ProcessEngine_CollectCommandLine) { cmdline = StringBuffer_create(64); args = CALLOC(1, systeminfo.argmax + 1); } for (int i = 0; i < treesize; i++) { pt[i].uptime = systeminfo.time / 10. - pinfo[i].kp_proc.p_starttime.tv_sec; pt[i].zombie = pinfo[i].kp_proc.p_stat == SZOMB ? true : false; pt[i].pid = pinfo[i].kp_proc.p_pid; pt[i].ppid = pinfo[i].kp_eproc.e_ppid; pt[i].cred.uid = pinfo[i].kp_eproc.e_pcred.p_ruid; pt[i].cred.euid = pinfo[i].kp_eproc.e_ucred.cr_uid; pt[i].cred.gid = pinfo[i].kp_eproc.e_pcred.p_rgid; if (pflags & ProcessEngine_CollectCommandLine) { size_t size = systeminfo.argmax; mib[0] = CTL_KERN; mib[1] = KERN_PROCARGS2; mib[2] = pt[i].pid; if (sysctl(mib, 3, args, &size, NULL, 0) != -1) { /* KERN_PROCARGS2 sysctl() returns following pseudo structure: * struct { * int argc * char execname[]; * char argv[argc][]; * char env[][]; * } * The strings are terminated with '\0' and may have variable '\0' padding */ int argc = *args; char *p = args + sizeof(int); // arguments beginning StringBuffer_clear(cmdline); p += strlen(p); // skip exename while (argc && p < args + systeminfo.argmax) { if (*p == 0) { // skip terminating 0 and variable length 0 padding p++; continue; } StringBuffer_append(cmdline, argc-- ? "%s " : "%s", p); p += strlen(p); } if (StringBuffer_length(cmdline)) pt[i].cmdline = Str_dup(StringBuffer_toString(StringBuffer_trim(cmdline))); } if (! pt[i].cmdline || ! *pt[i].cmdline) { FREE(pt[i].cmdline); pt[i].cmdline = Str_dup(pinfo[i].kp_proc.p_comm); } } if (! pt[i].zombie) { struct proc_taskinfo tinfo; int rv = proc_pidinfo(pt[i].pid, PROC_PIDTASKINFO, 0, &tinfo, sizeof(tinfo)); // If the process is zombie, skip this if (rv <= 0) { if (errno != EPERM) DEBUG("proc_pidinfo for pid %d failed -- %s\n", pt[i].pid, STRERROR); } else if (rv < sizeof(tinfo)) { LogError("proc_pidinfo for pid %d -- invalid result size\n", pt[i].pid); } else { pt[i].memory.usage = (uint64_t)tinfo.pti_resident_size; pt[i].cpu.time = (double)(tinfo.pti_total_user + tinfo.pti_total_system) / 100000000.; // The time is in nanoseconds, we store it as 1/10s pt[i].threads = tinfo.pti_threadnum; } } } if (pflags & ProcessEngine_CollectCommandLine) { StringBuffer_free(&cmdline); FREE(args); } FREE(pinfo); *reference = pt; return (int)treesize; }
T URL_new(const char *url) { if (STR_UNDEF(url)) return NULL; Exception_init(); return ctor((uchar_t*)Str_dup(url)); }
/** * Read all processes to initialize the information tree. * @param reference reference of ProcessTree * @param pflags Process engine flags * @return treesize > 0 if succeeded otherwise 0 */ int initprocesstree_sysdep(ProcessTree_T **reference, ProcessEngine_Flags pflags) { size_t size = sizeof(maxslp); static int mib_maxslp[] = {CTL_VM, VM_MAXSLP}; if (sysctl(mib_maxslp, 2, &maxslp, &size, NULL, 0) < 0) { LogError("system statistic error -- vm.maxslp failed\n"); return 0; } int mib_proc2[6] = {CTL_KERN, KERN_PROC2, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 0}; if (sysctl(mib_proc2, 6, NULL, &size, NULL, 0) == -1) { LogError("system statistic error -- kern.proc2 #1 failed\n"); return 0; } size *= 2; // Add reserve for new processes which were created between calls of sysctl struct kinfo_proc2 *pinfo = CALLOC(1, size); mib_proc2[5] = (int)(size / sizeof(struct kinfo_proc2)); if (sysctl(mib_proc2, 6, pinfo, &size, NULL, 0) == -1) { FREE(pinfo); LogError("system statistic error -- kern.proc2 #2 failed\n"); return 0; } int treesize = (int)(size / sizeof(struct kinfo_proc2)); ProcessTree_T *pt = CALLOC(sizeof(ProcessTree_T), treesize); char buf[_POSIX2_LINE_MAX]; kvm_t *kvm_handle = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf); if (! kvm_handle) { FREE(pinfo); FREE(pt); LogError("system statistic error -- kvm_openfiles failed: %s\n", buf); return 0; } StringBuffer_T cmdline = NULL; if (pflags & ProcessEngine_CollectCommandLine) cmdline = StringBuffer_create(64); for (int i = 0; i < treesize; i++) { pt[i].pid = pinfo[i].p_pid; pt[i].ppid = pinfo[i].p_ppid; pt[i].cred.uid = pinfo[i].p_ruid; pt[i].cred.euid = pinfo[i].p_uid; pt[i].cred.gid = pinfo[i].p_rgid; pt[i].threads = pinfo[i].p_nlwps; pt[i].uptime = systeminfo.time / 10. - pinfo[i].p_ustart_sec; pt[i].cpu.time = pinfo[i].p_rtime_sec * 10 + (double)pinfo[i].p_rtime_usec / 100000.; pt[i].memory.usage = (uint64_t)pinfo[i].p_vm_rssize * (uint64_t)pagesize; pt[i].zombie = pinfo[i].p_stat == SZOMB ? true : false; if (pflags & ProcessEngine_CollectCommandLine) { char **args = kvm_getargv2(kvm_handle, &pinfo[i], 0); if (args) { StringBuffer_clear(cmdline); for (int j = 0; args[j]; j++) StringBuffer_append(cmdline, args[j + 1] ? "%s " : "%s", args[j]); if (StringBuffer_length(cmdline)) pt[i].cmdline = Str_dup(StringBuffer_toString(StringBuffer_trim(cmdline))); } if (! pt[i].cmdline || ! *pt[i].cmdline) { FREE(pt[i].cmdline); pt[i].cmdline = Str_dup(pinfo[i].p_comm); } } } if (pflags & ProcessEngine_CollectCommandLine) StringBuffer_free(&cmdline); FREE(pinfo); kvm_close(kvm_handle); *reference = pt; return treesize; }
void Command_appendArgument(T C, const char *argument) { assert(C); if (argument) List_append(C->args, Str_dup(argument)); FREE(C->_args); // Recreate Command argument on exec }
/** * Generic service test. * * @file */ int check_generic(Socket_T socket) { Generic_T g= NULL; char *buf; #ifdef HAVE_REGEX_H int regex_return; #endif ASSERT(socket); if(socket_get_Port(socket)) g = ((Port_T)(socket_get_Port(socket)))->generic; buf = CALLOC(sizeof(char), Run.expectbuffer + 1); while (g != NULL) { if (g->send != NULL) { /* Unescape any \0x00 escaped chars in g's send string to allow sending a string containing \0 bytes also */ char *X = Str_dup(g->send); int l = Util_handle0Escapes(X); if(socket_write(socket, X, l) < 0) { socket_setError(socket, "GENERIC: error sending data -- %s\n", STRERROR); FREE(X); FREE(buf); return FALSE; } else DEBUG("GENERIC: successfully sent: '%s'\n", g->send); FREE(X); } else if (g->expect != NULL) { int n; /* Need read, not readln here */ if((n= socket_read(socket, buf, Run.expectbuffer))<0) { socket_setError(socket, "GENERIC: error receiving data -- %s\n", STRERROR); FREE(buf); return FALSE; } buf[n]= 0; #ifdef HAVE_REGEX_H regex_return= regexec(g->expect, buf, 0, NULL, 0); if (regex_return != 0) { char e[STRLEN]; regerror(regex_return, g->expect, e, STRLEN); socket_setError(socket, "GENERIC: receiving unexpected data [%s] -- %s\n", Str_trunc(buf, STRLEN - 4), e); FREE(buf); return FALSE; } else DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4)); #else /* w/o regex support */ if (strncmp(buf, g->expect, strlen(g->expect)) != 0) { socket_setError(socket, "GENERIC: receiving unexpected data [%s]\n", Str_trunc(buf, STRLEN - 4)); FREE(buf); return FALSE; } else DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4)); #endif } else { /* This should not happen */ socket_setError(socket, "GENERIC: unexpected strangeness\n"); FREE(buf); return FALSE; } g= g->next; } FREE(buf); return TRUE; }