// init_main - the second kernel thread used to create user_main kernel threads static int init_main(void *arg) { cprintf(" kernel_thread, pid = %d, name = %s\n", current->pid, get_proc_name(current)); schedule(); cprintf(" kernel_thread, pid = %d, name = %s , arg %s \n", current->pid, get_proc_name(current), (const char *)arg); schedule(); cprintf(" kernel_thread, pid = %d, name = %s , en.., Bye, Bye. :)\n",current->pid, get_proc_name(current)); return 0; }
void MessageBrokerStub::push(const char* queue_id, const void *msg, size_t size_txt) { Log::debug("MessageBrokerStub: triying to send msg of size %d to queue_id '%s'", size_txt, queue_id); if(size_txt+sizeof(long)>MAX_MSG_SIZE) throw MessageBrokerError("Message for %s too long size=%d, max_size=%d", queue_id, size_txt, MAX_MSG_SIZE); BrokerRequest request; BrokerResponse response; request.mtypebroker = BROKER_PUSH; strcpy(request.queue_id, queue_id); request.size_txt = size_txt; memcpy(request.msg, msg, size_txt+sizeof(long)); strcpy(request.sender_procname, get_proc_name().c_str() ); try { MessageBrokerServer::sendall(sock, (char*)&request, sizeof(BrokerRequest)); MessageBrokerServer::receiveall(sock, (char*)&response, sizeof(BrokerResponse)); } catch(OSError & error) { throw MessageBrokerError("Network error, cause: %s", error.what()); } if(response.mtypebroker == BROKER_ERROR) throw MessageBrokerError("Error al hacer push en la cola en el broker remoto"); }
log_conf::log_conf() { file_max_size = DEFAULT_LOG_MAX_SIZE; strcpy(log_path, "logs"); get_proc_name(prefix_file_name, sizeof(prefix_file_name)); type = LOGDEBUG; }
// init_main - the second kernel thread used to create user_main kernel threads static int init_main(void *arg) { cprintf("this initproc, pid = %d, name = \"%s\"\n", current->pid, get_proc_name(current)); cprintf("To U: \"%s\".\n", (const char *)arg); cprintf("To U: \"en.., Bye, Bye. :)\"\n"); return 0; }
ssize_t MessageBrokerStub::pull(const char* queue_id, void *msg, size_t max_size_txt, long type) { Log::debug("MessageBrokerStub: triying to pull of the queue_id '%s' with type %d", queue_id, type); BrokerRequest request; BrokerResponse response; request.mtypebroker = BROKER_PULL; strcpy(request.queue_id, queue_id); request.max_size_txt = max_size_txt; request.type = type; strcpy(request.sender_procname, get_proc_name().c_str() ); if(max_size_txt+sizeof(long)>MAX_MSG_SIZE) throw MessageBrokerError("Message for %s too long size=%d, max_size=%d", queue_id, max_size_txt, MAX_MSG_SIZE); try { MessageBrokerServer::sendall(sock, (char*)&request, sizeof(BrokerRequest)); MessageBrokerServer::receiveall(sock, (char*)&response, sizeof(BrokerResponse)); Log::debug("MessageBrokerStub: Receive from queue_id %s", queue_id); } catch(OSError & error) { throw MessageBrokerError("Network error, cause: %s", error.what()); } if(response.mtypebroker == BROKER_ERROR) { throw MessageBrokerError("Error al hacer push en la cola en el broker remoto"); } else { memcpy(msg, response.msg, max_size_txt+sizeof(long)); } return response.msg_size; }
void print_usage(struct usage *help_usage) { struct usage *use = help_usage; int opt_name_max_len = 0; int opt_name_len = 0; int opt_usg_len = 0; int usg_cnt = 0; int cpy_cnt = 0; char *buf = NULL; //char buf[usage_line_len + 1] = {0}; char c = ' '; if (!use) return; buf = (char *)malloc(usage_line_len + 1); if (!buf) return; printf("Usage: %s [Options] [Parameters]\n", get_proc_name()); printf("The Options arg:\n"); for (use = help_usage; use->opt_name != NULL; use++) { opt_name_len = strlen(use->opt_name); if (opt_name_len > opt_name_max_len) opt_name_max_len = opt_name_len; } for (use = help_usage; use->opt_name != NULL; use++) { usg_cnt = 0; opt_usg_len = strlen(use->opt_usage); opt_name_len = strlen(use->opt_name); if (opt_usg_len > usage_line_len) { memcpy(buf, use->opt_usage + usg_cnt, usage_line_len); usg_cnt += usage_line_len; if (isalpha((use->opt_usage + usg_cnt - 1)[0])) c = '-'; printf(" %s%*c%s%c\n", use->opt_name, opt_name_max_len + 2 - opt_name_len, ' ', buf, c); while (usg_cnt < opt_usg_len) { cpy_cnt = usg_cnt + usage_line_len < opt_usg_len ? usage_line_len - 1 : (opt_usg_len - usg_cnt); memcpy(buf, use->opt_usage + usg_cnt, cpy_cnt); buf[cpy_cnt] = '\0'; usg_cnt += cpy_cnt; if (usg_cnt < opt_usg_len) { if (isalpha((use->opt_usage + usg_cnt - 1)[0])) c = '-'; } else { c = ' '; } printf(" %*c%s%c\n", opt_name_max_len + 2, ' ', buf, c); } } else { printf(" %s%*c%s\n", use->opt_name, opt_name_max_len + 2 - opt_name_len, ' ', use->opt_usage); } memset(buf, 0, sizeof(usage_line_len + 1)); } if (buf) free(buf); }
PROTECTED int unw_get_proc_name (unw_cursor_t *cursor, char *buf, size_t buf_len, unw_word_t *offp) { struct cursor *c = (struct cursor *) cursor; return get_proc_name (tdep_get_as (c), tdep_get_ip (c), buf, buf_len, offp, tdep_get_as_arg (c)); }
static void __log_init(void) { int type = _log_type; const char *ident = _log_ident; if (_is_log_init) { return; } log_check_env(); #ifdef LOG_VERBOSE_ENABLE UPDATE_LOG_PREFIX(_log_prefix, LOG_VERBOSE_BIT); #endif UPDATE_LOG_PREFIX(_log_prefix, LOG_FUNCLINE_BIT); #ifdef LOG_IO_OPS _log_use_io = 1; #endif if (_log_use_io) { _log_handle = &log_io_ops; } else { _log_handle = &log_fio_ops; } if (CHECK_LOG_PREFIX(_log_prefix, LOG_VERBOSE_BIT)) { memset(_proc_name, 0, sizeof(_proc_name)); char *proc = get_proc_name(); if (proc) { memset(_log_name, 0, sizeof(_log_name)); strncpy(_proc_name, proc, strlen(proc)); free(proc); } } if (type == 0) { } else { _log_output = type; } switch (_log_output) { case LOG_STDERR: _log_driver = &log_stderr_driver; break; case LOG_FILE: _log_driver = &log_file_driver; break; case LOG_RSYSLOG: _log_driver = &log_rsys_driver; break; default: fprintf(stderr, "unsupport log type!\n"); break; } _log_driver->init(ident); _is_log_init = 1; pthread_mutex_init(&_log_mutex, NULL); return; }
int main(int argc, char** argv, char** envp) { pids.resize(100); auto size = get_pidlist(pids); while (pids.size()/sizeof(int) <= size) { pids.resize(pids.size() * 2); size = get_pidlist(pids); } pids.resize(size / sizeof(int)); for (auto x : pids) { std::string name = get_proc_name(x); if( name != "Unable to open process. Skipping...") procList[x] = get_proc_name(x); } print_procs(procList); return 0; }
string * sah_data_process::get_send_head() { string * str = new string; http_res_head_para & res_head = _base_process->get_res_head_para(); char proc_name[SIZE_LEN_256] = {'\0'}; get_proc_name(proc_name, sizeof(proc_name)); res_head._headers.insert(make_pair("Date", SecToHttpTime(time(NULL)))); res_head._headers.insert(make_pair("Server", proc_name)); res_head._headers.insert(make_pair("Connection", "keep-alive")); res_head._content_length = _body.length(); _base_process->gen_send_head(str); LOG_DEBUG("%s", str->c_str()); return str; }
void MessageBrokerStub::create_queue(const char* queue_id) { BrokerRequest request; BrokerResponse response; request.mtypebroker = BROKER_CREATE; strcpy(request.queue_id, queue_id); strcpy(request.sender_procname, get_proc_name().c_str() ); try { MessageBrokerServer::sendall(sock, (char*)&request, sizeof(BrokerRequest)); MessageBrokerServer::receiveall(sock, (char*)&response, sizeof(BrokerResponse)); } catch(OSError & error) { throw MessageBrokerError("Network error, cause: %s", error.what()); } if(response.mtypebroker == BROKER_ERROR) throw MessageBrokerError("Error al crear la cola en el broker remoto"); }
void rg_error_init(int _reboot_on_exit, int _reboot_on_panic, int strace, char *process, rg_error_level_t console_level, int (*_main_addr)(int, char *[]), const char *_main_name) { if (inited) rg_error(LEXIT, "rg_error_init: already inited"); inited = 1; if (strace) strace_init(); if (process) { strncpy(proc_name, process, MAX_PROC_NAME); proc_name[MAX_PROC_NAME-1] = 0; } else strcpy(proc_name, get_proc_name()); rg_error_console_level = console_level; main_addr = _main_addr; main_name = _main_name; reboot_on_exit = _reboot_on_exit; reboot_on_panic = _reboot_on_panic; }
/** * @brief check_proc_unique * * @param name [in] process name * * @return 1, if process unique; otherwise return 0 */ int check_proc_unique(const char *name) { FILE *fp = NULL; char cmd[256] = {0}; char buf[518] = {0}; int task_cnt = 0; name = get_proc_name(name); sprintf(cmd, "ps -a | grep %s | grep -v grep | \ awk '{ print $1}'", name); if ((fp = popen(cmd, "r")) == NULL) return -1; while (fgets(buf, sizeof(buf), fp) != NULL) { if (strlen(buf) && atoi(buf) > 0) task_cnt++; if (task_cnt > 1) break; } if (fp != NULL) pclose(fp); if (task_cnt > 1) return 0; return 1; }
void MessageBrokerStub::destroy_queue(const char* queue_id) { Log::debug("MessageBrokerStub: triying to destroy the queue_id '%s'", queue_id); BrokerRequest request; BrokerResponse response; request.mtypebroker = BROKER_DESTROY; strcpy(request.queue_id, queue_id); strcpy(request.sender_procname, get_proc_name().c_str() ); try { MessageBrokerServer::sendall(sock, (char*)&request, sizeof(BrokerRequest)); MessageBrokerServer::receiveall(sock, (char*)&response, sizeof(BrokerResponse)); Log::debug("MessageBrokerStub: queue destroy ok '%s'", queue_id); } catch(std::exception & error) { throw MessageBrokerError("Network error, cause: %s", error.what()); } if(response.mtypebroker == BROKER_ERROR) throw MessageBrokerError("Error al destruir la cola en el broker remoto"); }
int main(int argc, char **argv) { int32_t arg_id; int fd, clonefd = -1; int i, j, eoff, off, ret; kfs_event_arg_t *kea; struct fsevent_clone_args fca; char buffer[FSEVENT_BUFSIZ]; struct passwd *p; struct group *g; mode_t va_mode; u_int32_t va_type; u_int32_t is_fse_arg_vnode = 0; char fileModeString[11 + 1]; int8_t event_list[] = { // action to take for each event FSE_REPORT, // FSE_CREATE_FILE, FSE_REPORT, // FSE_DELETE, FSE_REPORT, // FSE_STAT_CHANGED, FSE_REPORT, // FSE_RENAME, FSE_REPORT, // FSE_CONTENT_MODIFIED, FSE_REPORT, // FSE_EXCHANGE, FSE_REPORT, // FSE_FINDER_INFO_CHANGED, FSE_REPORT, // FSE_CREATE_DIR, FSE_REPORT, // FSE_CHOWN, FSE_REPORT, // FSE_XATTR_MODIFIED, FSE_REPORT, // FSE_XATTR_REMOVED, }; if (argc != 1) { fprintf(stderr, "%s (%s)\n", PROGNAME, PROGVERS); fprintf(stderr, "File system change logger for Mac OS X. Usage:\n"); fprintf(stderr, "\n\t%s\n\n", PROGNAME); fprintf(stderr, "%s does not take any arguments. " "It must be run as root.\n\n", PROGNAME); printf("Please report bugs using the following contact information:\n" "<URL:http://www.osxbook.com/software/bugs/>\n"); exit(1); exit(1); } if (geteuid() != 0) { fprintf(stderr, "You must be root to run %s. Try again using 'sudo'.\n", PROGNAME); exit(1); } setbuf(stdout, NULL); if ((fd = open(DEV_FSEVENTS, O_RDONLY)) < 0) { perror("open"); exit(1); } fca.event_list = (int8_t *)event_list; fca.num_events = sizeof(event_list)/sizeof(int8_t); fca.event_queue_depth = EVENT_QUEUE_SIZE; fca.fd = &clonefd; if ((ret = ioctl(fd, FSEVENTS_CLONE, (char *)&fca)) < 0) { perror("ioctl"); close(fd); exit(1); } close(fd); if ((ret = ioctl(clonefd, FSEVENTS_WANT_EXTENDED_INFO, NULL)) < 0) { perror("ioctl"); close(clonefd); exit(1); } // char string2[20]="red dwarf"; // char string1[20]=""; // strcpy(string1, string2); char lastpath[300] = "blank"; char currentpath[300]; while (1) { // event processing loop ret = read(clonefd, buffer, FSEVENT_BUFSIZ); off = 0; while (off < ret) { // process one or more events received struct kfs_event *kfse = (struct kfs_event *)((char *)buffer + off); off += sizeof(int32_t) + sizeof(pid_t); // type + pid if (kfse->type == FSE_EVENTS_DROPPED) { // special event fprintf(stderr, " %-14s = %s\n", "type", "EVENTS DROPPED"); exit(1); } int32_t atype = kfse->type & FSE_TYPE_MASK; uint32_t aflags = FSE_GET_FLAGS(kfse->type); if ((atype < FSE_MAX_EVENTS) && (atype >= -1)) { if (aflags & FSE_COMBINED_EVENTS) { fprintf(stderr,"%s", ", combined events"); exit(1); } if (aflags & FSE_CONTAINS_DROPPED_EVENTS) { fprintf(stderr,"%s", ", contains dropped events"); exit(1); } } else { // should never happen printf("This may be a program bug (type = %d).\n", atype); exit(1); } kea = kfse->args; i = 0; while (off < ret) { // process arguments i++; if (kea->type == FSE_ARG_DONE) { // no more arguments // printf(" %s (%#x)\n", "FSE_ARG_DONE", kea->type); off += sizeof(u_int16_t); break; } eoff = sizeof(kea->type) + sizeof(kea->len) + kea->len; off += eoff; arg_id = (kea->type > FSE_MAX_ARGS) ? 0 : kea->type; // printf(" %-16s%4hd ", kfseArgNames[arg_id], kea->len); // switch kfseNames[atype] // printf("%d ", kfse->pid); if (kea->type == FSE_ARG_STRING) { // handle based on argument type strcpy (currentpath,(char *)&(kea->data.str)); // OR atype == FSE_RENAME // strcmp(currentpath, lastpath) != 0 if ( atype == FSE_RENAME || atype == FSE_CREATE_FILE || atype == FSE_CREATE_DIR || strcmp(currentpath, lastpath) != 0) { printf("%d\t%s\t%s\t%s\n", kfse->pid, get_proc_name(kfse->pid), kfseNames[atype], currentpath); } strcpy (lastpath,currentpath); } kea = (kfs_event_arg_t *)((char *)kea + eoff); // next } // for each argument } // for each event } // forever close(clonefd); exit(0); }
int main(int argc, char **argv) { int32_t arg_id; int fd, clonefd = -1; int i, j, eoff, off, ret; FILE* onf; char msg[MAX_SEND]; int c; int mlen = 0; int udp = 0; int det = 0; char fname[MAX_FILENAME] = ""; char raddr[MAX_IP] = "127.0.0.1"; int rport = 12345; kfs_event_arg_t *kea; struct fsevent_clone_args fca; char buffer[FSEVENT_BUFSIZ]; struct passwd *p; struct group *g; mode_t va_mode; u_int32_t va_type; u_int32_t is_fse_arg_vnode = 0; char fileModeString[11 + 1]; int8_t event_list[] = { // action to take for each event FSE_REPORT, // FSE_CREATE_FILE, FSE_REPORT, // FSE_DELETE, FSE_REPORT, // FSE_STAT_CHANGED, FSE_REPORT, // FSE_RENAME, FSE_REPORT, // FSE_CONTENT_MODIFIED, FSE_REPORT, // FSE_EXCHANGE, FSE_REPORT, // FSE_FINDER_INFO_CHANGED, FSE_REPORT, // FSE_CREATE_DIR, FSE_REPORT, // FSE_CHOWN, FSE_REPORT, // FSE_XATTR_MODIFIED, FSE_REPORT, // FSE_XATTR_REMOVED, }; // Print usage if not root if (geteuid() != 0){ usage(); exit(1); } onf = stdout; opterr = 0; while ((c = getopt (argc, argv, "huf:s:p:")) != -1) switch (c){ case 'f': strncpy(fname,optarg,MAX_FILENAME - 1); onf = fopen(fname,"w"); if (onf == NULL){ fprintf(stderr, "Cannot open output file %s.\n\n",optarg); usage(); exit(1); } break; case 'u': udp = 1; break; case 's': strncpy(raddr,optarg,MAX_IP); break; case 'p': rport = atoi( optarg ); break; case 'h': case '?': if (optopt == 'f'){ fprintf(stderr, "Output filename required.\n"); usage(); exit(1); } usage(); exit(1); } setbuf(onf, NULL); //Set UDP Socket set_dest(raddr, rport); set_sock(); if ((fd = open(DEV_FSEVENTS, O_RDONLY)) < 0) { perror("open"); exit(1); } fca.event_list = (int8_t *)event_list; fca.num_events = sizeof(event_list)/sizeof(int8_t); fca.event_queue_depth = EVENT_QUEUE_SIZE; fca.fd = &clonefd; if ((ret = ioctl(fd, FSEVENTS_CLONE, (char *)&fca)) < 0) { perror("ioctl"); close(fd); exit(1); } close(fd); //YAML comments lines start with '#'. Use this for debug and status statements snprintf(msg, MAX_DATA,"#fsevents device cloned (fd %d)\n#fslogger ready\n",clonefd); if (udp){ send_packet(msg, strlen(msg)); } else { fprintf(onf,"%s",msg); // Since we use setbuf this might not be necessary. Let's do it anyway. fflush(onf); } if ((ret = ioctl(clonefd, FSEVENTS_WANT_EXTENDED_INFO, NULL)) < 0) { perror("ioctl"); close(clonefd); exit(1); } while (1) { // event processing loop if ((ret = read(clonefd, buffer, FSEVENT_BUFSIZ)) > 0){ snprintf(msg, MAX_DATA, "# => received %d bytes\n", ret); if (udp){ send_packet(msg, strlen(msg)); } else { fprintf(onf,"%s", msg); fflush(onf); } } off = 0; while (off < ret) { // process one or more events received // Start message over mlen = 0; struct kfs_event *kfse = (struct kfs_event *)((char *)buffer + off); off += sizeof(int32_t) + sizeof(pid_t); // type + pid //Use snprintf for formatting to permit concantenting the message together mlen += snprintf(msg + mlen, MAX_DATA, "---\n"); if (kfse->type == FSE_EVENTS_DROPPED) { // special event mlen += snprintf(msg + mlen, MAX_DATA, "Event:\n"); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %s\n", "type", "EVENTS_DROPPED"); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "pid", kfse->pid); // Special event with continue. So send data then restart loop if (udp){ send_packet(msg, strlen(msg)); } else { fprintf(onf,"%s", msg); fflush(onf); } off += sizeof(u_int16_t); // FSE_ARG_DONE: sizeof(type) continue; } int32_t atype = kfse->type & FSE_TYPE_MASK; uint32_t aflags = FSE_GET_FLAGS(kfse->type); if ((atype < FSE_MAX_EVENTS) && (atype >= -1)) { mlen += snprintf(msg + mlen, MAX_DATA, "Event:\n"); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %s", "type", kfseNames[atype]); if (aflags & FSE_COMBINED_EVENTS) { mlen += snprintf(msg + mlen, MAX_DATA,"%s", ", combined events"); } if (aflags & FSE_CONTAINS_DROPPED_EVENTS) { mlen += snprintf(msg + mlen, MAX_DATA, "%s", ", contains dropped events"); } mlen += snprintf(msg + mlen,MAX_DATA, "%s","\n"); } else { // should never happen mlen += snprintf(msg + mlen, MAX_DATA, "# This may be a program bug (type = %d).\n", atype); // Special event with exit. So send data if (udp){ send_packet(msg, strlen(msg)); } else { fprintf(onf,"%s", msg); fflush(onf); } exit(1); } mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "pid", kfse->pid); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %s\n", "pname", get_proc_name(kfse->pid)); mlen += snprintf(msg + mlen, MAX_DATA, "%s", "Details:\n"); kea = kfse->args; i = 0; //while ((off < ret) && (i <= FSE_MAX_ARGS)) { // process arguments while (off < ret) { i++; if (kea->type == FSE_ARG_DONE) { // no more arguments mlen += snprintf(msg + mlen, MAX_DATA, " %s:\n", "FSE_ARG_DONE"); // Added Length for FSE_ARG_DONE to be consistent with other values mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "len", 0); // Added Type for FSE_ARG_DONE to be consistent with other values mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "type", kea->type); //This should be the only time to send data for a YAML doc which is a full FSEVENT if (udp){ send_packet(msg, strlen(msg)); } else { fprintf(onf,"%s", msg); fflush(onf); } det = 0; off += sizeof(u_int16_t); break; } eoff = sizeof(kea->type) + sizeof(kea->len) + kea->len; off += eoff; arg_id = (kea->type > FSE_MAX_ARGS) ? 0 : kea->type; // Do no put detail marker on timestamp if (arg_id == 5){ mlen += snprintf(msg + mlen, MAX_DATA, " %s:\n", kfseArgNames[arg_id]); } else { mlen += snprintf(msg + mlen, MAX_DATA, " %s_%d:\n", kfseArgNames[arg_id],det); } mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "len", kea->len); switch (kea->type) { // handle based on argument type case FSE_ARG_VNODE: // a vnode (string) pointer is_fse_arg_vnode = 1; mlen += snprintf(msg + mlen, MAX_DATA, " %s: %s\n", "path", (char *)&(kea->data.vp)); break; case FSE_ARG_STRING: // a string pointer // Added double quotes to protect strings with ":"s // Actually, to handle "\" it needs to be a single quote mlen += snprintf(msg + mlen, MAX_DATA, " %s: \'%s\'\n", "string", (char *)&(kea->data.str)-4); break; case FSE_ARG_INT32: mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "int32", kea->data.int32); break; case FSE_ARG_RAW: // a void pointer mlen += snprintf(msg + mlen, MAX_DATA, " %s: ", "ptr"); for (j = 0; j < kea->len; j++) mlen += snprintf(msg + mlen, MAX_DATA, "%02x ", ((char *)kea->data.ptr)[j]); mlen += snprintf(msg + mlen, MAX_DATA, "%s", "\n"); break; case FSE_ARG_INO: // an inode number mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d\n", "ino", (int)kea->data.ino); break; case FSE_ARG_UID: // a user ID p = getpwuid(kea->data.uid); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d (%s)\n", "uid", kea->data.uid, (p) ? p->pw_name : "?"); break; case FSE_ARG_DEV: // a file system ID or a device number if (is_fse_arg_vnode) { mlen += snprintf(msg + mlen, MAX_DATA, " %s: %#08x\n", "fsid", kea->data.dev); is_fse_arg_vnode = 0; } else { mlen += snprintf(msg + mlen, MAX_DATA, " %s: %#08x (major %u, minor %u)\n", "dev", kea->data.dev, major(kea->data.dev), minor(kea->data.dev)); } break; case FSE_ARG_MODE: // a combination of file mode and file type va_mode = (kea->data.mode & 0x0000ffff); va_type = (kea->data.mode & 0xfffff000); strmode(va_mode, fileModeString); va_type = iftovt_tab[(va_type & S_IFMT) >> 12]; mlen += snprintf(msg + mlen, MAX_DATA, " %s: %s (%#08x, vnode type %s)", "mode", fileModeString, kea->data.mode, (va_type < VTYPE_MAX) ? vtypeNames[va_type] : "?"); if (kea->data.mode & FSE_MODE_HLINK) { mlen += snprintf(msg + mlen, MAX_DATA, "%s", ", hard link"); } if (kea->data.mode & FSE_MODE_LAST_HLINK) { mlen += snprintf(msg + mlen, MAX_DATA, "%s", ", link count zero now"); } mlen += snprintf(msg + mlen, MAX_DATA, "%s", "\n"); break; case FSE_ARG_GID: // a group ID g = getgrgid(kea->data.gid); mlen += snprintf(msg + mlen, MAX_DATA, " %s: %d (%s)\n", "gid", kea->data.gid, (g) ? g->gr_name : "?"); // This is usually the last value before everything repeats. Inc det det += 1; break; case FSE_ARG_INT64: // timestamp mlen += snprintf(msg + mlen, MAX_DATA, " %s: %llu\n", "tstamp", kea->data.timestamp); break; default: mlen += snprintf(msg + mlen, MAX_DATA, " %s = ?\n", "unknown"); break; } kea = (kfs_event_arg_t *)((char *)kea + eoff); // next } // for each argument } // for each event } // forever close(clonefd); // Only close output file if it is not stdout if (argc == 2) { fclose(onf); } exit(0); }
/* * enumerate all modules for process * pid: process id * lsc: load sample count */ static void enum_modules_for_process(pid_t pid, unsigned long long lsc, const char * proc_name) { bool find = false; char *filename; unsigned long name_offset; unsigned int options; struct task_struct *task; struct vm_area_struct *mmap; struct mm_struct *mm; char * name = NULL; char * buffer = NULL; task = px_find_task_by_pid(pid); if (task == NULL) goto ret; name = kzalloc(PATH_MAX, GFP_ATOMIC); if (name == NULL) goto ret; buffer = kzalloc(PATH_MAX, GFP_ATOMIC); if (buffer == NULL) goto ret; mm = get_task_mm(task); if (mm != NULL) { down_read(&mm->mmap_sem); for (mmap = mm->mmap; mmap; mmap = mmap->vm_next) { if (is_valid_module(mmap)) { memset(name, 0, (PATH_MAX) * sizeof(char)); filename = px_d_path(mmap->vm_file, name, PATH_MAX); if (filename != NULL) { options = 0; if (find == false) { options |= MODULE_FLAG_1ST; find = true; if (proc_name != NULL) { /* for the first module (the executable image), if the process name is specified, use the specified one */ strcpy(buffer, proc_name); } else { /* * for the first module (the executable image), we need to get the process name again * because it may be modified by changing the argv[0] */ if (get_proc_name(task, buffer) == 0) { /* failed to get the process name, use the orignal module name */ strcpy(buffer, filename); } } /* save the orignal process name for checking if it will be updated later */ notify_new_loaded_process(task, buffer); } else { memset(buffer, 0, sizeof(PATH_MAX)); strcpy(buffer, filename); } name_offset = get_filename_offset(buffer); module_load_notif(buffer, name_offset, pid, mmap->vm_start, mmap->vm_end - mmap->vm_start, options, lsc); } } } up_read(&mm->mmap_sem); mmput(mm); } if (find == false) { module_load_notif(task->comm, 0, pid, LINUX_APP_BASE_LOW, 0,//mmap->vm_end - mmap->vm_start, MODULE_FLAG_1ST, lsc); } ret: if (name != NULL) kfree(name); if (buffer != NULL) kfree(buffer); return; }
CLIP_DLLEXPORT int _clip_load(ClipMachine * mp, const char *name, struct Coll *names, ClipVar * resp) { ClipFile *file = 0; int r = 0; ClipBlock b; ClipCodeVar c; long hash; char *e = strrchr(name, '.'); char *s = strrchr(name, '/'); c.u.block = &b; if (e && (!s || s < e)) { if (!strcmp(e, ".po") || !strcmp(e, ".pa")) { file = NEW(ClipFile); r = load_pobj(mp, file, name, 0); } else if (!strcasecmp(e, DLLREALSUFF)) { r = load_dll(mp, name, names, resp); return r; } else { _clip_trap_printf(mp, __FILE__, __LINE__, "unknown file type: '%s'", name); return _clip_call_errblock(mp, 1); } } else { char path[256]; snprintf(path, sizeof(path), "%s.po", name); if (!access(path, R_OK)) { file = NEW(ClipFile); r = load_pobj(mp, file, path, 0); } else { snprintf(path, sizeof(path), "%s.pa", name); if (!access(path, R_OK)) { file = NEW(ClipFile); r = load_pobj(mp, file, path, 0); } else { snprintf(path, sizeof(path), "%s%s", name, DLLREALSUFF); if (!access(path, R_OK)) { r = load_dll(mp, path, names, resp); return r; } else { _clip_trap_printf(mp, __FILE__, __LINE__, "cannot load file: '%s'", name); return _clip_call_errblock(mp, 1); } } } } if (r) return r; ++file->refCount; for (r = _clip_first_File(file, &c, &hash); r; r = _clip_next_File(file, &c, &hash)) { if (c.t.type == CCODE_t) { if (resp) { ClipVar *vp; vp = NEW(ClipVar); CLEAR_CLIPVAR(resp); resp->t.flags = F_MPTR; resp->t.type = CCODE_t; resp->p.vp = vp; vp->t.count = 1; vp->t.type = CCODE_t; vp->t.flags = F_NONE; vp->c.u.func = c.u.func; ++file->refCount; break; } else { if (_clip_register_hash(mp, c.u.func, hash)) ++file->refCount; } } else if (c.t.type == PCODE_t) { if (resp) { ClipVar *vp; ClipBlock *bp; vp = NEW(ClipVar); bp = NEW(ClipBlock); CLEAR_CLIPVAR(resp); resp->t.flags = F_MPTR; resp->t.type = PCODE_t; resp->p.vp = vp; vp->t.count = 1; vp->t.type = PCODE_t; vp->t.flags = F_NONE; *bp = *c.u.block; vp->c.u.block = bp; ++file->refCount; break; } else { if (_clip_register_block(mp, *c.u.block, hash)) { char *name; if (names) { name = get_proc_name(&c); append_Coll(names, strdup(name)); } ++file->refCount; } } } } _clip_load_inits(mp, file); delete_ClipFile(mp, file); return 0; }
static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) { FileMonitorEvent ev = {0}; uint8_t buf[FM_BUFSIZE] = {0}; int arg_len, rc, buf_idx = 0, buf_end = -1; if (sizeof (FMEventStruct) != 12) { eprintf ("Invalid FMEventStruct, check your compiler\n"); return false; } for (; fm->running; ) { int rewind = 0; if (buf_idx == buf_end) { buf_idx = 0; } else if (buf_idx > 0) { if (buf_idx > buf_end) { eprintf ("Overflow detected and corrected (%d, %d)\n", buf_idx, buf_end); buf_idx = 0; } else { memmove (buf, buf + buf_idx, (buf_end - buf_idx)); rewind = buf_idx = (buf_end - buf_idx); } } if (buf_idx > FM_BUFSIZE) { eprintf ("Warning: Some data is lost in fsevents data read (%d, %d)\n", buf_idx, FM_BUFSIZE); buf_idx = 0; } memset (buf + buf_idx, 0x00, FM_BUFSIZE - buf_idx); rc = read (fm->fd, buf + buf_idx, FM_BUFSIZE - buf_idx); // hexdump (buf+buf_idx, rc, 0); //arg_len + 2, 0); if (rc < 1) { if (errno != EINTR) { perror ("read"); } return false; } buf_idx = 0; buf_end = buf_idx + rc; if (buf_end >= sizeof (buf)) buf_end = sizeof (buf); while (buf_idx + 1 < buf_end) { // hexdump (buf+buf_idx, sizeof (FMEventStruct) + 32, 0); FMEventStruct *fme = (FMEventStruct*) (buf + buf_idx); /* initialize on first set */ if (ev.type == -1) { ev.type = fme->type; ev.pid = fme->val.u32; ev.ppid = 0; ev.proc = get_proc_name (ev.pid, &ev.ppid); ev.file = (const char *)buf + buf_idx + sizeof (FMEventStruct); } /* parse data packet */ arg_len = parse_event (&ev, fme); if (arg_len == -1) { if (ev.pid && ev.type != -1 && cb) { cb (fm, &ev); } fsevent_free (&ev); arg_len = 2; } else if (arg_len < 1) { arg_len = sizeof (FMEventStruct); } else if (arg_len > (buf_end - buf_idx)) { #if WIP int i; bool found = false; for (i = buf_idx + 1; i + sizeof (FMEventStruct) < buf_end; i++) { if (!memcmp ("\x00\x00\xd4\x8f", buf +i, 4)) { found = true; break; } } if (found) { arg_len = i - buf_idx; eprintf ("FCU %d\n", arg_len); } else { arg_len = sizeof (FMEventStruct) + 2; eprintf ("Invalid length in fsevents data packet (%d, %d)\n", arg_len, buf_end - buf_idx); //arg_len += 12; // hexdump (buf + buf_idx, buf_end - buf_idx, 0); //arg_len + 2, 0); } #else arg_len = sizeof (FMEventStruct) + 2; eprintf ("Invalid length in fsevents data packet (%d, %d)\n", arg_len, buf_end - buf_idx); #endif } buf_idx += arg_len; } } return true; }
CLIP_DLLEXPORT int _clip_load(ClipMachine * ClipMachineMemory, const char *name, struct Coll *names, ClipVar * resp) { ClipFile *file = 0; int r = 0; ClipBlock b; ClipCodeVar c; long hash; char *e = strrchr(name, '.'); char *s = strrchr(name, '/'); c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar = &b; if (e && (!s || s < e)) { if (!strcmp(e, ".po") || !strcmp(e, ".pa")) { file = NEW(ClipFile); r = load_pobj(ClipMachineMemory, file, name, 0); } else if (!strcasecmp(e, DLLREALSUFF)) { r = load_dll(ClipMachineMemory, name, names, resp); return r; } else { _clip_trap_printf(ClipMachineMemory, __FILE__, __LINE__, "unknown file type: '%s'", name); return _clip_call_errblock(ClipMachineMemory, 1); } } else { char path[256]; snprintf(path, sizeof(path), "%s.po", name); if (!access(path, R_OK)) { file = NEW(ClipFile); r = load_pobj(ClipMachineMemory, file, path, 0); } else { snprintf(path, sizeof(path), "%s.pa", name); if (!access(path, R_OK)) { file = NEW(ClipFile); r = load_pobj(ClipMachineMemory, file, path, 0); } else { snprintf(path, sizeof(path), "%s%s", name, DLLREALSUFF); if (!access(path, R_OK)) { r = load_dll(ClipMachineMemory, path, names, resp); return r; } else { _clip_trap_printf(ClipMachineMemory, __FILE__, __LINE__, "cannot load file: '%s'", name); return _clip_call_errblock(ClipMachineMemory, 1); } } } } if (r) return r; ++file->refCount_of_ClipFile; for (r = _clip_first_File(file, &c, &hash); r; r = _clip_next_File(file, &c, &hash)) { if (c.ClipType_type_of_ClipCodeVar.ClipVartype_type_of_ClipType == CCODE_type_of_ClipVarType) { if (resp) { ClipVar *vp; vp = NEW(ClipVar); CLEAR_CLIPVAR(resp); resp->ClipType_t_of_ClipVar.ClipFlags_flags_of_ClipType = F_MPTR_ClipFlags; resp->ClipType_t_of_ClipVar.ClipVartype_type_of_ClipType = CCODE_type_of_ClipVarType; resp->ClipRefVar_p_of_ClipVar.ClipVar_of_ClipRefVar = vp; vp->ClipType_t_of_ClipVar.count_of_ClipType = 1; vp->ClipType_t_of_ClipVar.ClipVartype_type_of_ClipType = CCODE_type_of_ClipVarType; vp->ClipType_t_of_ClipVar.ClipFlags_flags_of_ClipType = F_NONE_ClipFlags; vp->ClipCodeVar_c_of_ClipVar.union_of_ClipCodeVar.ClipFunction_function_of_ClipCodeVar = c.union_of_ClipCodeVar.ClipFunction_function_of_ClipCodeVar; ++file->refCount_of_ClipFile; break; } else { if (_clip_register_hash(ClipMachineMemory, c.union_of_ClipCodeVar.ClipFunction_function_of_ClipCodeVar, hash)) ++file->refCount_of_ClipFile; } } else if (c.ClipType_type_of_ClipCodeVar.ClipVartype_type_of_ClipType == PCODE_type_of_ClipVarType) { if (resp) { ClipVar *vp; ClipBlock *bp; vp = NEW(ClipVar); bp = NEW(ClipBlock); CLEAR_CLIPVAR(resp); resp->ClipType_t_of_ClipVar.ClipFlags_flags_of_ClipType = F_MPTR_ClipFlags; resp->ClipType_t_of_ClipVar.ClipVartype_type_of_ClipType = PCODE_type_of_ClipVarType; resp->ClipRefVar_p_of_ClipVar.ClipVar_of_ClipRefVar = vp; vp->ClipType_t_of_ClipVar.count_of_ClipType = 1; vp->ClipType_t_of_ClipVar.ClipVartype_type_of_ClipType = PCODE_type_of_ClipVarType; vp->ClipType_t_of_ClipVar.ClipFlags_flags_of_ClipType = F_NONE_ClipFlags; *bp = *c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar; vp->ClipCodeVar_c_of_ClipVar.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar = bp; ++file->refCount_of_ClipFile; break; } else { if (_clip_register_block(ClipMachineMemory, *c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar, hash)) { char *name; if (names) { name = get_proc_name(&c); append_Coll(names, strdup(name)); } ++file->refCount_of_ClipFile; } } } } _clip_load_inits(ClipMachineMemory, file); delete_ClipFile(ClipMachineMemory, file); return 0; }