static void ephy_smaps_pid_children_to_html (EphySMaps *smaps, GString *str, pid_t parent_pid) { GDir *proc; const char *name; proc = g_dir_open ("/proc/", 0, NULL); if (!proc) return; while ((name = g_dir_read_name (proc))) { pid_t pid, ppid; EphyProcess process; if (g_str_equal (name, "self")) continue; pid = get_pid_from_proc_name (name); if (pid == 0 || pid == parent_pid) continue; ppid = get_parent_pid (pid); if (ppid != parent_pid) continue; process = get_ephy_process (pid); if (process != EPHY_PROCESS_OTHER) ephy_smaps_pid_to_html (smaps, str, pid, process); } g_dir_close (proc); }
// enumerate all processes bool enumerate_processes(enumerate_processes_callback *callback, int flags) { struct dirent *subdir; DIR *dir; char *path; pid_t pid, ppid; // enumerate through /proc dir = opendir("/proc"); if (dir != NULL) { // first, get a list of all pids while ((subdir = readdir(dir)) != NULL) { if (subdir->d_type == DT_DIR) { pid = string_to_pid(subdir->d_name); if (pid != 0) { ppid = get_parent_pid(pid); pid_list_add(pid, ppid); } } } // create pid tree pid_list_create_tree(); pid_tree_do(pid_tree, callback, 0, flags); pid_list_destroy(); closedir(dir); return true; } else { perror("opendir"); } }
int main(int arc, char ** argv) { pid_t pid = getpid(); pid_t ppid = get_parent_pid(pid); printf("%d\n", ppid); return 0; }
const wchar_t* cprocess_tree::get_parent_name(_In_ DWORD child_pid) { DWORD ppid = get_parent_pid(child_pid); if (0 == ppid) return NULL; process_map::iterator it = _proc_map.find(ppid); if (it == _proc_map.end()) return NULL; return it->second.process_name().c_str(); }
swayc_t *workspace_for_pid(pid_t pid) { int i; swayc_t *ws = NULL; struct pid_workspace *pw = NULL; sway_log(L_DEBUG, "looking for workspace for pid %d", pid); // leaving this here as it's useful for debugging // sway_log(L_DEBUG, "all pid_workspaces"); // for (int k = 0; k < config->pid_workspaces->length; k++) { // pw = config->pid_workspaces->items[k]; // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added); // } do { for (i = 0; i < config->pid_workspaces->length; i++) { pw = config->pid_workspaces->items[i]; pid_t *pw_pid = pw->pid; if (pid == *pw_pid) { sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace); break; // out of for loop } pw = NULL; } if (pw) { break; // out of do-while loop } pid = get_parent_pid(pid); // no sense in looking for matches for pid 0. // also, if pid == getpid(), that is the compositor's // pid, which definitely isn't helpful } while (pid > 0 && pid != getpid()); if (pw) { ws = workspace_by_name(pw->workspace); if (!ws) { sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid); ws = workspace_create(pw->workspace); } list_del(config->pid_workspaces, i); } return ws; }
/* Find root process name */ int get_rpname (int pid, char *result) { int parentPid; char *name; size_t nameLen; parentPid = get_parent_pid(pid); if (PID_ROOT == parentPid) { name = read_proc_property(pid, PROC_PID_STATUS_KEY_NAME); if (NULL != name) { nameLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", name); free(name); } else { nameLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", UNKNOWN_STR); } return nameLen; } else if (PID_EMPTY == parentPid) { return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", UNKNOWN_STR); } else { return get_rpname(parentPid, result); } }
/** The main function to be called from the python ctypes library to * create a pylambda worker process. * * Different error routes produce different error codes of 101 and * above. */ int EXPORT pylambda_worker_main(const std::string& root_path, const std::string& server_address, int loglevel) { /** Set up the debug configuration. * * By default, all LOG_ERROR and LOG_FATAL messages are sent to * stderr, and all messages above loglevel are sent to stdout. * * If GRAPHLAB_LAMBDA_WORKER_LOG_FILE is set and is non-empty, then * all log messages are sent to the file instead of the stdout and * stderr. In this case, the only errors logged to stderr/stdout * concern opening the log file. * * If GRAPHLAB_LAMBDA_WORKER_DEBUG_MODE is set, then the default * log level is set to LOG_DEBUG. If a log file is set, then all * log messages are sent there, otherwise they are sent to stderr. */ boost::optional<std::string> debug_mode_str = graphlab::getenv_str("GRAPHLAB_LAMBDA_WORKER_DEBUG_MODE"); boost::optional<std::string> debug_mode_file_str = graphlab::getenv_str("GRAPHLAB_LAMBDA_WORKER_LOG_FILE"); std::string log_file_string = debug_mode_file_str ? *debug_mode_file_str : ""; bool log_to_file = (!log_file_string.empty()); bool debug_mode = (bool)(debug_mode_str); global_logger().set_log_level(loglevel); global_logger().set_log_to_console(true); // Logging using the LOG_DEBUG_WITH_PID macro requires this_pid to be set. size_t this_pid = get_my_pid(); global_logger().set_pid(this_pid); // Set up the logging to file if needed. if(log_to_file) { // Set up the logging to the file, with any errors being fully logged. global_logger().set_log_to_console(true, true); global_logger().set_log_file(log_file_string); LOG_DEBUG_WITH_PID("Logging lambda worker logs to " << log_file_string); global_logger().set_log_to_console(false); } // Now, set the log mode for debug if(debug_mode) { global_logger().set_log_level(LOG_DEBUG); if(!log_to_file) { // Set logging to console, with everything logged to stderr. global_logger().set_log_to_console(true, true); } } // Log the basic information about parameters. size_t parent_pid = get_parent_pid(); LOG_DEBUG_WITH_PID("root_path = '" << root_path << "'"); LOG_DEBUG_WITH_PID("server_address = '" << server_address << "'"); LOG_DEBUG_WITH_PID("parend pid = " << parent_pid); size_t _last_line = __LINE__; #define __TRACK do { _last_line = __LINE__; } while(0) try { LOG_DEBUG_WITH_PID("Library function entered successfully."); if(server_address == "debug") { logstream(LOG_INFO) << "Exiting dry run." << std::endl; return 1; } __TRACK; boost::optional<std::string> disable_shm = graphlab::getenv_str("GRAPHLAB_DISABLE_LAMBDA_SHM"); bool use_shm = true; if(disable_shm && *disable_shm == "1") { use_shm = false; __TRACK; LOG_DEBUG_WITH_PID("shm disabled."); } __TRACK; graphlab::shmipc::server shm_comm_server; bool has_shm = false; try { __TRACK; has_shm = use_shm ? shm_comm_server.bind() : false; } catch (const std::string& error) { logstream(LOG_ERROR) << "Internal PyLambda Error binding SHM server: " << error << "; disabling SHM." << std::endl; has_shm = false; } catch (const std::exception& error) { logstream(LOG_ERROR) << "Internal PyLambda Error binding SHM server: " << error.what() << "; disabling SHM." << std::endl; has_shm = false; } catch (...) { logstream(LOG_ERROR) << "Unknown internal PyLambda Error binding SHM server; disabling SHM." << std::endl; has_shm = false; } __TRACK; LOG_DEBUG_WITH_PID("shm_comm_server bind: has_shm=" << has_shm); // construct the server __TRACK; cppipc::comm_server server(std::vector<std::string>(), "", server_address); __TRACK; server.register_type<graphlab::lambda::lambda_evaluator_interface>([&](){ if (has_shm) { __TRACK; auto n = new graphlab::lambda::pylambda_evaluator(&shm_comm_server); __TRACK; LOG_DEBUG_WITH_PID("creation of pylambda_evaluator with SHM complete."); __TRACK; return n; } else { __TRACK; auto n = new graphlab::lambda::pylambda_evaluator(); __TRACK; LOG_DEBUG_WITH_PID("creation of pylambda_evaluator without SHM complete."); __TRACK; return n; } }); __TRACK; server.register_type<graphlab::lambda::graph_lambda_evaluator_interface>([&](){ __TRACK; auto n = new graphlab::lambda::graph_pylambda_evaluator(); __TRACK; LOG_DEBUG_WITH_PID("creation of graph_pylambda_evaluator complete."); __TRACK; return n; }); __TRACK; LOG_DEBUG_WITH_PID("Starting server."); __TRACK; server.start(); __TRACK; wait_for_parent_exit(parent_pid); return 0; /** Any exceptions happening? If so, propegate back what's going * on through the error codes. */ } catch (const std::string& error) { logstream(LOG_ERROR) << "Internal PyLambda Error: " << error << "; last successful line =" << _last_line << std::endl; return _last_line; } catch (const std::exception& error) { logstream(LOG_ERROR) << "PyLambda C++ Error: " << error.what() << "; last successful line =" << _last_line << std::endl; return _last_line; } catch (...) { logstream(LOG_ERROR) << "Unknown PyLambda Error" << "; last successful line =" << _last_line << std::endl; return _last_line; } }