static int io_read_cachefile(const char *filename, const char *type, void **buffer, unsigned long *buffersize) { unsigned long filesize; IO_HANDLE fp; /* open file */ fp = io_open_read(filename); if(!io_valid(fp)) return 0; /* read the whole file */ filesize = io_size(fp); *buffer = malloc(filesize); *buffersize = io_read(fp, *buffer, filesize); io_close(fp); /* verify read and header */ cache_setup_header(type); if( *buffersize != filesize || filesize < sizeof(bamheader) || memcmp(*buffer, bamheader, sizeof(bamheader)) != 0) { printf("%s: warning: cache file '%s' is invalid, generating new one\n", session.name, filename); free(*buffer); *buffer = NULL; return 0; } return 1; }
void io_get_file_contents (const char *path, void (*on_file_read)(void *, unsigned int, void *), void *aux) { struct io *io = io_open_read (path); struct memory_pool pool = MEMORY_POOL_INITIALISER (sizeof(struct io_get_file_data)); struct io_get_file_data *d = get_pool_mem (&pool); d->on_file_read = on_file_read; d->aux = aux; multiplex_add_io (io, mx_read, mx_close, (void *)d); }
static sexpr include (sexpr arguments, struct machine_state *st) { sexpr env = st->environment; define_string (str_slash, "/"); sexpr e = env, to = car (arguments), t = sx_join (webroot, str_slash, to), data = sx_end_of_list, lang, lcodes, te, tf, type = sx_nonexistent, lcc; struct sexpr_io *io; int len = 0, i = 0; const char *ts = sx_string (to); char *tmp; if (nexp (lx_environment_lookup (e, sym_original_name))) { e = lx_environment_bind (e, sym_original_name, car (arguments)); } if (truep (filep (t))) { return cons (e, cons (cons (sym_verbatim, cons (t, sx_end_of_list)), sx_end_of_list)); } te = sx_join (to, str_dot_ksu, str_nil); lcodes = (lx_environment_lookup (e, sym_language)); lcodes = sx_reverse (lcodes); lcodes = cons (str_dot, lcodes); lcodes = sx_reverse (lcodes); lcc = lcodes; while (consp (lcc)) { lang = car (lcc); t = sx_join (webroot, str_slash, sx_join (lang, str_slash, te)); if (truep (filep (t))) { sexpr v = lx_environment_lookup (e, sym_Vary); tf = lx_environment_lookup (e, sym_accept); e = lx_environment_bind (e, sym_base_name, to); if (!nexp (v)) { e = lx_environment_unbind (e, sym_Vary); e = lx_environment_bind (e, sym_Vary, sx_join (v, str_cAccept, sx_end_of_list)); } else { e = lx_environment_bind (e, sym_Vary, str_Accept); } if (!nexp (tf)) { tf = get_acceptable_type (tf); } else { tf = default_type; } goto include; } lcc = cdr (lcc); } while (ts[len] != (char)0) { if (ts[len] == '.') i = len; len++; } if (i > 0) { len = i; tmp = aalloc (len + 1); for (i = 0; i < len; i++) { tmp[i] = ts[i]; } tmp[i] = 0; i++; te = make_string (tmp); type = make_string (ts + i); afree (i, tmp); e = lx_environment_bind (e, sym_base_name, te); e = lx_environment_bind (e, sym_extension, type); te = sx_join (te, str_dot_ksu, str_nil); while (consp (lcodes)) { lang = car (lcodes); t = sx_join (webroot, str_slash, sx_join (lang, str_slash, te)); if (truep (filep (t))) { tf = lx_environment_lookup(mime_map, type); include: if (!nexp (tf)) { struct transdata td = { lx_environment_join (lx_environment_join (kho_environment, env), e), &data, 0 }; e = lx_environment_bind (e, sym_format, tf); io = sx_open_i (io_open_read (sx_string (t))); multiplex_add_sexpr (io, include_on_read, &td); do { multiplex (); } while (td.done == 0); return cons (e, sx_reverse (data)); } else { return cons (e, cons (cons (sym_object, cons (sym_verbatim, cons (t, sx_end_of_list))), sx_end_of_list)); } } lcodes = cdr (lcodes); } } if (!nexp (lx_environment_lookup (e, sym_error))) { return cons (sym_object, cons (cons (sym_error, cons (sym_file_not_found, sx_end_of_list)), sx_end_of_list)); } else { return sx_nonexistent; } }
int cmain() { int i; struct dfs *fs; char use_stdio = 0; char mount_self = 0; char *use_socket = (char *)0; char next_socket = 0; char had_rules_file = 0; char initialise_common = 0; char o_foreground = 0; // terminate_on_allocation_errors(); multiplex_io(); dfs_update_ids(); multiplex_sexpr(); for (i = 1; curie_argv[i]; i++) { if (curie_argv[i][0] == '-') { int j; for (j = 1; curie_argv[i][j] != (char)0; j++) { switch (curie_argv[i][j]) { case 'o': use_stdio = 1; break; case 'p': sys_mount ("proc", "/proc", "proc", 0, (char *)0); sys_mount ("sys", "/sys", "sysfs", 0, (char *)0); break; case 'i': initialise_common = 1; break; case 'm': mount_self = 1; break; case 's': next_socket = 1; break; case 'f': o_foreground = 1; break; default: print_help(); } } continue; } if (next_socket) { use_socket = curie_argv[i]; next_socket = 0; continue; } multiplex_add_sexpr(sx_open_io (io_open_read (curie_argv[i]), io_open (-1)), on_rules_read, (void *)0); while (multiplex() != mx_nothing_to_do); had_rules_file = 1; } if ((use_socket == (char *)0) && (use_stdio == 0) && (mount_self == 0)) { print_help(); } if (!had_rules_file) { multiplex_add_sexpr(sx_open_io (io_open_read (DEFAULT_RULES), io_open (-1)), on_rules_read, (void *)0); while (multiplex() != mx_nothing_to_do); } fs = dfs_create ((void *)0, (void *)0); fs->root->c.mode |= 0111; struct dfs_directory *d_dev9 = dfs_mk_directory (fs->root, "dev9"); struct dfs_file *d_dev9_ctl = dfs_mk_file (d_dev9, "control", (char *)0, (int_8 *)"(nop)\n", 6, (void *)0, (void *)0, on_control_write); queue_io = io_open_special(); d_dev9->c.mode = 0550; d_dev9->c.uid = "dev9"; d_dev9->c.gid = "dev9"; d_dev9_ctl->c.mode = 0660; d_dev9_ctl->c.uid = "dev9"; d_dev9_ctl->c.gid = "dev9"; queue = sx_open_io (queue_io, queue_io); multiplex_add_sexpr (queue, mx_sx_ctl_queue_read, (void *)0); if (initialise_common) { struct dfs_directory *d; d = dfs_mk_directory (fs->root, "pts"); d->c.mode |= 0111; d = dfs_mk_directory (fs->root, "shm"); d->c.mode |= 0111; dfs_mk_symlink (fs->root, "fd", "/proc/self/fd"); dfs_mk_symlink (fs->root, "stdin", "fd/0"); dfs_mk_symlink (fs->root, "stdout", "fd/1"); dfs_mk_symlink (fs->root, "stderr", "fd/2"); } connect_to_netlink(fs); multiplex_all_processes(); multiplex_d9s(); if (use_stdio) { multiplex_add_d9s_stdio (fs); } else if (o_foreground == 0) { struct exec_context *context = execute(EXEC_CALL_NO_IO, (char **)0, (char **)0); switch (context->pid) { case -1: cexit (11); case 0: break; default: cexit (0); } } if (use_socket != (char *)0) { multiplex_add_d9s_socket (use_socket, fs); } if (mount_self) { static char options[] = "access=any,trans=fd,rfdno=000000,wfdno=000000"; struct io *in, *out; int fdi[2], fdo[2]; if ((sys_pipe (fdi) != -1) && (sys_pipe (fdo) != -1)) { struct exec_context *context; in = io_open(fdi[0]); out = io_open(fdo[1]); multiplex_add_d9s_io(in, out, fs); if (!((fdo[0] > 999999) || (fdi[1] > 999999) || (fdo[0] < 1) || (fdi[1] < 1))) { int tj, ti, mn, s2 = 44; for (tj = 31, ti = fdo[0], mn = 6; ti > 0; tj--, ti /= 10, mn--) { options[tj] = '0' + (ti % 10); } for (tj = 26, ti = tj + mn; options[ti]; tj++, ti++) { options[tj] = options[ti]; } options[tj] = options[ti]; s2 -= mn; for (tj = s2, ti = fdi[1], mn = 6; ti > 0; tj--, ti /= 10, mn--) { options[tj] = '0' + (ti % 10); } for (tj = s2-5, ti = tj + mn; options[ti]; tj++, ti++) { options[tj] = options[ti]; } options[tj] = options[ti]; context = execute(EXEC_CALL_NO_IO, (char **)0, (char **)0); switch (context->pid) { case -1: cexit (30); case 0: sys_close (fdi[0]); sys_close (fdo[1]); sys_mount ("dev9", "/dev", "9p", 0, options); if (initialise_common) { sys_mount ("devpts", "/dev/pts", "devpts", 0, (void *)0); sys_mount ("shm", "/dev/shm", "tmpfs", 0, (void *)0); } cexit (0); default: sys_close (fdo[0]); sys_close (fdi[1]); multiplex_add_process(context, mx_on_subprocess_death, (void *)0); } } } } while (multiplex() != mx_nothing_to_do); return 0; }
static void update_status_from_pid_files ( void ) { sexpr c, a, n, pl, name, m, flags; struct sexpr_io *io; struct kyu_module *mod; int online; for (c = lx_environment_alist (mod_metadata); consp (c); c = cdr (c)) { a = car (c); name = car (a); a = cdr (a); online = 0; if (consp ((pl = cdr (a)))) { while (!online && (consp (pl))) { a = car (pl); if (truep (filep (a))) { io = sx_open_i (io_open_read (sx_string (a))); while (!eofp ((n = sx_read (io)))) { if (integerp (n)) { online = kyu_test_pid (sx_integer (n)); break; } } sx_close_io (io); } pl = cdr (pl); } if (!nexp (m = lx_environment_lookup (my_modules, name))) { mod = (struct kyu_module *)m; flags = mod->schedulerflags; if (truep (sx_set_memberp (flags, sym_enabled)) != online) { my_modules = lx_environment_unbind (my_modules, name); if (online) { flags = sx_set_add (flags, sym_enabled); } else { flags = sx_set_remove (flags, sym_enabled); } m = kyu_make_module (mod->name, mod->description, mod->provides, mod->requires, mod->before, mod->after, mod->conflicts, flags, mod->functions); my_modules = lx_environment_bind (my_modules, name, m); kyu_command (cons (sym_update, cons (native_system, cons (m, sx_end_of_list)))); } } } } }
static void on_event (sexpr sx, void *aux) { if (consp (sx)) { sexpr a = car (sx); if (truep (equalp (a, sym_reply))) { sx = cdr (sx); a = car (sx); if (truep (equalp (a, sym_configuration))) { sx = cdr (sx); a = car (sx); if (truep (equalp (a, sym_server_seteh))) { kyu_command (cons (sym_initialising, cons (sym_server_seteh, sx_end_of_list))); sx = lx_environment_lookup (car (cdr (sx)), sym_source); while (consp (sx)) { sexpr files = read_directory_sx (car (sx)); while (consp (files)) { sexpr t = car (files); open_config_files++; multiplex_add_sexpr (sx_open_i (io_open_read (sx_string (t))), on_script_file_read, (void *)0); files = cdr (files); } sx = cdr (sx); } if (open_config_files == 0) { kyu_command (cons (sym_initialised, cons (sym_server_seteh, sx_end_of_list))); } } } } else if (truep (equalp (a, sym_update))) { /* this might be a request from the scheduler, let's have a look... */ sx = cdr (sx); a = car (sx); if (truep (equalp (a, native_system))) { sx = cdr (sx); while (consp (sx)) { a = car (sx); if (kmodulep (a)) { struct kyu_module *mod = (struct kyu_module *)a; a = lx_environment_lookup (my_modules, mod->name); if (!nexp (a)) { /* someone else updated one of our modules... */ struct kyu_module *mydef = (struct kyu_module *)a; handle_external_mod_update (mod, mydef); } } sx = cdr (sx); } } } else if (truep (equalp (a, sym_process_terminated))) { update_status_from_pid_files (); } } }
int cmain () { define_symbol (sym_monitor, "monitor"); int i; programme_identification = cons (sym_monitor, make_integer (2)); #if defined(have_sys_setsid) sys_setsid(); #endif initialise_kyu_script_commands (); multiplex_add_kyu_callback (on_ipc_read, (void *)0); global_environment = kyu_sx_default_environment (); global_environment = lx_environment_bind (global_environment, sym_on_event, lx_foreign_mu (sym_on_event, on_event)); global_environment = lx_environment_bind (global_environment, sym_power_on, lx_foreign_mu (sym_power_on, power_on)); global_environment = lx_environment_bind (global_environment, sym_power_down, lx_foreign_mu (sym_power_down, power_down)); global_environment = lx_environment_bind (global_environment, sym_power_reset, lx_foreign_mu (sym_power_reset, power_reset)); global_environment = lx_environment_bind (global_environment, sym_ctrl_alt_del, lx_foreign_mu (sym_ctrl_alt_del, ctrl_alt_del)); for (i = 1; curie_argv[i] != (char *)0; i++) { sexpr n = make_string (curie_argv[i]); if (truep(filep(n))) { open_script_files++; multiplex_add_sexpr (sx_open_i (io_open_read (curie_argv[i])), on_script_read, (void *)0); } else { native_system = make_symbol (curie_argv[i]); } } kyu_sd_add_listener_stdio (); while (multiplex() == mx_ok); return 21; }