char *increField(char *str) { char *temp = NULL; char field[20] = {'\0'}; int value = 0; char value_str[10] = {'\0'}; char *return_str = NULL; temp = memmem(str, strlen(str), ":", 1); if(temp != NULL){ memcpy(field, str, (temp-str)+1); value = atoi(temp+1); //printf("==%d\n", value); value++; //printf("==%d\n", value); sprintf(value_str, "%d", value); return_str = (char *)malloc(sizeof(char) * strlen(str)+1); memset(return_str,'\0', strlen(str)+1); strcpy(return_str, field); strcat(return_str, value_str); strcat(return_str, "\n"); return return_str; } }
bool w_string_contains_cstr_len( const w_string_t* str, const char* needle, uint32_t nlen) { #if HAVE_MEMMEM return memmem(str->buf, str->len, needle, nlen) != NULL; #else // Most likely only for Windows. // Inspired by http://stackoverflow.com/a/24000056/149111 const char *haystack = str->buf; uint32_t hlen = str->len; const char *limit; if (nlen == 0 || hlen < nlen) { return false; } limit = haystack + hlen - nlen + 1; while ((haystack = (const char*)memchr( haystack, needle[0], limit - haystack)) != NULL) { if (memcmp(haystack, needle, nlen) == 0) { return true; } haystack++; } return false; #endif }
int urlCheck(char *url,char *host){ UPU *tmp; if(memmem(url,strlen(url),host,strlen(host))==NULL) return 0; if(memcmp(url,"mail",4)==0) return 0; if(find(failedPool,url,strlen(url))!=NULL) return 0; if(find(URLPool,url,strlen(url))!=NULL) return 0; if(find(finishPool,url,strlen(url))!=NULL) return 0; if(CURL_typeTest(url)){ return 1; }else{ tmp=failedPool[hashfn(url)]; if(tmp!=NULL){ while(tmp->next!=NULL){ tmp=tmp->next; } myAdd(&(tmp->next),url,strlen(url)); }else{ myAdd(&(failedPool[hashfn(url)]),url,strlen(url)); } return 0; } }
/* returns HTTP code or -1 if error occured, blocking function * returns: * > 0 - http response bytes in buffer * -1 - error writing request * -2 - timeout waiting for response * -3 - incomplete/bad headers */ int http_handshake(int sock, const void *hdr, unsigned hdr_len, pstr_t *data) { int bytes, code, i; char *p; char buf[4096]; if (write_full_ms(sock, hdr, hdr_len, 1000) != hdr_len) return -1; bytes = read_ms(sock, buf, sizeof(buf), 5000); if (bytes < 12 /* minimal headers length */ || memcmp(buf, "HTTP/1.", 7) != 0 || (buf[7] != '0' && buf[7] != '1') || buf[8] != 0x20 || !isdigit(buf[9])) return -2; /* read response code */ code = 0; for (i = 9; i < bytes && isdigit(buf[i]); i++) code = code * 10 + (buf[i] - '0'); /* find end-of-headers */ p = memmem(buf, sizeof(buf), "\r\n\r\n", 4); if (p == NULL) return -3; data->len = buf + bytes - (p + 4); memcpy(data->data, buf, data->len); return code; }
int main(int argc, char **argv) { if(argc != 3) return usage(argv[0]); const char* file = argv[1], *term = argv[2]; unsigned char *dblbuf = calloc(1, BLOCKSIZE * 2); unsigned char *readbuf = dblbuf + BLOCKSIZE; size_t termlen = strlen(term); unsigned char *searchbuf = readbuf - termlen; unsigned char *copybuf = readbuf + BLOCKSIZE - termlen; int fd = open(file, O_RDONLY), success=0; if(fd == -1) { perror("open"); return 1; } signal(SIGALRM, sigh); alarm(ATIME); while(1) { ssize_t n = read(fd, readbuf, BLOCKSIZE); if(n <= 0) break; void* res = memmem(searchbuf, BLOCKSIZE+termlen, term, termlen); if(res) { if(neednl) dprintf(2, "\n"); neednl = 0; dprintf(1, "bingo: 0x%llx\n", (unsigned long long) offset + ((uintptr_t) res - (uintptr_t)readbuf)); fflush(stdout); success = 1; } memcpy(searchbuf, copybuf, termlen); offset += n; } return !success; }
static void scan_memory(int i, unsigned char *p, off_t size) { unsigned char *p0 = p; int found = 0; while ((p = memmem(p, size - (p-p0), GZIP_HEADER, strlen(GZIP_HEADER)))) { char *fname=NULL; if (p[8] > 4) { p++; continue; } /* only want unix gzip output */ if (p[9] > 11) { p++; continue; } found++; asprintf(&fname, "extract-%d-%d.gz", i, found); file_save(fname, p, size - (p-p0)); printf("Extracted %d bytes to %s\n", (int)(size - (p-p0)), fname); free(fname); p++; } }
static void* find_ccs_search_binary_handler_address_in_ccsecurity_ops(void *mmap_base_address) { unsigned long int ccsecurity_ops = 0; unsigned long int *ccs_search_binary_handlers; int i = 0; void *mapped_ccsecurity_ops; void *found_address = NULL; ccsecurity_ops = kallsyms_in_memory_lookup_name("ccsecurity_ops"); if (!ccsecurity_ops) { return NULL; } ccs_search_binary_handlers = kallsyms_in_memory_lookup_names("__ccs_search_binary_handler"); if (!ccs_search_binary_handlers) { return NULL; } mapped_ccsecurity_ops = fb_mem_convert_to_mmaped_address((void*)ccsecurity_ops, mmap_base_address); while (ccs_search_binary_handlers[i]) { found_address = memmem(mapped_ccsecurity_ops, 0x100, &ccs_search_binary_handlers[i], sizeof(ccs_search_binary_handlers[i])); if (found_address) { break; } i++; } free(ccs_search_binary_handlers); return found_address; }
JNIEXPORT jlong JNICALL Java_jxtn_core_unix_NativeString_memmem(JNIEnv *env, jclass thisObj, jlong haystack, jlong haystacklen, jbyteArray needle) { if (haystack == 0L || needle == 0L) return SETERR(EFAULT); size_t needlelen = (size_t) (*env)->GetArrayLength(env, needle); return (jlong) memmem((void*) haystack, UL(haystacklen), resolveBA(needle), needlelen); }
tree_cell* nasl_strstr(lex_ctxt * lexic) { char * a = get_str_var_by_num(lexic, 0); char * b = get_str_var_by_num(lexic, 1); int sz_a = get_var_size_by_num(lexic, 0); int sz_b = get_var_size_by_num(lexic, 1); char * c; tree_cell * retc; if(a == NULL || b == NULL) return NULL; if(sz_b > sz_a) return NULL; c = (char*)memmem(a, sz_a, b, sz_b); if(c == NULL) return FAKE_CELL; retc = alloc_tree_cell(0, NULL); retc->type = CONST_DATA; retc->size = sz_a - (c - a); retc->x.str_val = strndup(c, retc->size); return retc; }
static int fixmatch(struct grep_pat *p, char *line, char *eol, regmatch_t *match) { char *hit; if (p->ignore_case) { char *s = line; do { hit = strcasestr(s, p->pattern); if (hit) break; s += strlen(s) + 1; } while (s < eol); } else hit = memmem(line, eol - line, p->pattern, p->patternlen); if (!hit) { match->rm_so = match->rm_eo = -1; return REG_NOMATCH; } else { match->rm_so = hit - line; match->rm_eo = match->rm_so + p->patternlen; return 0; } }
/* * data specific functions */ static int data_compare(const idmef_value_type_t *t1, const idmef_value_type_t *t2, size_t len, idmef_criterion_operator_t op) { int ret; size_t s1_len, s2_len; const void *s1 = NULL, *s2 = NULL; if ( t1->data.data_val ) s1 = idmef_data_get_data(t1->data.data_val); if ( t2->data.string_val ) s2 = idmef_data_get_data(t2->data.data_val); if ( ! s1 || ! s2 ) return (s1) ? 1 : -1; if ( op & IDMEF_CRITERION_OPERATOR_SUBSTR ) { s1_len = idmef_data_get_len(t1->data.data_val); s2_len = idmef_data_get_len(t2->data.data_val); return ( memmem(s1, s1_len, s2, s2_len) ) ? 0 : -1; } ret = idmef_data_compare(t1->data.data_val, t2->data.data_val); if ( ret == 0 && op & IDMEF_CRITERION_OPERATOR_EQUAL ) return 0; else if ( ret < 0 && op & IDMEF_CRITERION_OPERATOR_LESSER ) return 0; else if ( ret > 0 && op & IDMEF_CRITERION_OPERATOR_GREATER ) return 0; return -1; }
static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) { unsigned long size; enum object_type type; char *buf = NULL, *line, *lineend; unsigned long date; int result = 1; if (revs->max_age == -1 && revs->min_age == -1) goto out; buf = read_sha1_file(tag->oid.hash, &type, &size); if (!buf) goto out; line = memmem(buf, size, "\ntagger ", 8); if (!line++) goto out; lineend = memchr(line, '\n', buf + size - line); line = memchr(line, '>', lineend ? lineend - line : buf + size - line); if (!line++) goto out; date = strtoul(line, NULL, 10); result = (revs->max_age == -1 || revs->max_age < date) && (revs->min_age == -1 || revs->min_age > date); out: free(buf); return result; }
// Removes any dependencies a module might have by overwriting the 'depends=' // string in .modinfo with zeroes. ELF_ERROR elf_clean_dependencies(ELF_OBJ *obj) { Elf_Shdr *modinfo; uint8_t *modinfo_content; char *dependencies; size_t dependencies_len; const char *dep_prefix = "depends="; size_t dep_prefix_len = strlen(dep_prefix); Elf_Word modinfo_idx; if (obj->section_by_name(obj, ".modinfo", &modinfo_idx, &modinfo) != ELF_SUCCESS) { log_print(LL_ERR, "No need to clean dependencies, .modinfo does not exist"); return ELF_SUCCESS; } if (obj->section_get_contents(obj, modinfo, &modinfo_content) != ELF_SUCCESS) { log_print(LL_ERR, "Failed to get .modinfo contents, still it exists"); return ELF_FAILURE; } if ((dependencies = (char *)memmem(modinfo_content, modinfo->sh_size, dep_prefix, dep_prefix_len)) == NULL) { log_print(LL_LOG, "Module has no dependencies, no need to clean up"); return ELF_SUCCESS; } dependencies_len = strlen(dependencies); log_print(LL_DBG, "Cleaning dependency string %s of len %d", dependencies, dependencies_len); memset(dependencies, 0x00, dependencies_len); return ELF_SUCCESS; }
void extract_arc_files_main(void) { arcfile_files_extracted = 0; arcfile_understood = 0; arcfile_offs = 0; /* RESET */ uint32_t test0 = ru32(); if (test0 == 0x101F4667) { mdie("this format not yet implemented"); } else { const char pathStartSeq[5] = { 0, ':', 0, '\\', 0 }; const uint8_t *dp; reset_parse: if ((dp=memmem(arcfile_data+arcfile_offs,arcfile_size-arcfile_offs, pathStartSeq, 5))) { arcfile_offs = dp - (uint8_t*)arcfile_data; arcfile_offs -= 2; while (1) { if (extract_arc_file_try()) { if (arcfile_offs>=arcfile_size) { break; /* End Of File */ } else { goto reset_parse; } } } } } }
int main() { struct stat st; int r = stat ("source", &st); if (r < 0) return 1; char* buf = malloc (st.st_size); FILE* source = fopen ("source", "rb"); assert (source && !ferror (source)); size_t cnt = fread (buf, 1, st.st_size, source); if (cnt != st.st_size) return 1; fclose (source); void* to_patch = memmem (buf, st.st_size, signature, sizeof (signature)); if (!to_patch) return 1; memcpy (to_patch, replace, sizeof (replace)); FILE* cracked = fopen ("cracked", "wb"); assert (cracked && !ferror (cracked)); cnt = fwrite (buf, 1, st.st_size, cracked); if (cnt != st.st_size) return 1; fclose (cracked); free (buf); return 0; }
tree_cell* nasl_stridx(lex_ctxt * lexic) { char *a = get_str_var_by_num(lexic, 0); int sz_a = get_var_size_by_num(lexic, 0); char *b = get_str_var_by_num(lexic, 1); int sz_b = get_var_size_by_num(lexic, 1); char *c; int start = get_int_var_by_num(lexic, 2, 0); tree_cell *retc = alloc_typed_cell(CONST_INT); retc->x.i_val = -1; if (a == NULL || b == NULL) { nasl_perror(lexic, "stridx(string, substring [, start])\n"); return retc; } if(start < 0 || start > sz_a) { nasl_perror(lexic, "stridx(string, substring [, start])\n"); return retc; } if ((sz_a == start) || (sz_b > sz_a + start)) return retc; c = (char*)memmem(a + start, sz_a - start, b, sz_b); if(c != NULL) retc->x.i_val = c - a; return retc; }
kern_return_t fake_IOConnectCallStructMethod( mach_port_t connection, // In uint32_t selector, // In const void *inputStruct, // In size_t inputStructCnt, // In void *outputStruct, // Out size_t *outputStructCnt) // In/Out { if (selector == 2){ if (token_buf != 0){ uint16_t needle = 0x8500; // BlitFramebuffer token uint32_t* at = memmem(token_buf, 0x1000, &needle, 2); if (at){ // overwrite the output offset(??) field with a large value // a value a few bytes less than this will be rax at the crash: // mov [rcx + 4*rax] = 0x5000000 at[1] = 0xffff01; } } } return IOConnectCallStructMethod( connection, // In selector, // In inputStruct, // In inputStructCnt, // In outputStruct, // Out outputStructCnt); // In/Out }
static void format_subst(const struct commit *commit, const char *src, size_t len, struct strbuf *buf) { char *to_free = NULL; struct strbuf fmt = STRBUF_INIT; if (src == buf->buf) to_free = strbuf_detach(buf, NULL); for (;;) { const char *b, *c; b = memmem(src, len, "$Format:", 8); if (!b) break; c = memchr(b + 8, '$', (src + len) - b - 8); if (!c) break; strbuf_reset(&fmt); strbuf_add(&fmt, b + 8, c - b - 8); strbuf_add(buf, src, b - src); format_commit_message(commit, fmt.buf, buf, DATE_NORMAL); len -= c + 1 - src; src = c + 1; } strbuf_add(buf, src, len); strbuf_release(&fmt); free(to_free); }
static bool run_sfilter(const struct sfilter *sf, const struct buf *buf) { int i = 0; char *pos = NULL; size_t left = 0; char *match = NULL; size_t match_len = 0; pos = buf->data; left = buf->len; for (i = 0; i < sf->num_matches; ++i) { match = sf->matches[i]; match_len = strlen(match); pos = memmem(pos, left, match, match_len); if (!pos) { return false; } pos += match_len; left -= match_len; } print_sfilter(sf); // for debugging return true; }
static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) { unsigned long size; enum object_type type; char *buf, *line, *lineend; unsigned long date; if (revs->max_age == -1 && revs->min_age == -1) return 1; buf = read_sha1_file(tag->sha1, &type, &size); if (!buf) return 1; line = memmem(buf, size, "\ntagger ", 8); if (!line++) return 1; lineend = memchr(line, buf + size - line, '\n'); line = memchr(line, lineend ? lineend - line : buf + size - line, '>'); if (!line++) return 1; date = strtoul(line, NULL, 10); free(buf); return (revs->max_age == -1 || revs->max_age < date) && (revs->min_age == -1 || revs->min_age > date); }
static sphtimer_t get_cpu_freq_internal (void) { sphtimer_t result = 0L; char buf[1024]; ssize_t cnt; ssize_t hcnt = sizeof (buf) / 2; int fd; /* Is this annoying! There are functions inside glibc that do this and the PPC VDSO also provide direct access to the value we need, but they are hidden (not plublic) and we can't get to either. So we are forced to read /proc/cpuinfo. */ fd = open ("/proc/cpuinfo", O_RDONLY); if (fd != -1) { /* Multi-core processors are getting scary big and this means that /prop/cpuinfo can also get very large. The cpuinfo for a full blown P7 795 is over 90KB. The bad news is the info we are after is at the end of the file (following the enumeration of the processors). We we will double buffer 512 byte chunks until we get to the end of the file. */ cnt = read (fd, buf, sizeof (buf)); if (cnt == sizeof (buf)) { /* did not get lucky, reading the whole cpuinfo in the 1st IO. */ do { /* move the bottom half up */ memcpy (buf, buf + hcnt, hcnt); /* read the next block */ cnt = read (fd, buf + hcnt, hcnt); } while (cnt == hcnt); cnt += hcnt; } if (cnt > 0) { char *tb_str = (char *) memmem (buf, cnt, "timebase", 7); char *bufmax = buf + cnt; if (tb_str != NULL) { while (tb_str < bufmax) { if (*tb_str >= '0' && (*tb_str <= '9' || *tb_str == '\n')) break; else tb_str++; } if (tb_str < bufmax) result = strtoll (tb_str, NULL, 10); } } } return result; }
static void format_subst(const struct commit *commit, const char *src, size_t len, struct strbuf *buf) { char *to_free = NULL; struct strbuf fmt = STRBUF_INIT; struct pretty_print_context ctx = {0}; ctx.date_mode.type = DATE_NORMAL; ctx.abbrev = DEFAULT_ABBREV; if (src == buf->buf) to_free = strbuf_detach(buf, NULL); for (;;) { const char *b, *c; b = memmem(src, len, "$Format:", 8); if (!b) break; c = memchr(b + 8, '$', (src + len) - b - 8); if (!c) break; strbuf_reset(&fmt); strbuf_add(&fmt, b + 8, c - b - 8); strbuf_add(buf, src, b - src); format_commit_message(commit, fmt.buf, buf, &ctx); len -= c + 1 - src; src = c + 1; } strbuf_add(buf, src, len); strbuf_release(&fmt); free(to_free); }
static enum proto_parse_status tns_parse_sql_query_oci(struct sql_proto_info *info, struct cursor *cursor) { SLOG(LOG_DEBUG, "Parsing an oci query"); enum proto_parse_status status; char pattern[] = {0xfe, MAX_OCI_CHUNK}; uint8_t const *new_head = memmem(cursor->head, cursor->cap_len, pattern, sizeof(pattern)); if (new_head != NULL) { size_t gap_size = new_head - cursor->head; SLOG(LOG_DEBUG, "%zu bytes before sql", gap_size); DROP_FIX(cursor, gap_size + 1); } else { SLOG(LOG_DEBUG, "{0xfe 0x40} not found, size might be < 0x40"); if (!lookup_query(cursor)) return PROTO_PARSE_ERR; } char *sql; if (PROTO_OK != (status = cursor_read_chunked_string(cursor, &sql, MAX_OCI_CHUNK))) return status; SLOG(LOG_DEBUG, "Sql parsed: %s", sql); sql_set_query(info, "%s", sql); // Drop the rest if(cursor->cap_len > 0) cursor_drop(cursor, cursor->cap_len - 1); return PROTO_OK; }
void *run_chunk(void *data) { char *line; char *next; size_t linelen; size_t offset; chunk_t *chunk = (chunk_t *)data; if (opt.debug) { fprintf(stderr, "DBG: %ld, %ld\n", (long int)(chunk->start - map), (long int)chunk->len); } line = chunk->start; offset = 0; while (offset < chunk->len) { next = strchr(line, '\n'); if(!next) { break; } /* Add 1 to linelen to include newline */ linelen = next - line + 1; /* Print line if it contains needle */ if (memmem(line, linelen - 1, opt.needle, opt.needlen)) { /* and does *not* contain vstring (if present) */ if ((!opt.vstring) || (!memmem(line, linelen - 1, opt.vstring, opt.vlen))) { /* Not necessary to lock mutex for printf or fputs * in threads. Stdio functions use teir own locking. But * for the unlocked counterparts (like fputs_unlocked) * or something low-level, like write(), it *is* needed. * In that case, uncomment the pthread_mutex_lock/unlock * lines below. */ /* pthread_mutex_lock(&print_mutex); */ /* print linelen bytes of line to stdout */ printf("%.*s", (int)linelen, line); /* pthread_mutex_unlock(&print_mutex); */ } } offset += linelen; line = next + 1; } return NULL; }
char *read_multi_attribute(char *bufstart, char *bufend, char *element) { char start_element[GM_BUFFERSIZE], end_element[GM_BUFFERSIZE]; sprintf(start_element, "<%s>", element); sprintf(end_element, "</%s>", element); if ((bufstart=(char *)memmem(bufstart,bufend-bufstart,start_element,strlen(start_element))) == NULL) { gm_log( GM_LOG_TRACE, "\tread_multi_attribute: start element \'%s\' not found\n", start_element); return NULL; } bufstart+=strlen(start_element); if ((bufend=(char *)memmem(bufstart,bufend-bufstart,end_element,strlen(end_element))) == NULL) { gm_log( GM_LOG_TRACE, "\tread_multi_attribute: end element \'%s\' not found\n", end_element); return NULL; } *bufend='\0'; return bufstart; }
bool check_if_repeat(char *lib) { if (memmem(all_libs, ALL_LIBS_SIZE, lib, strlen(lib))) { /* printf("skipping %s!!\n", lib); */ return true; } return false; }
/* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */ static int get_file_version(int fd, char **v) { struct stat st; char *buf; const char *s, *e; char *x = NULL; int r = 0; assert(fd >= 0); assert(v); if (fstat(fd, &st) < 0) return log_error_errno(errno, "Failed to stat EFI binary: %m"); if (st.st_size < 27) { *v = NULL; return 0; } buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (buf == MAP_FAILED) return log_error_errno(errno, "Failed to memory map EFI binary: %m"); s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17); if (!s) goto finish; s += 17; e = memmem(s, st.st_size - (s - buf), " ####", 5); if (!e || e - s < 3) { log_error("Malformed version string."); r = -EINVAL; goto finish; } x = strndup(s, e - s); if (!x) { r = log_oom(); goto finish; } r = 1; finish: (void) munmap(buf, st.st_size); *v = x; return r; }
int jincheng_h3c_record_rtsp_request(pvp_uthttp put, char **ut_buf, u32 *pack_len) { const static char FLG_RCV_IP[] = "destination="; const static char FLG_RCV_PORT[] = "client_port="; char cli_ip[16] = {0}; char cli_port[16] = {0}; char r_src[64] = {0}; char r_dst[64] = {0}; char *ptr = NULL; u16 lport = 0; if ((ptr = (char*)memmem(*ut_buf, *pack_len, FLG_RCV_IP, sizeof(FLG_RCV_IP)-1)) == NULL) return 1; ptr += sizeof(FLG_RCV_IP)-1; sscanf(ptr, "%[^;]", cli_ip); if ((ptr = (char*)memmem(ptr, *pack_len - (ptr - *ut_buf), FLG_RCV_PORT, sizeof(FLG_RCV_PORT)-1)) == NULL) return 1; ptr += sizeof(FLG_RCV_PORT)-1; sscanf(ptr, "%[^-]", cli_port); // get an idle port from pool if ((lport = pplist_getidle_port()) <= 0) { logdbg_out("jincheng_h3c_record_rtsp_request: get idle port failed!"); return -1; } // replace client ip to ums ip sprintf(r_src, "%s%s", FLG_RCV_IP, cli_ip); sprintf(r_dst, "%s%s", FLG_RCV_IP, g_ums_ip); strreplace_pos(NULL,NULL, ut_buf, r_src,r_dst, -1, pack_len); // replace client port to ums port sprintf(r_src, "%s%s", FLG_RCV_PORT, cli_port); sprintf(r_dst, "%s%d", FLG_RCV_PORT, lport); strreplace_pos(NULL,NULL, ut_buf, r_src,r_dst, -1, pack_len); printf("start record proxy\n"); printf("laddr: %s:%d\n", g_ums_ip, lport); printf("daddr: %s:%s\n", cli_ip, cli_port); // start proxy run_vs_udp_proxy(__gg.outer_addr, inet_atoul(cli_ip), lport, atoi(cli_port), 0, put->session_tout, __gg.ferry_port); return 1; }
static enum proto_parse_status cursor_drop_until(struct cursor *cursor, const void *marker, size_t marker_len) { uint8_t *new_head = memmem(cursor->head, cursor->cap_len, marker, marker_len); if (!new_head) return PROTO_PARSE_ERR; size_t gap_size = new_head - cursor->head; cursor_drop(cursor, gap_size); return PROTO_OK; }
/* This is called if conn->console_sock becomes ready to read while we * are doing one of the connection operations above. It reads and * deals with the log message. * * Returns: * 1 = log message(s) were handled successfully * 0 = connection to appliance closed * -1 = error */ static int handle_log_message (guestfs_h *g, struct connection_socket *conn) { char buf[BUFSIZ]; ssize_t n; /* Carried over from ancient proto.c code. The comment there was: * * "QEMU's console emulates a 16550A serial port. The real 16550A * device has a small FIFO buffer (16 bytes) which means here we * see lots of small reads of 1-16 bytes in length, usually single * bytes. Sleeping here for a very brief period groups reads * together (so we usually get a few lines of output at once) and * improves overall throughput, as well as making the event * interface a bit more sane for callers. With a virtio-serial * based console (not yet implemented) we may be able to remove * this. XXX" */ usleep (1000); n = read (conn->console_sock, buf, sizeof buf); if (n == 0) return 0; if (n == -1) { if (errno == EINTR || errno == EAGAIN) return 1; /* not an error */ perrorf (g, _("error reading console messages from the appliance")); return -1; } /* It's an actual log message, send it upwards. */ guestfs_int_log_message_callback (g, buf, n); #ifdef VALGRIND_DAEMON /* Find the canary printed by appliance/init if valgrinding of the * daemon fails, and exit abruptly. Note this is only used in * developer builds, and should never be enabled in ordinary/ * production builds. */ if (g->verbose) { const char *valgrind_canary = "DAEMON VALGRIND FAILED"; if (memmem (buf, n, valgrind_canary, strlen (valgrind_canary)) != NULL) { fprintf (stderr, "Detected valgrind failure in the daemon! Exiting with exit code 119.\n" "See log messages printed above.\n" "Note: This happens because libguestfs was configured with\n" "'--enable-valgrind-daemon' which should not be used in production builds.\n"); exit (119); } } #endif return 1; }