[BACK]Return to procname.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/procname.c, Revision 1.1

1.1     ! nicm        1: /* $OpenBSD$ */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
        !             5:  *
        !             6:  * Permission to use, copy, modify, and distribute this software for any
        !             7:  * purpose with or without fee is hereby granted, provided that the above
        !             8:  * copyright notice and this permission notice appear in all copies.
        !             9:  *
        !            10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
        !            15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
        !            16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            17:  */
        !            18:
        !            19: #include <sys/param.h>
        !            20: #include <sys/sysctl.h>
        !            21: #include <sys/stat.h>
        !            22:
        !            23: #include <errno.h>
        !            24: #include <stdlib.h>
        !            25: #include <string.h>
        !            26: #include <unistd.h>
        !            27:
        !            28: #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
        !            29:
        !            30: #define is_runnable(p) \
        !            31:        ((p)->p_stat == SRUN || (p)->p_stat == SIDL || (p)->p_stat == SONPROC)
        !            32: #define is_stopped(p) \
        !            33:        ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
        !            34:
        !            35: char   *get_proc_name(int, char *);
        !            36:
        !            37: char *
        !            38: get_proc_name(int fd, char *tty)
        !            39: {
        !            40:        int              mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
        !            41:        struct stat      sb;
        !            42:        size_t           len;
        !            43:        struct kinfo_proc *buf, *newbuf;
        !            44:        struct proc     *p, *bestp;
        !            45:        u_int            i;
        !            46:        char            *name;
        !            47:
        !            48:        buf = NULL;
        !            49:
        !            50:        if (stat(tty, &sb) == -1)
        !            51:                return (NULL);
        !            52:        if ((mib[3] = tcgetpgrp(fd)) == -1)
        !            53:                return (NULL);
        !            54:
        !            55: retry:
        !            56:        if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
        !            57:                return (NULL);
        !            58:        len = (len * 5) / 4;
        !            59:
        !            60:        if ((newbuf = realloc(buf, len)) == NULL) {
        !            61:                free(buf);
        !            62:                return (NULL);
        !            63:        }
        !            64:        buf = newbuf;
        !            65:
        !            66:        if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
        !            67:                if (errno == ENOMEM)
        !            68:                        goto retry;
        !            69:                free(buf);
        !            70:                return (NULL);
        !            71:        }
        !            72:
        !            73:        bestp = NULL;
        !            74:        for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
        !            75:                if (buf[i].kp_eproc.e_tdev != sb.st_rdev)
        !            76:                        continue;
        !            77:                p = &buf[i].kp_proc;
        !            78:                if (bestp == NULL) {
        !            79:                        bestp = p;
        !            80:                        continue;
        !            81:                }
        !            82:
        !            83:                if (is_runnable(p) && !is_runnable(bestp))
        !            84:                        bestp = p;
        !            85:                else if (!is_runnable(p) && is_runnable(bestp))
        !            86:                        continue;
        !            87:
        !            88:                if (!is_stopped(p) && is_stopped(bestp))
        !            89:                        bestp = p;
        !            90:                else if (is_stopped(p) && !is_stopped(bestp))
        !            91:                        continue;
        !            92:
        !            93:                if (p->p_estcpu > bestp->p_estcpu)
        !            94:                        bestp = p;
        !            95:                else if (p->p_estcpu < bestp->p_estcpu)
        !            96:                        continue;
        !            97:
        !            98:                if (p->p_slptime < bestp->p_slptime)
        !            99:                        bestp = p;
        !           100:                else if (p->p_slptime > bestp->p_slptime)
        !           101:                        continue;
        !           102:
        !           103:                if (p->p_flag & P_SINTR && !(bestp->p_flag & P_SINTR))
        !           104:                        bestp = p;
        !           105:                else if (!(p->p_flag & P_SINTR) && bestp->p_flag & P_SINTR)
        !           106:                        continue;
        !           107:
        !           108:                if (LIST_FIRST(&p->p_children) == NULL &&
        !           109:                    LIST_FIRST(&bestp->p_children) != NULL) /* XXX ugh */
        !           110:                        bestp = p;
        !           111:                else if (LIST_FIRST(&p->p_children) != NULL &&
        !           112:                    LIST_FIRST(&bestp->p_children) == NULL)
        !           113:                        continue;
        !           114:
        !           115:                if (strcmp(p->p_comm, p->p_comm) < 0)
        !           116:                        bestp = p;
        !           117:                else if (strcmp(p->p_comm, p->p_comm) > 0)
        !           118:                        continue;
        !           119:
        !           120:                if (p->p_pid > bestp->p_pid)
        !           121:                        bestp = p;
        !           122:        }
        !           123:
        !           124:        name = NULL;
        !           125:        if (bestp != NULL)
        !           126:                name = strdup(bestp->p_comm);
        !           127:
        !           128:        free(buf);
        !           129:        return (name);
        !           130: }