Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Added two sysctl-able flags: proc.curproc.stopfork and proc....



details:   https://anonhg.NetBSD.org/src/rev/bbb2a9235008
branches:  trunk
changeset: 539047:bbb2a9235008
user:      manu <manu%NetBSD.org@localhost>
date:      Thu Nov 07 00:22:28 2002 +0000

description:
Added two sysctl-able flags: proc.curproc.stopfork and proc.curproc.stopexec
that can be used to block a process after fork(2) or exec(2) calls. The
new process is created in the SSTOP state and is never scheduled for running.

This feature is designed so that it is esay to attach the process using gdb
before it has done anything.

It works also with sproc, kthread_create, clone...

diffstat:

 lib/libc/gen/sysctl.3  |  30 +++++++++++++++++++++++++++++-
 sbin/sysctl/sysctl.8   |   4 +++-
 sys/kern/kern_exec.c   |  16 ++++++++++++++--
 sys/kern/kern_fork.c   |  23 ++++++++++++++++++-----
 sys/kern/kern_sysctl.c |  44 +++++++++++++++++++++++++++++++++++++++-----
 sys/sys/proc.h         |   4 +++-
 sys/sys/sysctl.h       |   8 ++++++--
 7 files changed, 112 insertions(+), 17 deletions(-)

diffs (282 lines):

diff -r 3f55841b26e5 -r bbb2a9235008 lib/libc/gen/sysctl.3
--- a/lib/libc/gen/sysctl.3     Wed Nov 06 22:26:47 2002 +0000
+++ b/lib/libc/gen/sysctl.3     Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sysctl.3,v 1.99 2002/10/01 16:59:47 wiz Exp $
+.\"    $NetBSD: sysctl.3,v 1.100 2002/11/07 00:22:30 manu Exp $
 .\"
 .\" Copyright (c) 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -1273,6 +1273,8 @@
 .Bl -column "USER_COLL_WEIGHTS_MAXXXX" "integerXXX" "yes" -offset indent
 .It Sy Pa Third level name     Type    Changeable
 .It PROC\_PID\_CORENAME        string  yes
+.It PROC\_STOPEXEC     int     yes
+.It PROC\_STOPFORK     int     yes
 .It PROC\_PID\_LIMIT   node    not applicable
 .El
 .Bl -tag -width "123456"
@@ -1330,6 +1332,32 @@
 The fifth level name is one of PROC_PID_LIMIT_TYPE_SOFT or
 PROC_PID_LIMIT_TYPE_HARD, to select respectively the soft or hard limit.
 Both are of type integer.
+.It Li PROC_STOPEXEC
+If non zero, the process will be stopped on next 
+.Xr exec 2
+call. The process created by 
+.Xr exec 2
+is created in the SSTOP state and is never scheduled for running 
+before being stopped. This feature helps attaching a process with
+a debugger such as
+.Xr gdb 1
+before it had the opportunity to actually do anything.
+.Pp
+This value is inherited by the process's children.
+.It Li PROC_STOPFORK
+If non zero, the process' children will be stopped after 
+.Xr fork 2
+calls. The children is created in the SSTOP state and is never scheduled 
+for running before being stopped. This feature helps attaching a process
+with a debugger such as
+.Xr gdb 1
+before it had the opportunity to actually do anything.
+.Pp
+This value is inherited by the process's children, and it also
+apply to emulation specific system calls that fork a new process, such as 
+.Fn sproc 
+or 
+.Fn clone .
 .El
 .Sh CTL_USER
 The string and integer information available for the CTL_USER level
diff -r 3f55841b26e5 -r bbb2a9235008 sbin/sysctl/sysctl.8
--- a/sbin/sysctl/sysctl.8      Wed Nov 06 22:26:47 2002 +0000
+++ b/sbin/sysctl/sysctl.8      Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sysctl.8,v 1.81 2002/10/03 15:41:47 wiz Exp $
+.\"    $NetBSD: sysctl.8,v 1.82 2002/11/07 00:22:31 manu Exp $
 .\"
 .\" Copyright (c) 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -356,6 +356,8 @@
 .It proc.\*[Lt]pid\*[Gt].rlimit.memoryuse.soft integer yes
 .It proc.\*[Lt]pid\*[Gt].rlimit.stacksize.hard integer yes
 .It proc.\*[Lt]pid\*[Gt].rlimit.stacksize.soft integer yes
+.It proc.\*[Lt]pid\*[Gt].stopexec      int     yes
+.It proc.\*[Lt]pid\*[Gt].stopfork      int     yes
 .It user.bc_base_max   integer no
 .It user.bc_dim_max    integer no
 .It user.bc_scale_max  integer no
diff -r 3f55841b26e5 -r bbb2a9235008 sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Wed Nov 06 22:26:47 2002 +0000
+++ b/sys/kern/kern_exec.c      Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.161 2002/11/01 19:27:05 jdolecek Exp $ */
+/*     $NetBSD: kern_exec.c,v 1.162 2002/11/07 00:22:30 manu Exp $     */
 
 /*-
  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.161 2002/11/01 19:27:05 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.162 2002/11/07 00:22:30 manu Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_syscall_debug.h"
@@ -738,6 +738,18 @@
        lockmgr(&exec_lock, LK_RELEASE, NULL);
 #endif
        p->p_flag &= ~P_INEXEC;
+
+       if (p->p_flag & P_STOPEXEC) {
+               int s;
+
+               sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
+               SCHED_LOCK(s);
+               p->p_stat = SSTOP;
+               mi_switch(p, NULL);
+               SCHED_ASSERT_UNLOCKED();
+               splx(s);
+       }
+
        return (EJUSTRETURN);
 
  bad:
diff -r 3f55841b26e5 -r bbb2a9235008 sys/kern/kern_fork.c
--- a/sys/kern/kern_fork.c      Wed Nov 06 22:26:47 2002 +0000
+++ b/sys/kern/kern_fork.c      Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_fork.c,v 1.96 2002/10/23 09:14:17 jdolecek Exp $  */
+/*     $NetBSD: kern_fork.c,v 1.97 2002/11/07 00:22:29 manu Exp $      */
 
 /*-
  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.96 2002/10/23 09:14:17 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.97 2002/11/07 00:22:29 manu Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_systrace.h"
@@ -471,16 +471,29 @@
        proclist_unlock_write(s);
 
        /*
-        * Make child runnable, set start time, and add to run queue.
+        * Make child runnable, set start time, and add to run queue
+        * except if the parent requested the child to start in SSTOP state.
         */
        SCHED_LOCK(s);
        p2->p_stats->p_start = time;
        p2->p_acflag = AFORK;
-       p2->p_stat = SRUN;
-       setrunqueue(p2);
+       if (p1->p_flag & P_STOPFORK) {
+               p2->p_stat = SSTOP;
+       } else {
+               p2->p_stat = SRUN;
+               setrunqueue(p2);
+       }
        SCHED_UNLOCK(s);
 
        /*
+        * Inherit STOPFORK and STOPEXEC flags 
+        */
+       if (p1->p_flag & P_STOPFORK)
+               p2->p_flag |= P_STOPFORK;
+       if (p1->p_flag & P_STOPEXEC)
+               p2->p_flag |= P_STOPEXEC;
+
+       /*
         * Now can be swapped.
         */
        PRELE(p1);
diff -r 3f55841b26e5 -r bbb2a9235008 sys/kern/kern_sysctl.c
--- a/sys/kern/kern_sysctl.c    Wed Nov 06 22:26:47 2002 +0000
+++ b/sys/kern/kern_sysctl.c    Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_sysctl.c,v 1.114 2002/11/02 07:25:21 perry Exp $  */
+/*     $NetBSD: kern_sysctl.c,v 1.115 2002/11/07 00:22:29 manu Exp $   */
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -43,7 +43,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.114 2002/11/02 07:25:21 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.115 2002/11/07 00:22:29 manu Exp $");
 
 #include "opt_ddb.h"
 #include "opt_insecure.h"
@@ -744,7 +744,34 @@
                                return EPERM;
                }
        }
-       if (name[1] == PROC_PID_CORENAME) {
+       switch(name[1]) {
+       case PROC_PID_STOPFORK: 
+               if (namelen != 2)
+                       return EINVAL;
+               i = ((ptmp->p_flag & P_STOPFORK) != 0);
+               if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
+                       return error;
+               if (i != 0)
+                       ptmp->p_flag |= P_STOPFORK;
+               else
+                       ptmp->p_flag &= ~P_STOPFORK;
+               return 0;
+               break;
+       
+       case PROC_PID_STOPEXEC: 
+               if (namelen != 2)
+                       return EINVAL;
+               i = ((ptmp->p_flag & P_STOPEXEC) != 0);
+               if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
+                       return error;
+               if (i != 0)     
+                       ptmp->p_flag |= P_STOPEXEC;
+               else
+                       ptmp->p_flag &= ~P_STOPEXEC;
+               return 0;
+               break;
+
+       case PROC_PID_CORENAME:
                if (namelen != 2)
                        return EINVAL;
                /*
@@ -814,8 +841,9 @@
                if (tmps)
                        free(tmps, M_TEMP);
                return (error);
-       }
-       if (name[1] == PROC_PID_LIMIT) {
+               break;
+
+       case PROC_PID_LIMIT:
                if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
                        return EINVAL;
                memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
@@ -835,7 +863,13 @@
                        error = dosetrlimit(ptmp, p->p_cred,
                            name[2] - 1, &alim);
                return error;
+               break;
+
+       default:
+               return (EINVAL);
+               break;
        }
+       /* NOTREACHED */
        return (EINVAL);
 }
 
diff -r 3f55841b26e5 -r bbb2a9235008 sys/sys/proc.h
--- a/sys/sys/proc.h    Wed Nov 06 22:26:47 2002 +0000
+++ b/sys/sys/proc.h    Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: proc.h,v 1.147 2002/10/23 09:14:59 jdolecek Exp $      */
+/*     $NetBSD: proc.h,v 1.148 2002/11/07 00:22:28 manu Exp $  */
 
 /*-
  * Copyright (c) 1986, 1989, 1991, 1993
@@ -308,6 +308,8 @@
 #define        P_INEXEC        0x100000 /* Process is exec'ing and cannot be traced */
 #define        P_SYSTRACE      0x200000 /* Process system call tracing active */
 #define        P_CHTRACED      0x400000 /* Child has been traced & reparented */
+#define        P_STOPFORK      0x800000 /* Child will be stopped on fork(2) */
+#define        P_STOPEXEC      0x1000000 /* Will be stopped on exec(2) */
 
 /*
  * Macro to compute the exit signal to be delivered.
diff -r 3f55841b26e5 -r bbb2a9235008 sys/sys/sysctl.h
--- a/sys/sys/sysctl.h  Wed Nov 06 22:26:47 2002 +0000
+++ b/sys/sys/sysctl.h  Thu Nov 07 00:22:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysctl.h,v 1.78 2002/08/26 13:09:39 augustss Exp $     */
+/*     $NetBSD: sysctl.h,v 1.79 2002/11/07 00:22:28 manu Exp $ */
 
 /*
  * Copyright (c) 1989, 1993
@@ -590,12 +590,16 @@
  */
 #define        PROC_PID_CORENAME       1
 #define        PROC_PID_LIMIT          2
-#define        PROC_PID_MAXID          3
+#define        PROC_PID_STOPFORK       3
+#define        PROC_PID_STOPEXEC       4
+#define        PROC_PID_MAXID          5
 
 #define        PROC_PID_NAMES { \
        { 0, 0 }, \
        { "corename", CTLTYPE_STRING }, \
        { "rlimit", CTLTYPE_NODE }, \
+       { "stopfork", CTLTYPE_INT }, \
+       { "stopexec", CTLTYPE_INT }, \
 }
 
 /* Limit types from <sys/resources.h> */



Home | Main Index | Thread Index | Old Index