Hi hackers,

I had a need to track all processes including daemons that become owned
by init that started from a process for my statistics collection function of
my clustering software.
The basic idea is to add a structure to the 'proc' structure to keep current
and future data in and to be able to set (at least today) the application ID
into this structure via a shell command using a syscall().

Please give me comments on the patches.

*** /usr/src/sys/kern/kern_fork.c.orig  Thu May 10 19:54:16 2001
--- /usr/src/sys/kern/kern_fork.c       Thu Jan 10 16:50:25 2002
***************
*** 362,367 ****
--- 362,369 ----
        crhold(p1->p_ucred);
        uihold(p1->p_cred->p_uidinfo);

+       bcopy(&p1->p_csed, &p2->p_csed, sizeof(p2->p_csed));
+
        if (p2->p_prison) {
                p2->p_prison->pr_ref++;
                p2->p_flag |= P_JAILED;


*** /usr/src/sys/sys/proc.h.orig        Thu Jan 10 16:56:13 2002
--- /usr/src/sys/sys/proc.h     Thu Jan 10 17:00:22 2002
***************
*** 53,58 ****
--- 53,59 ----
  #endif
  #include <sys/ucred.h>
  #include <sys/event.h>                        /* For struct klist */
+ #include <sys/cse.h>

  /*
   * One structure allocated per session.
***************
*** 144,149 ****
--- 145,151 ----
        char    p_stat;                 /* S* process status. */
        char    p_pad1[3];

+       struct csed     p_csed;         /* Virtual Application Descriptor */
        pid_t   p_pid;                  /* Process identifier. */
        LIST_ENTRY(proc) p_hash;        /* Hash chain. */
        LIST_ENTRY(proc) p_pglist;      /* List of processes in pgrp. */

*** /usr/src/sys/conf/files.orig        Thu Jan 10 17:07:21 2002
--- /usr/src/sys/conf/files     Thu Jan 10 17:07:35 2002
***************
*** 575,580 ****
--- 575,581 ----
  kern/subr_scanf.c     standard
  kern/subr_taskqueue.c standard
  kern/subr_xxx.c               standard
+ kern/sys_cse.c                standard
  kern/sys_generic.c    standard
  kern/sys_pipe.c               standard
  kern/sys_process.c    standard

*** /usr/src/sys/kern/syscalls.master.orig      Thu Jan 10 17:10:01 2002
--- /usr/src/sys/kern/syscalls.master   Thu Jan 10 17:10:27 2002
***************
*** 520,522 ****
--- 520,523 ----
                            const struct kevent *changelist, int nchanges, \
                            struct kevent *eventlist, int nevents, \
                            const struct timespec *timeout); }
+ ZZZ   STD     BSD     { int cse_set_id(int id_num, pid_t pid); }

(NOTE 'ZZZ' changed by install script to be the next available #)


(HERE IS 'sys_cse.c')

#include "opt_ktrace.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/resourcevar.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>

/*
 * CSE system call.
 */
#ifndef _SYS_SYSPROTO_H_
struct cse_set_id_args {
        int     id_num;
        pid_t   pid;
};
#endif
int
cse_set_id(p, uap)
        struct proc *p;
        register struct cse_set_id_args *uap;
{
struct proc     *px;

        if ((px = pfind(uap->pid)) == NULL) {
                return (ESRCH);
        }

        px->p_csed.cse_c_id = uap->id_num;

        return (0);
}

(PATCHES TO 'PS' TO MAKE USE OF THIS)

*** /usr/src/bin/ps/ps.c.orig   Wed Aug  1 07:06:23 2001
--- /usr/src/bin/ps/ps.c        Thu Jan 10 18:34:12 2002
***************
*** 103,109 ****
  static void    usage __P((void));
  static uid_t  *getuids(const char *, int *);

! char dfmt[] = "pid tt state time command";
  char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
  char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time 
command";  char   o1[] = "pid";
--- 103,109 ----
  static void    usage __P((void));
  static uid_t  *getuids(const char *, int *);

! char dfmt[] = "pid tt vapp state time command";
  char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
  char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time 
command";  char   o1[] = "pid";

*** /usr/src/bin/ps/extern.h.orig       Sat Aug 28 01:14:50 1999
--- /usr/src/bin/ps/extern.h    Thu Jan 10 18:34:42 2002
***************
*** 59,64 ****
--- 59,65 ----
  void   maxrss __P((KINFO *, VARENT *));
  void   nlisterr __P((struct nlist *));
  void   p_rssize __P((KINFO *, VARENT *));
+ void   p_vapp __P((KINFO *, VARENT *));
  void   pagein __P((KINFO *, VARENT *));
  void   parsefmt __P((char *));
  void   pcpu __P((KINFO *, VARENT *));


*** /usr/src/bin/ps/keyword.c.orig      Wed Feb 14 19:55:31 2001
--- /usr/src/bin/ps/keyword.c   Thu Jan 10 18:33:40 2002
***************
*** 185,190 ****
--- 185,191 ----
        {"upr", "UPR", NULL, 0, pvar, NULL, 3, POFF(p_usrpri), CHAR, "d"},
        {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN},
        {"usrpri", "", "upr"},
+       {"vapp", "VAPP", NULL, 0, p_vapp, NULL, 2},
        {"vsize", "", "vsz"},
        {"vsz", "VSZ", NULL, 0, vsize, NULL, 5},
        {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6},


*** /usr/src/bin/ps/print.c.orig        Wed Sep 20 04:16:11 2000
--- /usr/src/bin/ps/print.c     Thu Jan 10 18:33:13 2002
***************
*** 48,53 ****
--- 48,54 ----
  #include <sys/ucred.h>
  #include <sys/user.h>
  #include <sys/sysctl.h>
+ #include <sys/cse.h>
  #include <vm/vm.h>

  #include <err.h>
***************
*** 446,451 ****
--- 447,468 ----

        v = ve->var;
        (void)printf("%*ld", v->width, 
(long)pgtok(KI_EPROC(k)->e_vm.vm_rssize))+ }
+
+ void
+ p_vapp(k, ve)         /* doesn't account for text */
+       KINFO *k;
+       VARENT *ve;
+ {
+       VAR *v;
+       struct proc *p;
+       struct csed *c;
+
+       p = KI_PROC(k);
+       c = &p->p_csed;
+
+       v = ve->var;
+       (void)printf("%*ld", v->width, c->cse_c_id);
  }

  void

(USERLAND CODE)

#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/signal.h>
#include <stdio.h>
#include "cse.h"

void sigsys_handler(int signum);

/*
 *
 * main(int argc, char **argv)
 *
 * parameters:
 *      0       'cse'
 *      1       set_id
 *      2       pid
 *      3       appid
 */
int main(int argc, char **argv)
{
int                     mon_status;
int                     need_flush;

        signal (SIGSYS, sigsys_handler);

        if (argc < 3) {
                fprintf(stderr, "Insufficient parameters...\n");
                exit (1);
        } /* if */

        if (syscall(SYS_cse_set_id, atoi(argv[3]), atoi(argv[2])) == -1)  {
                fprintf(stderr, "Failured to set id\n");
                exit(1);
        } /* if */

        exit (0);

} /* main() */


void sigsys_handler(int signum)
{
        fprintf(stderr, "Sorry, no kernel support for this function\n");
        exit(1);

} /* sigsys_handler() */





To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to