Skip to content

Instantly share code, notes, and snippets.

@nefanov
Last active October 27, 2016 20:59
Show Gist options
  • Save nefanov/94c5f9eaa86f2b47eb16639af2fad6ec to your computer and use it in GitHub Desktop.
Save nefanov/94c5f9eaa86f2b47eb16639af2fad6ec to your computer and use it in GitHub Desktop.
Since kernel 3.3 with CONFIG_CHECKPOINT_RESTORE set(which is set in most distros),
there is /proc/sys/kernel/ns_last_pid which contains last pid generated by kernel.
Open /proc/sys/kernel/ns_last_pid and get fd
flock it with LOCK_EX
write PID-1
fork
https://www.opennet.ru/man.shtml?topic=flock&category=2&russian=0
Voilà! Child will have PID that you wanted. Also, don't forget to unlock (flock with LOCK_UN) and close ns_last_pid.
http://efiop-notes.blogspot.ru/2014/06/how-to-set-pid-using-nslastpid.html
/* Экстракт - ставим эксклюзивный лок на файл, в котором лежит последний pid, переписываем его значением PID-1 и сразу же форкаемся и снимаем блокировку.
*/
2) Orphan processes:
Determine if a process group is "orphaned", according to the POSIX
* definition in 2.2.2.52. Orphaned process groups are not to be affected
* by terminal-generated stop signals. Newly orphaned process groups are
* to receive a SIGHUP and a SIGCONT.
Потерянные группы процессов не должны быть затронуты стоп-сигналов терминальных генерируемых.
Вновь осиротел группы процессов должны получать SIGNUP и SIGCONT.
SIGHUP — сигнал, посылаемый процессу для уведомления о потере соединения с управляющим терминалом пользователя.
POSIX закрепляет за SIGHUP значение 1. Например, для активации новых терминалов после их добавления в файл /etc/ttys рекомендуется послать SIGHUP процессу init, командой «kill -1 1» (init имеет PID = 1).
SIGCONT — сигнал, посылаемый для возобновления выполнения процесса,
ранее остановленного сигналом SIGSTOP или другим сигналом (SIGTSTP, SIGTTIN, SIGTTOU).
...../kernel/exit.c :
static int will_become_orphaned_pgrp(struct pid *pgrp,
struct task_struct *ignored_task)
{
struct task_struct *p;
do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
if ((p == ignored_task) ||
(p->exit_state && thread_group_empty(p)) ||
is_global_init(p->real_parent))
continue;
if (task_pgrp(p->real_parent) != pgrp &&
task_session(p->real_parent) == task_session(p))
return 0;
} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
return 1;
}
int is_current_pgrp_orphaned(void)
{
int retval;
read_lock(&tasklist_lock);
retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
read_unlock(&tasklist_lock);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment