[BACK]Return to procfs_status.c CVS log [TXT][DIR] Up to [local] / sys / miscfs / procfs

Annotation of sys/miscfs/procfs/procfs_status.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: procfs_status.c,v 1.10 2007/06/18 08:30:07 jasper Exp $       */
                      2: /*     $NetBSD: procfs_status.c,v 1.11 1996/03/16 23:52:50 christos Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1993 Jan-Simon Pendry
                      6:  * Copyright (c) 1993
                      7:  *     The Regents of the University of California.  All rights reserved.
                      8:  *
                      9:  * This code is derived from software contributed to Berkeley by
                     10:  * Jan-Simon Pendry.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  * 3. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  *
                     36:  *     @(#)procfs_status.c     8.4 (Berkeley) 6/15/94
                     37:  */
                     38:
                     39: #include <sys/param.h>
                     40: #include <sys/systm.h>
                     41: #include <sys/time.h>
                     42: #include <sys/kernel.h>
                     43: #include <sys/malloc.h>
                     44: #include <sys/proc.h>
                     45: #include <sys/vnode.h>
                     46: #include <sys/ioctl.h>
                     47: #include <sys/tty.h>
                     48: #include <sys/resource.h>
                     49: #include <sys/resourcevar.h>
                     50: #include <miscfs/procfs/procfs.h>
                     51:
                     52: int    procfs_stat_gen(struct proc *, char *s, int);
                     53:
                     54: #define COUNTORCAT(s, l, ps, n)        do { \
                     55:                                        if (s) \
                     56:                                                strlcat(s, ps, l); \
                     57:                                        else \
                     58:                                                n += strlen(ps); \
                     59:                                } while (0)
                     60:
                     61: /* Generates:
                     62:  *  comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg uid gid groups
                     63:  */
                     64: int
                     65: procfs_stat_gen(struct proc *p, char *s, int l)
                     66: {
                     67:        struct session *sess;
                     68:        struct tty *tp;
                     69:        struct ucred *cr;
                     70:        int pid, ppid, pgid, sid;
                     71:        struct timeval ut, st;
                     72:        char ps[256], *sep;
                     73:        int i, n;
                     74:
                     75:        pid = p->p_pid;
                     76:        ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
                     77:        pgid = p->p_pgrp->pg_id;
                     78:        sess = p->p_pgrp->pg_session;
                     79:        sid = sess->s_leader ? sess->s_leader->p_pid : 0;
                     80:
                     81:        n = 0;
                     82:        if (s)
                     83:                bzero(s, l);
                     84:
                     85:        bcopy(p->p_comm, ps, MAXCOMLEN-1);
                     86:        ps[MAXCOMLEN] = '\0';
                     87:        COUNTORCAT(s, l, ps, n);
                     88:
                     89:        (void) snprintf(ps, sizeof(ps), " %d %d %d %d ",
                     90:            pid, ppid, pgid, sid);
                     91:        COUNTORCAT(s, l, ps, n);
                     92:
                     93:        if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
                     94:                snprintf(ps, sizeof(ps), "%d,%d ",
                     95:                    major(tp->t_dev), minor(tp->t_dev));
                     96:        else
                     97:                snprintf(ps, sizeof(ps), "%d,%d ",
                     98:                    -1, -1);
                     99:        COUNTORCAT(s, l, ps, n);
                    100:
                    101:        sep = "";
                    102:        if (sess->s_ttyvp) {
                    103:                snprintf(ps, sizeof(ps), "%sctty", sep);
                    104:                sep = ",";
                    105:                COUNTORCAT(s, l, ps, n);
                    106:        }
                    107:
                    108:        if (SESS_LEADER(p)) {
                    109:                snprintf(ps, sizeof(ps), "%ssldr", sep);
                    110:                sep = ",";
                    111:                COUNTORCAT(s, l, ps, n);
                    112:        }
                    113:
                    114:        if (*sep != ',') {
                    115:                snprintf(ps, sizeof(ps), "noflags");
                    116:                COUNTORCAT(s, l, ps, n);
                    117:        }
                    118:
                    119:        snprintf(ps, sizeof(ps), " %ld,%ld",
                    120:            p->p_stats->p_start.tv_sec, p->p_stats->p_start.tv_usec);
                    121:        COUNTORCAT(s, l, ps, n);
                    122:
                    123:        calcru(p, &ut, &st, (void *) 0);
                    124:        snprintf(ps, sizeof(ps), " %ld,%ld %ld,%ld",
                    125:            ut.tv_sec, ut.tv_usec, st.tv_sec, st.tv_usec);
                    126:        COUNTORCAT(s, l, ps, n);
                    127:
                    128:        snprintf(ps, sizeof(ps), " %s",
                    129:            (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
                    130:        COUNTORCAT(s, l, ps, n);
                    131:
                    132:        cr = p->p_ucred;
                    133:
                    134:        snprintf(ps, sizeof(ps), " %u, %u", cr->cr_uid, cr->cr_gid);
                    135:        COUNTORCAT(s, l, ps, n);
                    136:        for (i = 0; i < cr->cr_ngroups; i++) {
                    137:                snprintf(ps, sizeof(ps), ",%u", cr->cr_groups[i]);
                    138:                COUNTORCAT(s, l, ps, n);
                    139:        }
                    140:
                    141:        snprintf(ps, sizeof(ps), "\n");
                    142:        COUNTORCAT(s, l, ps, n);
                    143:
                    144:        return (s != NULL ? strlen(s) + 1 : n + 1);
                    145: }
                    146:
                    147: int
                    148: procfs_dostatus(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct uio *uio)
                    149: {
                    150:        char *ps;
                    151:        int error, len;
                    152:
                    153:        if (uio->uio_rw != UIO_READ)
                    154:                return (EOPNOTSUPP);
                    155:
                    156:        len = procfs_stat_gen(p, NULL, 0);
                    157:        ps = malloc(len, M_TEMP, M_WAITOK);
                    158:        len = procfs_stat_gen(p, ps, len);
                    159:
                    160:        if (len <= uio->uio_offset)
                    161:                error = 0;
                    162:        else {
                    163:                len -= uio->uio_offset;
                    164:                len = imin(len, uio->uio_resid);
                    165:                error = uiomove(ps + uio->uio_offset, len, uio);
                    166:        }
                    167:
                    168:        free(ps, M_TEMP);
                    169:        return (error);
                    170: }

CVSweb