static struct snobj *handle_create_module(struct snobj *arg) { const char *mclass_name; const struct mclass *mclass; struct module *module; struct snobj *r; mclass_name = snobj_eval_str(arg, "mclass"); if (!mclass_name) return snobj_err(EINVAL, "Missing 'mclass' field in arg"); mclass = find_mclass(mclass_name); if (!mclass) return snobj_err(ENOENT, "No mclass '%s' found", mclass_name); module = create_module(snobj_eval_str(arg, "name"), mclass, snobj_eval(arg, "arg"), &r); if (!module) return r; printf("Module %s created at %p\n", module->name, module); r = snobj_map(); snobj_map_set(r, "name", snobj_str(module->name)); return r; }
int main(int argc, char **argv) { int lc; tst_parse_opts(argc, argv, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset tst_count in case we are looping */ tst_count = 0; /* Test the system call */ TEST(create_module(modname, MODSIZE)); /* check return code */ if (TEST_RETURN == -1) { tst_resm(TFAIL, "create_module() failed errno=%d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); } else { tst_resm(TPASS, "create_module() returned 0x%x", TEST_RETURN); if (delete_module(modname) != 0) { tst_brkm(TBROK, NULL, "Failed to delete" "loadable module entry for %s", modname); } } } /* perform global cleanup and exit */ cleanup(); }
//------------------------------------------------------------------------------ // Main function to generate module files in current directory //------------------------------------------------------------------------------ int main(int argc, char* argv[]){ // Strings for command line options std::string namespace_name=""; std::string author=""; std::string email=""; // determine namespace name, author and email from command line process_command_line(argc, argv, namespace_name, author, email); // create file header std::string file_header = create_file_header(author, email); // Generate data.cpp create_data(file_header, namespace_name); // Generate interface.cpp create_interface(file_header, namespace_name); // Generate initialise.cpp create_initialise(file_header, namespace_name); // Generate internal.hpp create_internal(file_header, namespace_name); // Generate internal.hpp create_module(file_header, namespace_name); // Generate makefile create_makefile(file_header, namespace_name); return EXIT_SUCCESS; }
static PyObject* __import__(FILE *file, char *name, char *path, char *as, PyObject *local, PyObject *global) { assert(file); assert(name); PyObject *m; PyCodeObject *co = NULL; char *module_name = as ? as : name; if((m = get_exists(name, path))) { Py_INCREF(m); return m; } co = compile(file); //Ps_LogObject((PyObject*)co, Ps_LOG_WARING); if(co == NULL) { Ps_Log("compile failed\n", Ps_LOG_WARING); return NULL; } m = create_module(name, (PyObject*)co); if(m == NULL) { Ps_Log("create_module failed\n", Ps_LOG_WARING); return NULL; } Py_DECREF(co); if(local && PyDict_Check(local)) { Py_INCREF(m); #ifdef IMPORT_DEBUG Ps_LogFormat("The module name is %s\n", Ps_LOG_NORMAL, module_name); Ps_LogObject(local, Ps_LOG_WARING); Ps_LogObject(m, Ps_LOG_WARING); int ret = #endif PyDict_SetItemString(local, module_name, m); #ifdef IMPORT_DEBUG if(ret == 0) Ps_LogFormat("ret is %d, Import module %s successfully\n", Ps_LOG_NORMAL, ret, module_name); else Ps_LogFormat("ret is %d, Import module %s failed\n", Ps_LOG_NORMAL, ret, module_name); #endif } else { PyObject *info = PyString_FromFormat("Import module %s failed", name); if(!info) { PyErr_SetString(PyExc_ImportError, "Import module failed"); } else PyErr_SetObject(PyExc_ImportError, info); return NULL; } return m; }
int main() { textcolor(BRIGHT, RED, BLACK); printf("This is the Bomb component.\n"); textcolor(RESET, WHITE, BLACK); create_module("./asciiart/symbols/s1.dat"); return 0; }
int setup2(void) { /* Create a loadable module entry */ if (create_module(modname, MODSIZE) == -1) { tst_resm(TBROK, "Failed to create module entry" " for %s", modname); return 1; } return 0; }
void initialize_vm(const char* fn) { hdata_t main_code; hpermission_t* permission; registry_file = malloc(sizeof(registry_file_t)); memset(registry_file, 0, sizeof(*registry_file)); main_code = bind_file(fn, &error); print_error("when loading main_code", &error); main_module = create_module(main_code, 0, &error); print_error("when creating main_module", &error); permission = get_permission(main_module, main_code, &error); print_error("when ask access to permission table", &error); *permission |= P_READ | P_READ_TRANSIT | P_WRITE | P_WRITE_TRANSIT | P_EXECUTE | P_EXECUTE_TRANSIT; }
int main(int argc, char **argv) { int lc; char *msg; if ((msg = parse_opts(argc, argv, NULL, NULL)) != (char *)NULL) { tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); } setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; for (testno = 0; testno < TST_TOTAL; ++testno) { if ((tdat[testno].setup) && (tdat[testno].setup())) { /* setup() failed, skip this test */ continue; } TEST(create_module(tdat[testno].modname, tdat[testno].size)); TEST_ERROR_LOG(TEST_ERRNO); if ((TEST_RETURN == (int) tdat[testno].retval) && (TEST_ERRNO == tdat[testno].experrno) ) { tst_resm(TPASS, "Expected results for %s, " "errno: %d", tdat[testno].desc, TEST_ERRNO); } else { tst_resm(TFAIL, "Unexpected results for %s ; " "returned %d (expected %d), errno %d " "(expected %d)", tdat[testno].desc, TEST_RETURN, tdat[testno].retval, TEST_ERRNO, tdat[testno].experrno); } if (tdat[testno].cleanup) { tdat[testno].cleanup(); } } } cleanup(); tst_exit(); }
int main(int argc, char **argv) { int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ /* parse standard options */ if ((msg = parse_opts(argc, argv, NULL, NULL)) != (char *)NULL) { tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); } setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; /* Test the system call */ TEST(create_module(modname,MODSIZE)); /* check return code */ if (TEST_RETURN == -1) { tst_resm(TFAIL, "create_module() failed errno=%d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); } else { tst_resm(TPASS, "create_module() returned 0x%x", TEST_RETURN); if (delete_module(modname) != 0) { tst_brkm(TBROK, NULL, "Failed to delete" "loadable module entry for %s", modname); } } } /* perform global cleanup and exit */ cleanup(); }
bool tweakbukconv::LoadModule(std::string modulename) { bool loaded = false; for (unsigned int i = 0; i < modulelist.size(); i++) { if (modulelist[i] == modulename) { loaded = true; } } if (!loaded) { ModuleInterface* mi; void* module; create_tmi* create_module; destroy_tmi* destroy_module; std::string modulepath = "./" + moduledir + modulename + ".so"; // load the library module = dlopen(modulepath.c_str(), RTLD_LAZY); if (!module) { std::cerr << "Cannot load library: " << dlerror() << '\n'; return false; exit(1); } // load the symbols create_module = (create_tmi*) dlsym(module, "create"); destroy_module = (destroy_tmi*) dlsym(module, "destroy"); if (!create_module || !destroy_module) { cerr << "Cannot load symbols: " << dlerror() << '\n'; return false; exit(1); } std::cout << "Module " << modulename << " Loaded" << std::endl; // create an instance of the class mi = create_module(); mi->BaseInit(reader, groups, users); mi->Init(); modulelist.push_back(modulename); modulevector.push_back(module); moduleinterfacevector.push_back(mi); createvector.push_back(create_module); destroyvector.push_back(destroy_module); boost::shared_ptr<boost::thread> tmp_thread; int modi = -1; for (unsigned int i = 0; i < modulelist.size(); i++) { if (modulelist[i] == modulename) { modi = i; } } if (modi >= 0) { assert(!tmp_thread); tmp_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&tweakbukconv::LoadThreadLoop, this, modi))); module_thread_vector.push_back(tmp_thread); } return true; } std::cout << "module " << modulename << " already loaded" << std::endl; return false; }
int find_or_create_module (char * question, char * module_name) { int ind = find_module_id (question, module_name); if (ind < 0) return create_module (question, module_name); return ind; }
static void stage_capability_test(void) { char tmp1[128]; char tmp2[128]; memset(tmp1, 0, sizeof(tmp1)); memset(tmp2, 0, sizeof(tmp2)); capability = "inet_tcp_create"; set_capability(); if (write_policy()) { int fd = socket(AF_INET, SOCK_STREAM, 0); show_result(fd, 1); if (fd != EOF) close(fd); delete_policy(); fd = socket(AF_INET, SOCK_STREAM, 0); show_result(fd, 0); if (fd != EOF) close(fd); } unset_capability(); { int fd1 = socket(AF_INET, SOCK_STREAM, 0); int fd2 = socket(AF_INET, SOCK_STREAM, 0); int fd3 = socket(AF_INET, SOCK_STREAM, 0); int fd4 = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; socklen_t size = sizeof(addr); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(0); bind(fd1, (struct sockaddr *) &addr, sizeof(addr)); bind(fd2, (struct sockaddr *) &addr, sizeof(addr)); bind(fd3, (struct sockaddr *) &addr, sizeof(addr)); bind(fd4, (struct sockaddr *) &addr, sizeof(addr)); getsockname(fd1, (struct sockaddr *) &addr, &size); capability = "inet_tcp_listen"; set_capability(); if (write_policy()) { show_result(listen(fd1, 5), 1); delete_policy(); show_result(listen(fd2, 5), 0); } unset_capability(); capability = "inet_tcp_connect"; set_capability(); if (write_policy()) { show_result(connect(fd3, (struct sockaddr *) &addr, sizeof(addr)), 1); delete_policy(); show_result(connect(fd4, (struct sockaddr *) &addr, sizeof(addr)), 0); } unset_capability(); if (fd1 != EOF) close(fd1); if (fd2 != EOF) close(fd2); if (fd3 != EOF) close(fd3); if (fd4 != EOF) close(fd4); } capability = "use_inet_udp"; set_capability(); if (write_policy()) { int fd = socket(AF_INET, SOCK_DGRAM, 0); show_result(fd, 1); if (fd != EOF) close(fd); delete_policy(); fd = socket(AF_INET, SOCK_DGRAM, 0); show_result(fd, 0); if (fd != EOF) close(fd); } unset_capability(); capability = "use_inet_ip"; set_capability(); if (write_policy()) { int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); show_result(fd, 1); if (fd != EOF) close(fd); delete_policy(); fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); show_result(fd, 0); if (fd != EOF) close(fd); } unset_capability(); capability = "use_route"; set_capability(); if (write_policy()) { int fd = socket(AF_ROUTE, SOCK_RAW, 0); show_result(fd, 1); if (fd != EOF) close(fd); delete_policy(); fd = socket(AF_ROUTE, SOCK_RAW, 0); show_result(fd, 0); if (fd != EOF) close(fd); } unset_capability(); capability = "use_packet"; set_capability(); if (write_policy()) { int fd = socket(AF_PACKET, SOCK_RAW, 0); show_result(fd, 1); if (fd != EOF) close(fd); delete_policy(); fd = socket(AF_PACKET, SOCK_RAW, 0); show_result(fd, 0); if (fd != EOF) close(fd); } unset_capability(); capability = "use_kernel_module"; set_capability(); if (write_policy()) { if (!is_kernel26) show_result((int) create_module("", 0), 1); show_result(init_module("", NULL), 1); show_result(delete_module(""), 1); delete_policy(); if (!is_kernel26) show_result((int) create_module("", 0), 0); show_result(init_module("", NULL), 0); show_result(delete_module(""), 0); } unset_capability(); capability = "create_fifo"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/XXXXXX"); close(mkstemp(tmp1)); unlink(tmp1); show_result(mknod(tmp1, S_IFIFO, 0), 1); unlink(tmp1); delete_policy(); show_result(mknod(tmp1, S_IFIFO, 0), 0); unlink(tmp1); } unset_capability(); capability = "create_block_dev"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/XXXXXX"); close(mkstemp(tmp1)); unlink(tmp1); show_result(mknod(tmp1, S_IFBLK, MKDEV(1, 0)), 1); unlink(tmp1); delete_policy(); show_result(mknod(tmp1, S_IFBLK, MKDEV(1, 0)), 0); unlink(tmp1); } unset_capability(); capability = "create_char_dev"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/XXXXXX"); close(mkstemp(tmp1)); unlink(tmp1); show_result(mknod(tmp1, S_IFCHR, MKDEV(1, 3)), 1); unlink(tmp1); delete_policy(); show_result(mknod(tmp1, S_IFCHR, MKDEV(1, 3)), 0); unlink(tmp1); } unset_capability(); capability = "create_unix_socket"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/XXXXXX"); close(mkstemp(tmp1)); unlink(tmp1); show_result(mknod(tmp1, S_IFSOCK, 0), 1); unlink(tmp1); delete_policy(); show_result(mknod(tmp1, S_IFSOCK, 0), 0); unlink(tmp1); } if (write_policy()) { struct sockaddr_un addr; int fd1 = socket(AF_UNIX, SOCK_STREAM, 0); int fd2 = socket(AF_UNIX, SOCK_STREAM, 0); memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strcpy(tmp1, "/tmp/XXXXXX"); strncpy(addr.sun_path, tmp1, sizeof(addr.sun_path) - 1); show_result(bind(fd1, (struct sockaddr *) &addr, sizeof(addr)), 1); unlink(tmp1); delete_policy(); show_result(bind(fd2, (struct sockaddr *) &addr, sizeof(addr)), 0); unlink(tmp1); if (fd1 != EOF) close(fd1); if (fd2 != EOF) close(fd2); } unset_capability(); capability = "SYS_MOUNT"; set_capability(); if (write_policy()) { show_result(mount("/", "/", "tmpfs", 0, NULL), 1); delete_policy(); show_result(mount("/", "/", "tmpfs", 0, NULL), 0); } unset_capability(); capability = "SYS_UMOUNT"; set_capability(); if (write_policy()) { mount("/tmp", "/tmp", "tmpfs", 0, NULL); show_result(umount("/tmp"), 1); delete_policy(); mount("/tmp", "/tmp", "tmpfs", 0, NULL); show_result(umount("/"), 0); } unset_capability(); capability = "SYS_REBOOT"; set_capability(); if (write_policy()) { FILE *fp = fopen("/proc/sys/kernel/ctrl-alt-del", "a+"); unsigned int c; if (fp && fscanf(fp, "%u", &c) == 1) { show_result(reboot(LINUX_REBOOT_CMD_CAD_ON), 1); delete_policy(); show_result(reboot(LINUX_REBOOT_CMD_CAD_ON), 0); fprintf(fp, "%u\n", c); } else { /* Use invalid value */ show_result(reboot(0x0000C0DE), 1); delete_policy(); show_result(reboot(0x0000C0DE), 0); } if (fp) fclose(fp); } unset_capability(); capability = "SYS_CHROOT"; set_capability(); if (write_policy()) { show_result(chroot("/"), 1); delete_policy(); show_result(chroot("/"), 0); } unset_capability(); capability = "SYS_PIVOT_ROOT"; set_capability(); if (write_policy()) { int error; char *stack = malloc(8192); pid_t pid = clone(child, stack + (8192 / 2), CLONE_NEWNS, NULL); while (waitpid(pid, &error, __WALL) == EOF && errno == EINTR) error += 0; /* Dummy. */ errno = WIFEXITED(error) ? WEXITSTATUS(error) : -1; show_result(errno ? EOF : 0, 1); delete_policy(); pid = clone(child, stack + (8192 / 2), CLONE_NEWNS, NULL); while (waitpid(pid, &error, __WALL) == EOF && errno == EINTR) error += 0; /* Dummy. */ errno = WIFEXITED(error) ? WEXITSTATUS(error) : -1; show_result(errno ? EOF : 0, 0); free(stack); } unset_capability(); signal(SIGINT, SIG_IGN); capability = "SYS_KILL"; set_capability(); if (write_policy()) { show_result(kill(pid, SIGINT), 1); show_result(tkill(gettid(), SIGINT), 1); #ifdef __NR_tgkill if (is_kernel26) show_result(tgkill(pid, gettid(), SIGINT), 1); #endif delete_policy(); show_result(kill(pid, SIGINT), 0); show_result(tkill(gettid(), SIGINT), 0); #ifdef __NR_tgkill if (is_kernel26) show_result(tgkill(pid, gettid(), SIGINT), 0); #endif } unset_capability(); signal(SIGINT, SIG_DFL); capability = "SYS_KEXEC_LOAD"; set_capability(); if (write_policy()) { #ifdef __NR_sys_kexec_load if (is_kernel26) show_result(sys_kexec_load(0, 0, NULL, 0), 1); #endif delete_policy(); #ifdef __NR_sys_kexec_load if (is_kernel26) show_result(sys_kexec_load(0, 0, NULL, 0), 0); #endif } unset_capability(); capability = "SYS_VHANGUP"; set_capability(); if (write_policy()) { int pty_fd = EOF, status = 0; int pipe_fd[2] = { EOF, EOF }; pipe(pipe_fd); switch (forkpty(&pty_fd, NULL, NULL, NULL)) { case 0: errno = 0; vhangup(); /* Unreachable if vhangup() succeeded. */ status = errno; write(pipe_fd[1], &status, sizeof(status)); _exit(0); case -1: fprintf(stderr, "forkpty() failed.\n"); break; default: close(pipe_fd[1]); read(pipe_fd[0], &status, sizeof(status)); wait(NULL); close(pipe_fd[0]); close(pty_fd); errno = status; show_result(status ? EOF : 0, 1); } delete_policy(); status = 0; pipe(pipe_fd); switch (forkpty(&pty_fd, NULL, NULL, NULL)) { case 0: errno = 0; vhangup(); /* Unreachable if vhangup() succeeded. */ status = errno; write(pipe_fd[1], &status, sizeof(status)); _exit(0); case -1: fprintf(stderr, "forkpty() failed.\n"); break; default: close(pipe_fd[1]); read(pipe_fd[0], &status, sizeof(status)); wait(NULL); close(pipe_fd[0]); close(pty_fd); errno = status; show_result(status ? EOF : 0, 0); } } unset_capability(); capability = "SYS_TIME"; set_capability(); if (write_policy()) { struct timeval tv; struct timezone tz; struct timex buf; time_t now = time(NULL); show_result(stime(&now), 1); gettimeofday(&tv, &tz); show_result(settimeofday(&tv, &tz), 1); memset(&buf, 0, sizeof(buf)); buf.modes = 0x100; /* Use invalid value so that the clock won't change. */ show_result(adjtimex(&buf), 1); delete_policy(); now = time(NULL); show_result(stime(&now), 0); gettimeofday(&tv, &tz); show_result(settimeofday(&tv, &tz), 0); memset(&buf, 0, sizeof(buf)); buf.modes = 0x100; /* Use invalid value so that the clock won't change. */ show_result(adjtimex(&buf), 0); } unset_capability(); capability = "SYS_NICE"; set_capability(); if (write_policy()) { show_result(nice(0), 1); show_result(setpriority(PRIO_PROCESS, pid, getpriority(PRIO_PROCESS, pid)), 1); delete_policy(); show_result(nice(0), 0); show_result(setpriority(PRIO_PROCESS, pid, getpriority(PRIO_PROCESS, pid)), 0); } unset_capability(); capability = "SYS_SETHOSTNAME"; set_capability(); if (write_policy()) { char buffer[4096]; memset(buffer, 0, sizeof(buffer)); gethostname(buffer, sizeof(buffer) - 1); show_result(sethostname(buffer, strlen(buffer)), 1); getdomainname(buffer, sizeof(buffer) - 1); show_result(setdomainname(buffer, strlen(buffer)), 1); delete_policy(); gethostname(buffer, sizeof(buffer) - 1); show_result(sethostname(buffer, strlen(buffer)), 0); getdomainname(buffer, sizeof(buffer) - 1); show_result(setdomainname(buffer, strlen(buffer)), 0); } unset_capability(); capability = "SYS_LINK"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/link_source_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/link_target_XXXXXX"); show_result(link(tmp1, tmp2), 1); unlink(tmp2); unlink(tmp1); delete_policy(); strcpy(tmp1, "/tmp/link_source_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/link_target_XXXXXX"); show_result(link(tmp1, tmp2), 0); unlink(tmp2); unlink(tmp1); } unset_capability(); capability = "SYS_SYMLINK"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/symlink_target_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/symlink_source_XXXXXX"); show_result(symlink(tmp1, tmp2), 1); unlink(tmp2); unlink(tmp1); delete_policy(); strcpy(tmp1, "/tmp/symlink_target_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/symlink_source_XXXXXX"); show_result(symlink(tmp1, tmp2), 0); unlink(tmp2); unlink(tmp1); } unset_capability(); capability = "SYS_RENAME"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/rename_old_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/rename_new_XXXXXX"); show_result(rename(tmp1, tmp2), 1); unlink(tmp2); unlink(tmp1); delete_policy(); strcpy(tmp1, "/tmp/rename_old_XXXXXX"); close(mkstemp(tmp1)); strcpy(tmp2, "/tmp/rename_new_XXXXXX"); show_result(rename(tmp1, tmp2), 0); unlink(tmp2); unlink(tmp1); } unset_capability(); capability = "SYS_UNLINK"; set_capability(); if (write_policy()) { strcpy(tmp1, "/tmp/unlinkXXXXXX"); close(mkstemp(tmp1)); show_result(unlink(tmp1), 1); delete_policy(); strcpy(tmp1, "/tmp/unlinkXXXXXX"); close(mkstemp(tmp1)); show_result(unlink(tmp1), 0); } unset_capability(); unlink(tmp1); capability = "SYS_CHMOD"; set_capability(); if (write_policy()) { show_result(chmod("/dev/null", 0222), 1); delete_policy(); show_result(chmod("/dev/null", 0444), 0); } unset_capability(); chmod("/dev/null", 0666); capability = "SYS_CHOWN"; set_capability(); if (write_policy()) { show_result(chown("/dev/null", 1, 1), 1); delete_policy(); show_result(chown("/dev/null", 2, 2), 0); } unset_capability(); chown("/dev/null", 0, 0); capability = "SYS_IOCTL"; set_capability(); if (0 && write_policy()) { int fd = open("/dev/null", O_RDONLY); show_result(ioctl(fd, 0 /* Use invalid value so that nothing happen. */), 1); delete_policy(); show_result(ioctl(fd, 0 /* Use invalid value so that nothing happen. */), 0); close(fd); } if (write_policy()) { struct ifreq ifreq; int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); memset(&ifreq, 0, sizeof(ifreq)); snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1, "lo"); show_result(ioctl(fd, 35123, &ifreq), 1); delete_policy(); show_result(ioctl(fd, 35123, &ifreq), 0); close(fd); } unset_capability(); capability = "SYS_PTRACE"; set_capability(); if (write_policy()) { int status = 0; int pipe_fd[2] = { EOF, EOF }; pipe(pipe_fd); switch (fork()) { case 0: errno = 0; ptrace(PTRACE_TRACEME, 0, NULL, NULL); status = errno; write(pipe_fd[1], &status, sizeof(status)); _exit(0); case -1: fprintf(stderr, "fork() failed.\n"); break; default: close(pipe_fd[1]); read(pipe_fd[0], &status, sizeof(status)); wait(NULL); close(pipe_fd[0]); errno = status; show_result(status ? EOF : 0, 1); } delete_policy(); status = 0; pipe(pipe_fd); switch (fork()) { case 0: errno = 0; ptrace(PTRACE_TRACEME, 0, NULL, NULL); status = errno; write(pipe_fd[1], &status, sizeof(status)); _exit(0); case -1: fprintf(stderr, "fork() failed.\n"); break; default: close(pipe_fd[1]); read(pipe_fd[0], &status, sizeof(status)); wait(NULL); close(pipe_fd[0]); errno = status; show_result(status ? EOF : 0, 0); } } unset_capability(); }
static PyObject* __import__compiled(FILE *f, char *name, char *path, char *as, PyObject *local, PyObject *global) { char *module_name = as ? as : name; PyCodeObject *co; PyObject *m; if(name == NULL) return NULL; //比较文件的魔数 if(PyMarshal_ReadLongFromFile(f) != PYC_MAGIC) { PyErr_Format(PyExc_ImportError, "Bad magic number of %s", name); return NULL; } //读掉时间信息 (void*)PyMarshal_ReadLongFromFile(f); //创建PyCodeObject co = (PyCodeObject*)PyMarshal_ReadLastObjectFromFile(f); if(co == NULL) { PyErr_Format(PyExc_ImportError, "Cannot create code object from module %s", name); return NULL; } if(!PyCode_Check(co)) { PyErr_Format(PyExc_ImportError, "Non-code object in module %s", name); Py_DECREF(co); return NULL; } /*创建模块*/ m = create_module(name, (PyObject*)co); if(m == NULL) { Ps_Log("create_module failed\n", Ps_LOG_WARING); return NULL; } Py_DECREF(co); /*将模块导入命名空间*/ if(local && PyDict_Check(local)) { Py_INCREF(m); #ifdef IMPORT_DEBUG Ps_LogFormat("The module name is %s\n", Ps_LOG_NORMAL, module_name); Ps_LogObject(local, Ps_LOG_WARING); Ps_LogObject(m, Ps_LOG_WARING); int ret = #endif PyDict_SetItemString(local, module_name, m); #ifdef IMPORT_DEBUG if(ret == 0) Ps_LogFormat("ret is %d, Import module %s successfully\n", Ps_LOG_NORMAL, ret, module_name); else Ps_LogFormat("ret is %d, Import module %s failed\n", Ps_LOG_NORMAL, ret, module_name); #endif } else { PyObject *info = PyString_FromFormat("Import module %s failed", name); if(!info) { PyErr_SetString(PyExc_ImportError, "Import module failed"); } else PyErr_SetObject(PyExc_ImportError, info); return NULL; } return m; }
unsigned init_modules() { return create_module("section", SECTION1) && create_module("section", SECTION2) && create_module("led", LED);//так, или из массива брать, или из конфига }
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) { # if PY_VERSION_HEX >= 0x03000000 return # endif create_module(); }
// --- tERROR pr_call LoaderData::find_modules_in_folder( const tVOID* param_pool, tDWORD param_pool_size, tCODEPAGE cp ) { tERROR error = errOK; cStrObj path; if ( param_pool && param_pool_size ) path.assign( param_pool, cp ); else path.assign( ::g_plugins_path, cCP ); PR_TRACE(( this, prtNOTIFY, "ldr\tSearch folder is \"%tS\"", path.data() )); #if !defined(NOT_VERIFY) && !defined(LATE_CHECK) && defined(NEW_SIGN_LIB) cSCheckList* list; error = sysCreateObjectQuick( (hOBJECT*)&list, IID_SCHECKLIST ); if ( PR_SUCC(error) ) { tUINT err_count = 0; tUINT loads_count = 0; tUINT files_count = 0; cDSKMObjParams* par; path.add_path_sect( L"*.ppl", cCP_UNICODE ); cStrBuff mask( path, cp ); error = list->AddObjByMask( 0, &files_count, mask, cp ); if ( PR_SUCC(error) ) { tDWORD id = 0; PR_TRACE(( this, prtIMPORTANT, "ldr\tmodules loading" )); _check_list( 0, list, 0 ); PR_TRACE(( this, prtIMPORTANT, "ldr\tmodules loading, phase 2" )); error = list->GetFirstObjId( &id ); while( PR_SUCC(error) && files_count ) { tDWORD dskm_err = DSKM_ERR_OK; tDWORD dskm_err_size( sizeof(dskm_err) ); tDWORD par_size( sizeof(par) ); --files_count; list->GetObjProp( id, DSKM_OBJ_PROCESSING_ERROR, &dskm_err, &dskm_err_size ); if ( DSKM_NOT_OK(dskm_err) ) { dskm_err = DSKM_ERR_OK; list->GetObjProp( id, DSKM_OBJ_EXTERNAL_PARAM_PTR, &par, &par_size ); ++err_count; } else if ( PR_FAIL(list->GetObjProp(id,DSKM_OBJ_EXTERNAL_PARAM_PTR,&par,&par_size)) || !par->m_name_size ) ++err_count; else if ( PR_FAIL(create_module(par->m_name,par->m_name_size,par->m_name_cp)) ) ++err_count; else ++loads_count; error = list->GetNextObjId( &id, id ); } if ( error == errEND_OF_THE_LIST ) error = errOK; PR_TRACE(( this, prtIMPORTANT, "ldr\tmodules loaded, files=%d, loads=%d, err=%d", err_count+loads_count, loads_count, err_count )); } list->sysCloseObject(); } #else HANDLE fh; cStrObj mask( path ); WIN32_FIND_DATAW fd; mask.add_path_sect( L"*.ppl", cCP_UNICODE ); memset( &fd, 0, sizeof(fd) ); cStrBuff name( mask, cp ); PR_TRACE(( this, prtIMPORTANT, "ldr\tmodules loading." )); if ( cp == cCP_UNICODE ) fh = FindFirstFileW( (LPCWSTR)(tWCHAR*)name, &fd ); else fh = FindFirstFileA( (tCHAR*)name, (WIN32_FIND_DATA*)&fd ); if ( fh && (fh != INVALID_HANDLE_VALUE) ) { BOOL res; do { tERROR err; cStrObj curr = path; curr.add_path_sect( fd.cFileName, cp ); cStrBuff name( curr, cp ); err = create_module( name, name.used(), cp ); if ( PR_SUCC(error) && PR_FAIL(err) ) error = err; if ( cp == cCP_UNICODE ) res = FindNextFileW( fh, &fd ); else res = FindNextFileA( fh, (WIN32_FIND_DATA*)&fd ); } while( res ); if ( PR_FAIL(error) ) { PR_TRACE(( this, prtERROR, "ldr\tError finding modules - %terr", error )); } FindClose( fh ); } else { error = errMODULE_NOT_FOUND; PR_TRACE(( this, prtERROR, "ldr\tFolder has no prague modules" )); } PR_TRACE(( this, prtIMPORTANT, "ldr\tmodules loaded." )); #endif return error; }
void bootstrap_kernel() { memset(&FUNCS, 0, sizeof(FUNCS)); memset(&TYPES, 0, sizeof(TYPES)); // Allocate a World object. g_world = alloc_world(); g_world->bootstrapStatus = s_Bootstrapping; World* world = g_world; // Instanciate the types that are used by Type. TYPES.table = create_type_unconstructed(); TYPES.nil = create_type_unconstructed(); TYPES.string = create_type_unconstructed(); TYPES.type = create_type_unconstructed(); // Now we can fully instanciate types. type_finish_construction(TYPES.table); type_finish_construction(TYPES.nil); type_finish_construction(TYPES.string); type_finish_construction(TYPES.type); string_setup_type(TYPES.string); // Initialize remaining global types. TYPES.any = create_type(); TYPES.blob = create_type(); TYPES.block = create_type(); TYPES.bool_type = create_type(); TYPES.error = create_type(); TYPES.float_type = create_type(); TYPES.int_type = create_type(); TYPES.list = create_type(); TYPES.native_ptr = create_type(); TYPES.table = create_type(); TYPES.opaque_pointer = create_type(); TYPES.symbol = create_type(); TYPES.term = create_type(); TYPES.vm = create_type(); TYPES.void_type = create_type(); for_each_root_type(type_set_root); any_setup_type(TYPES.any); blob_setup_type(TYPES.blob); block_setup_type(TYPES.block); bool_setup_type(TYPES.bool_type); hashtable_setup_type(TYPES.table); int_setup_type(TYPES.int_type); list_t::setup_type(TYPES.list); symbol_setup_type(TYPES.symbol); native_ptr_setup_type(TYPES.native_ptr); null_setup_type(TYPES.nil); number_setup_type(TYPES.float_type); opaque_pointer_setup_type(TYPES.opaque_pointer); term_setup_type(TYPES.term); string_setup_type(TYPES.error); // errors are just stored as strings for now type_t::setup_type(TYPES.type); void_setup_type(TYPES.void_type); vm_setup_type(TYPES.vm); // Finish initializing World (this requires List and Hashtable types) world_initialize(g_world); // Create builtins block. Value builtinsStr; set_string(&builtinsStr, "builtins"); Block* builtins = create_module(g_world); module_set_name(world, builtins, &builtinsStr); g_world->builtins = builtins; // Create function_decl function. Term* functionDeclFunction = builtins->appendNew(); rename(functionDeclFunction, "function_decl"); FUNCS.function_decl = functionDeclFunction; FUNCS.function_decl->function = FUNCS.function_decl; make_nested_contents(FUNCS.function_decl); block_set_function_has_nested(nested_contents(FUNCS.function_decl), true); // Create value function Term* valueFunc = builtins->appendNew(); rename(valueFunc, "value"); FUNCS.value = valueFunc; // Create Type type Term* typeType = builtins->appendNew(); typeType->function = FUNCS.value; typeType->type = TYPES.type; term_value(typeType)->value_type = TYPES.type; term_value(typeType)->value_data.ptr = TYPES.type; TYPES.type->declaringTerm = typeType; rename(typeType, "Type"); // Create Any type Term* anyType = builtins->appendNew(); anyType->function = valueFunc; anyType->type = TYPES.type; term_value(anyType)->value_type = TYPES.type; term_value(anyType)->value_data.ptr = TYPES.any; TYPES.any->declaringTerm = anyType; rename(anyType, "any"); // Initialize value() func valueFunc->type = TYPES.any; valueFunc->function = FUNCS.function_decl; make_nested_contents(valueFunc); block_set_evaluation_empty(nested_contents(valueFunc), true); // Initialize primitive types (this requires value() function) create_type_value(builtins, TYPES.blob, "Blob"); create_type_value(builtins, TYPES.bool_type, "bool"); create_type_value(builtins, TYPES.block, "Block"); create_type_value(builtins, TYPES.float_type, "number"); create_type_value(builtins, TYPES.int_type, "int"); create_type_value(builtins, TYPES.list, "List"); create_type_value(builtins, TYPES.opaque_pointer, "opaque_pointer"); create_type_value(builtins, TYPES.native_ptr, "native_ptr"); create_type_value(builtins, TYPES.string, "String"); create_type_value(builtins, TYPES.symbol, "Symbol"); create_type_value(builtins, TYPES.term, "Term"); create_type_value(builtins, TYPES.table, "Table"); create_type_value(builtins, TYPES.void_type, "void"); create_type_value(builtins, TYPES.vm, "VM"); // Create global symbol table (requires Hashtable type) symbol_initialize_global_table(); // Setup output_placeholder() function, needed to declare functions properly. FUNCS.output = apply(builtins, FUNCS.function_decl, TermList(), "output_placeholder"); nested_contents(FUNCS.output)->overrides.specializeType = output_placeholder_specializeType; ca_assert(get_output_type(nested_contents(FUNCS.output), 0) == TYPES.any); // Now that output_placeholder is created, fix the value() function. { Term* output = append_output_placeholder(nested_contents(valueFunc), NULL); set_declared_type(output, TYPES.any); finish_building_function(nested_contents(valueFunc)); } ca_assert(get_output_type(nested_contents(valueFunc), 0) == TYPES.any); // input_placeholder() is needed before we can declare a function with inputs FUNCS.input = apply(builtins, FUNCS.function_decl, TermList(), "input_placeholder"); block_set_evaluation_empty(nested_contents(FUNCS.input), true); // Now that we have input_placeholder(), declare one input on output_placeholder() apply(nested_contents(FUNCS.output), FUNCS.input, TermList())->setBoolProp(s_Optional, true); // Initialize a few more types TYPES.selector = unbox_type(create_value(builtins, TYPES.type, "Selector")); list_t::setup_type(TYPES.selector); // Need the comment() function before parsing stdlib.ca FUNCS.comment = apply(builtins, FUNCS.function_decl, TermList(), "comment"); // Parse stdlib.ca parse(builtins, parse_statement_list, find_builtin_file("$builtins/stdlib.ca")); set_string(block_insert_property(builtins, s_ModuleName), "stdlib"); blob_install_functions(world->builtinPatch); selector_setup_funcs(world->builtinPatch); closures_install_functions(world->builtinPatch); reflection_install_functions(world->builtinPatch); misc_builtins_setup_functions(world->builtinPatch); type_install_functions(world->builtinPatch); vm_install_functions(world->builtinPatch); block_set_bool_prop(builtins, s_Builtins, true); ca_assert(FUNCS.declared_state != NULL); nested_contents(FUNCS.add)->overrides.specializeType = specializeType_add_sub_mult; nested_contents(FUNCS.sub)->overrides.specializeType = specializeType_add_sub_mult; nested_contents(FUNCS.mult)->overrides.specializeType = specializeType_add_sub_mult; nested_contents(FUNCS.div)->overrides.specializeType = specializeType_div; FUNCS.get_with_symbol = builtins->get("get_with_symbol"); FUNCS.length = builtins->get("length"); FUNCS.list_append = builtins->get("List.append"); FUNCS.native_patch = builtins->get("native_patch"); FUNCS.package = builtins->get("package"); nested_contents(builtins->get("Type.cast"))->overrides.specializeType = Type_cast_specializeType; // Finish setting up types that are declared in stdlib.ca. TYPES.color = as_type(builtins->get("Color")); TYPES.func = as_type(builtins->get("Func")); TYPES.module_ref = as_type(builtins->get("Module")); TYPES.vec2 = as_type(builtins->get("Vec2")); // Fix function_decl now that Func type is available. { set_declared_type(append_output_placeholder(nested_contents(FUNCS.function_decl), NULL), TYPES.func); set_declared_type(FUNCS.function_decl, TYPES.func); finish_building_function(nested_contents(FUNCS.function_decl)); } // Also, now that Func type is available, update all static closures. for (BlockIterator it(builtins); it; ++it) { Term* term = *it; if (is_function(term)) { set_declared_type(term, TYPES.func); if (term->owningBlock == builtins) // Functions at top level must be static closures update_static_closure_force(term); else update_static_closure_if_possible(term); } } nested_contents(FUNCS.list_append)->overrides.specializeType = List__append_specializeType; #define set_evaluation_empty(name) block_set_evaluation_empty(nested_contents(FUNCS.name), true) set_evaluation_empty(annotate); set_evaluation_empty(annotate_block); set_evaluation_empty(return_func); set_evaluation_empty(discard); set_evaluation_empty(break_func); set_evaluation_empty(continue_func); set_evaluation_empty(comment); set_evaluation_empty(extra_output); set_evaluation_empty(loop_iterator); set_evaluation_empty(static_error); #undef set_evaluation_empty block_link_missing_functions(builtins, builtins); }