Annotation of embedaddon/tmux/osdep-dragonfly.c, revision 1.1.1.1

1.1       misho       1: /* $OpenBSD$ */
                      2: 
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
                      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/stat.h>
                     21: #include <sys/sysctl.h>
                     22: #include <sys/user.h>
                     23: 
                     24: #include <err.h>
                     25: #include <errno.h>
                     26: #include <event.h>
                     27: #include <stdint.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #include <unistd.h>
                     31: 
                     32: struct kinfo_proc      *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
                     33: char                   *osdep_get_name(int, char *);
                     34: char                   *osdep_get_cwd(int);
                     35: struct event_base      *osdep_event_init(void);
                     36: 
                     37: #ifndef nitems
                     38: #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
                     39: #endif
                     40: 
                     41: #define is_runnable(p) \
                     42:        ((p)->kp_stat == SACTIVE || (p)->kp_stat == SIDL)
                     43: #define is_stopped(p) \
                     44:        ((p)->kp_stat == SSTOP || (p)->kp_stat == SZOMB)
                     45: 
                     46: struct kinfo_proc *
                     47: cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
                     48: {
                     49:        if (is_runnable(p1) && !is_runnable(p2))
                     50:                return (p1);
                     51:        if (!is_runnable(p1) && is_runnable(p2))
                     52:                return (p2);
                     53: 
                     54:        if (is_stopped(p1) && !is_stopped(p2))
                     55:                return (p1);
                     56:        if (!is_stopped(p1) && is_stopped(p2))
                     57:                return (p2);
                     58: 
                     59:        if (strcmp(p1->kp_comm, p2->kp_comm) < 0)
                     60:                return (p1);
                     61:        if (strcmp(p1->kp_comm, p2->kp_comm) > 0)
                     62:                return (p2);
                     63: 
                     64:        if (p1->kp_pid > p2->kp_pid)
                     65:                return (p1);
                     66:        return (p2);
                     67: }
                     68: 
                     69: char *
                     70: osdep_get_name(int fd, char *tty)
                     71: {
                     72:        int              mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
                     73:        struct stat      sb;
                     74:        size_t           len;
                     75:        struct kinfo_proc *buf, *newbuf, *bestp;
                     76:        u_int            i;
                     77:        char            *name;
                     78: 
                     79:        buf = NULL;
                     80: 
                     81:        if (stat(tty, &sb) == -1)
                     82:                return (NULL);
                     83:        if ((mib[3] = tcgetpgrp(fd)) == -1)
                     84:                return (NULL);
                     85: 
                     86: retry:
                     87:        if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
                     88:                return (NULL);
                     89:        len = (len * 5) / 4;
                     90: 
                     91:        if ((newbuf = realloc(buf, len)) == NULL)
                     92:                goto error;
                     93:        buf = newbuf;
                     94: 
                     95:        if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
                     96:                if (errno == ENOMEM)
                     97:                        goto retry;
                     98:                goto error;
                     99:        }
                    100: 
                    101:        bestp = NULL;
                    102:        for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
                    103:                if (buf[i].kp_tdev != sb.st_rdev)
                    104:                        continue;
                    105:                if (bestp == NULL)
                    106:                        bestp = &buf[i];
                    107:                else
                    108:                        bestp = cmp_procs(&buf[i], bestp);
                    109:        }
                    110: 
                    111:        name = NULL;
                    112:        if (bestp != NULL)
                    113:                name = strdup(bestp->kp_comm);
                    114: 
                    115:        free(buf);
                    116:        return (name);
                    117: 
                    118: error:
                    119:        free(buf);
                    120:        return (NULL);
                    121: }
                    122: 
                    123: char *
                    124: osdep_get_cwd(int fd)
                    125: {
                    126:        return (NULL);
                    127: }
                    128: 
                    129: struct event_base *
                    130: osdep_event_init(void)
                    131: {
                    132:        return (event_init());
                    133: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>