int shproc_child_poll(shproc_t *proc) { struct shproc_req_t req; struct timeval to; shbuf_t *sp_buf; fd_set in_set; int r_len; int err; int fd; err = shproc_read(proc); if (err == 1) return (0); /* nothing to do */ if (err) return (err); memset(&req, 0, sizeof(req)); /* spawned worker - data receieved as request */ sp_buf = shbuf_clone(proc->proc_buff); memset(&req, 0, sizeof(req)); req.state = SHPROC_RUN; shbuf_clear(proc->proc_buff); err = shproc_write(proc, &req); fd = 0; if (proc->user_fd) { fd = shproc_read_fd(proc); } err = 0; if (proc->proc_req) { err = (*proc->proc_req)(fd, sp_buf); /* user-result data */ shbuf_append(sp_buf, proc->proc_buff); } if (fd != 0) close(fd); shbuf_free(&sp_buf); memset(&req, 0, sizeof(req)); req.state = SHPROC_IDLE; req.error = err; err = shproc_write(proc, &req); proc->proc_idx++; shproc_state_set(proc, SHPROC_IDLE); shbuf_clear(proc->proc_buff); return (0); }
static int _test_shproc_req(int fd, shbuf_t *buff) { int val; if (!fd) { if (shbuf_size(buff) != sizeof(val)) { return (-1); } val = *((int *)shbuf_data(buff)); } else { if (shbuf_size(buff) != 0) { return (-1); } lseek(fd, 0L, SEEK_SET); read(fd, &val, sizeof(int)); close(fd); } _test_shproc_value[val] = -1; shbuf_clear(buff); shbuf_cat(buff, &val, sizeof(int)); return (0); }
int shproc_schedule(shproc_t *proc, unsigned char *data, size_t data_len) { shproc_req_t req; int err; if (!proc) return (SHERR_INVAL); shbuf_clear(proc->proc_buff); if (data && data_len) shbuf_cat(proc->proc_buff, data, data_len); memset(&req, 0, sizeof(req)); req.state = SHPROC_RUN; req.user_fd = proc->user_fd; err = shproc_write(proc, &req); if (err) { return (err); } if (proc->user_fd) shproc_write_fd(proc, proc->user_fd); /* set process to pending state */ shproc_state_set(proc, SHPROC_PEND); proc->stat.out_tot++; return (0); }
static int shproc_read(shproc_t *proc) { struct shproc_req_t req; struct timeval to; fd_set in_set; char buf[32768]; int r_len; int err; int of; err = shproc_read_wait(proc, child_proc ? 1000 : 1); memset(&req, 0, sizeof(req)); r_len = read(proc->proc_fd, &req, sizeof(req)); if (r_len == -1 && errno != EAGAIN) { return (errno2sherr()); } if (r_len != sizeof(req)) return (1); /* nothing to read */ if (!child_proc) { /* parent process */ if (req.state == SHPROC_RUN || req.state == SHPROC_IDLE) { shproc_state_set(proc, req.state); } proc->stat.in_tot++; if (req.state == SHPROC_IDLE) { proc->user_fd = 0; } } else { proc->user_fd = req.user_fd; } of = 0; shbuf_clear(proc->proc_buff); for (of = 0; of < req.data_len; of += r_len) { r_len = read(proc->proc_fd, buf, MIN(req.data_len-of, sizeof(buf))); if (r_len == -1) return (errno2sherr()); if (r_len == 0) return (SHERR_AGAIN); shbuf_cat(proc->proc_buff, buf, r_len); } proc->proc_error = req.error; return (0); }
int shlog(int level, int err_code, char *log_str) { static time_t last_day; static time_t last_flush; static shbuf_t *buff; time_t day; time_t now; int err; if (level < _log_level) return (0); now = time(NULL); day = now / 86400; if (day != last_day) { // shlog_zcompr(); /* compress .YY.WW bin log file, removing prev zip */ shlog_free(); } last_day = day; if (!buff) buff = shbuf_init(); { shbuf_lock(buff); shbuf_clear(buff); shlog_write(buff, level, err_code, log_str); if (shbuf_data(buff) && _shlog_file) { fprintf(_shlog_file, "%s", shbuf_data(buff)); if (last_flush < (now - MAX_FLUSH_SPAN)) { fflush(_shlog_file); last_flush = now; } } shbuf_unlock(buff); } return (0); }
/** * Generates the working-copy for a particular revision. */ int shfs_rev_read(shfs_ino_t *i_rev, shbuf_t *buff) { shfs_ino_t *repo; shfs_ino_t *file; shfs_ino_t *rev; shbuf_t *delta_buff; shbuf_t *head_buff; shbuf_t *out_buff; int err; if (shfs_type(i_rev) != SHINODE_REVISION) { return (SHERR_INVAL); } repo = shfs_inode_parent(i_rev); if (!repo || shfs_type(repo) != SHINODE_REPOSITORY) return (SHERR_IO); file = shfs_inode_parent(repo); if (!file || shfs_type(file) != SHINODE_FILE) return (SHERR_IO); head_buff = shbuf_init(); err = shfs_rev_ref_read(file, "tag", "BASE", head_buff); if (err) return (err); err = SHERR_NOENT; /* ret'd when no matching revision found */ out_buff = shbuf_init(); delta_buff = shbuf_init(); /* search BASE chain for revision -- applying each revision's patch. */ rev = shfs_rev_tag_resolve(file, "BASE"); while (rev) { if (shkey_cmp(shfs_token(rev), shfs_token(i_rev))) { /* found revision in branch chain. */ shbuf_append(head_buff, buff); err = 0; break; } shbuf_clear(delta_buff); err = shfs_rev_delta_read(rev, delta_buff); if (err) break; /* DEBUG: TODO: merge together deltas to reduce performance over-head */ shbuf_clear(out_buff); err = shpatch(head_buff, delta_buff, out_buff); if (err) break; shbuf_clear(head_buff); shbuf_append(out_buff, head_buff); rev = shfs_rev_prev(rev); } shbuf_free(&head_buff); shbuf_free(&out_buff); shbuf_free(&delta_buff); return (err); }
int sharelog_tail(shpeer_t *peer) { shbuf_t *buff; fd_set read_fd; shjson_t *json; char tbuf[256]; time_t stime, etime; time_t now; char *str; int err; int fd; fd = shconnect_host("127.0.0.1", PROCESS_PORT, SHNET_ASYNC); if (fd < 0) return (fd); json = shjson_init(NULL); shjson_num_add(json, "id", 1); shjson_str_add(json, "method", "log.subscribe"); shjson_str_add(json, "key", (char *)shkey_print(shpeer_kpub(peer))); shjson_null_add(json, "params"); str = shjson_print(json); shjson_free(&json); err = shnet_write(fd, str, strlen(str)); free(str); if (err < 0) { shclose(fd); return (err); } err = shnet_write(fd, "\n", 1); if (err < 0) { shclose(fd); return (err); } while (proc_mode == RUN_TAIL) { FD_ZERO(&read_fd); FD_SET(fd, &read_fd); err = shnet_verify(&read_fd, NULL, NULL); if (err < 0) { continue; } buff = shnet_read_buf(fd); if (!buff || shbuf_size(buff) == 0) continue; json = shjson_init(shbuf_data(buff)); if (json) { char *text = shjson_astr(json, "result", NULL); if (text) { now = time(NULL); strftime(tbuf, sizeof(tbuf) - 1, "%D %T", localtime(&now)); printf("[%s] %s", tbuf, text); } } shbuf_clear(buff); } shclose(fd); return (0); }