process-util.h revision ceee6d3a44a81bcf42b21b777302a226422b11a1
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen#pragma once
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/***
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen This file is part of systemd.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Copyright 2010 Lennart Poettering
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is free software; you can redistribute it and/or modify it
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen under the terms of the GNU Lesser General Public License as published by
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen (at your option) any later version.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is distributed in the hope that it will be useful, but
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Lesser General Public License for more details.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen You should have received a copy of the GNU Lesser General Public License
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen***/
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <stdbool.h>
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich#include <sys/types.h>
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <alloca.h>
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <stdio.h>
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen#include <string.h>
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen#include <signal.h>
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen#include "formats-util.h"
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen#define procfs_file_alloca(pid, field) \
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering ({ \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen pid_t _pid_ = (pid); \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen const char *_r_; \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen if (_pid_ == 0) { \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _r_ = ("/proc/self/" field); \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen } else { \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen } \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _r_; \
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen })
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_state(pid_t pid);
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poetteringint get_process_comm(pid_t pid, char **name);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_exe(pid_t pid, char **name);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_uid(pid_t pid, uid_t *uid);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_gid(pid_t pid, gid_t *gid);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_capeff(pid_t pid, char **capeff);
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poetteringint get_process_cwd(pid_t pid, char **cwd);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_root(pid_t pid, char **root);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint get_process_environ(pid_t pid, char **environ);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint wait_for_terminate(pid_t pid, siginfo_t *status);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersenint wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code);
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichvoid sigkill_wait(pid_t *pid);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich#define _cleanup_sigkill_wait_ _cleanup_(sigkill_wait)
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichint kill_and_sigcont(pid_t pid, int sig);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichvoid rename_process(const char name[8]);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichint is_kernel_thread(pid_t pid);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichint getenv_for_pid(pid_t pid, const char *field, char **_value);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichbool pid_is_alive(pid_t pid);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichbool pid_is_unwaited(pid_t pid);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichbool is_main_thread(void);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich#define PTR_TO_PID(p) ((pid_t) ((uintptr_t) p))
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich#define PID_TO_PTR(p) ((void*) ((uintptr_t) p))
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich