static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt)) { const char *str; ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count); struct cgit_filter *save_filter; str = lua_tostring(lua_state, 1); if (!str) return 0; save_filter_write = filter_write; save_filter = current_write_filter; unhook_write(); fn(str); hook_write(save_filter, save_filter_write); return 0; }
static int open_lua_filter(struct cgit_filter *base, va_list ap) { struct lua_filter *filter = (struct lua_filter *)base; int i; if (init_lua_filter(filter)) return 1; hook_write(base, write_lua_filter); lua_getglobal(filter->lua_state, "filter_open"); for (i = 0; i < filter->base.argument_count; ++i) lua_pushstring(filter->lua_state, va_arg(ap, char *)); if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0)) { error_lua_filter(filter); return 1; } return 0; }
aio_fstream::aio_fstream(aio_handle* handle, ACL_FILE_HANDLE fd, unsigned int oflags /* = 600 */) : aio_stream(handle), aio_istream(handle), aio_ostream(handle) { acl_assert(handle); acl_assert(fd != ACL_FILE_INVALID); ACL_VSTREAM* vstream = acl_vstream_fhopen(fd, oflags); stream_ = acl_aio_open(handle->get_handle(), vstream); // 调用基类的 hook_error 以向 handle 中增加异步流计数, // 同时 hook 关闭及超时回调过程 hook_error(); // 只有当流连接成功后才可 hook IO 读写状态 // hook 读回调过程 hook_read(); // hook 写回调过程 hook_write(); }
bool aio_fstream::open(const char* path, unsigned int oflags, unsigned int mode) { ACL_VSTREAM* fp = acl_vstream_fopen(path, oflags, mode, 8192); if (fp == NULL) return false; stream_ = acl_aio_open(handle_->get_handle(), fp); // 调用基类的 hook_error 以向 handle 中增加异步流计数, // 同时 hook 关闭及超时回调过程 hook_error(); // 只有当流连接成功后才可 hook IO 读写状态 // hook 读回调过程 if ((oflags & (O_RDONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC))) hook_read(); // hook 写回调过程 if ((oflags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC))) hook_write(); return true; }
asmlinkage long n_sys_write ( unsigned int fd, const char __user *buf, size_t count ) { long ret; #if __DEBUG_RW__ if ( memstr((void *)buf, "filter keyword", count) ) { unsigned long i; printk("DEBUG sys_write: fd=%d, count=%zu, buf=\n", fd, count); for ( i = 0; i < count; i++ ) printk("%x", (unsigned char)buf[i]); printk("\n"); } #endif hook_write(&fd, buf, &count); hijack_pause(sys_write); ret = sys_write(fd, buf, count); hijack_resume(sys_write); return ret; }
/** Helper function to build and send a database-related event to the server hook. * * \param self Database on which the event happened * \param event event to report * * \return 0 on success (even if the hook is disabled), -1 otherwise * * \see HOOK_CMD_DBCREATED, HOOK_CMD_DBOPENED, HOOK_CMD_DBCLOSED */ static int database_hook_send_event(Database *self, const char* event){ char dburi[PATH_MAX+1]; MString *hook_command = mstring_create(); if(hook_enabled()) { if(!self->get_uri(self, dburi, sizeof(dburi))) { logwarn("%s: Unable to get full URI to database for hook\n", self->name); } else if (mstring_sprintf(hook_command, "%s %s\n", event, dburi) == -1) { logwarn("%s: Failed to construct command string for event hook\n", self->name); mstring_delete(hook_command); return -1; } else if(hook_write(mstring_buf(hook_command), mstring_len(hook_command)) < (int)mstring_len(hook_command)) { logwarn("%s: Failed to send command string to event hook: %s\n", self->name, strerror(errno)); mstring_delete(hook_command); return -1; } } return 0; }
/** Terminate the event hook process */ void hook_cleanup (void) { int i, ret; if (!hook) return; logdebug("hook: Cleaning up `%s'\n", hook); if(hook_write(HOOK_CMD_EXIT, (int)(sizeof(HOOK_CMD_EXIT) - 1)) == (int)(sizeof(HOOK_CMD_EXIT) - 1)) return; logdebug("hook: Problem commanding `%s' to exit: %s\n", hook, strerror(errno)); if(!kill(hookpid, SIGTERM)) return; logdebug("hook: Cannot kill (TERM) process %d: %s\n", hookpid, strerror(errno)); for (i=0; i<3; i++) { ret = kill(hookpid, SIGKILL); if(ret == ESRCH) return; logdebug("hook: Cannot kill (KILL) process %d: %s\n", hookpid, strerror(errno)); } logwarn("hook: Could not kill process %d; giving up...", hookpid); }