X-Git-Url: https://git.saurik.com/apple/file_cmds.git/blobdiff_plain/864a4b6e4495e8bb9744303352b4f19fccc90738..refs/heads/master:/ipcs/ipcs.c diff --git a/ipcs/ipcs.c b/ipcs/ipcs.c index 480a3c6..986b229 100644 --- a/ipcs/ipcs.c +++ b/ipcs/ipcs.c @@ -42,7 +42,7 @@ #include #include -#include +#include "sys/types.h" #include #include #include @@ -63,7 +63,7 @@ #define IXSEQ_TO_IPCID(ix,perm) (((perm._seq) << 16L) | (ix & 0xffff)) #endif -char * +static char * fmt_perm(u_short mode, char write_char) { static char buffer[100]; @@ -83,7 +83,7 @@ fmt_perm(u_short mode, char write_char) return (&buffer[0]); } -void +static void cvt_time(time_t t, char *buf) { struct tm *tm; @@ -92,8 +92,10 @@ cvt_time(time_t t, char *buf) strcpy(buf, "no-entry"); } else { tm = localtime(&t); - sprintf(buf, "%2d:%02d:%02d", - tm->tm_hour, tm->tm_min, tm->tm_sec); + if (tm != NULL) { + sprintf(buf, "%2d:%02d:%02d", + tm->tm_hour, tm->tm_min, tm->tm_sec); + } } } #define SHMINFO 1 @@ -109,32 +111,12 @@ cvt_time(time_t t, char *buf) #define PID 8 #define TIME 16 -void usage() +static void +usage(void) { errx(EX_USAGE, "%s","usage: ipcs [-abcmopqstMQST]\n"); } -int safe_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, - size_t newlen) - -{ - int rv, sv_errno=0; - - if (seteuid(0)) /* iterator needs root write access to sysctl */ - err(1, "seteuid(0) failed"); - - rv = sysctlbyname(name, oldp, oldlenp, newp, newlen); - if (rv < 0) - sv_errno = errno; - - if (seteuid(getuid())) - err(1, "seteuid(%d) failed", getuid()); - - if (rv < 0) - errno = sv_errno; - return rv; -} - int main(argc, argv) int argc; @@ -147,9 +129,6 @@ main(argc, argv) char datestring[100]; int i; - if (seteuid(getuid())) /* run as user */ - err(1, "seteuid(%d) failed", getuid()); - while ((i = getopt(argc, argv, "MmQqSsabcoptT")) != -1) switch (i) { case 'M': @@ -197,7 +176,12 @@ main(argc, argv) if (display == 0) display = SHMINFO | MSGINFO | SEMINFO; now = time(0); - if (0 == strftime(datestring, sizeof(datestring), "%a %b %e %H:%M:%S %Z %Y", localtime(&now))) + struct tm* tm = localtime(&now); + if (tm == NULL) { + now = 0; + tm = localtime(&now); + } + if (0 == strftime(datestring, sizeof(datestring), "%a %b %e %H:%M:%S %Z %Y", tm)) errx(1, "strftime failed\n"); printf("IPC status from as of %s\n", datestring); if ((display & (MSGINFO | MSGTOTAL))) { @@ -212,7 +196,7 @@ main(argc, argv) ic.ipcs_data = &msginfo; ic.ipcs_datalen = sizeof(msginfo); - if (safe_sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size)) { + if (sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size)) { if (errno != EPERM) { char buffer[1024]; snprintf(buffer, 1024, "sysctlbyname(IPCS_MSG_SYSCTL, op=CONF, &ic, &%ld) datalen=%d", @@ -263,7 +247,7 @@ main(argc, argv) memset(msqptr, 0, sizeof(*msqptr)); - while(!(safe_sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size))) { + while(!(sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size))) { ic.ipcs_data = msqptr; if (msqptr->msg_qbytes != 0) { @@ -275,7 +259,7 @@ main(argc, argv) cvt_time(msqptr->msg_ctime, ctime_buf); printf("q %6d 0x%08x %s %8s %8s", - IXSEQ_TO_IPCID(ic.ipcs_cursor-1, msqptr->msg_perm), + IXSEQ_TO_IPCID((ic.ipcs_cursor-1), msqptr->msg_perm), (int)msqptr->msg_perm._key, fmt_perm(msqptr->msg_perm.mode, 'w'), user_from_uid(msqptr->msg_perm.uid, 0), @@ -338,7 +322,7 @@ main(argc, argv) ic.ipcs_data = &shminfo; ic.ipcs_datalen = sizeof(shminfo); - if (safe_sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size)) { + if (sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size)) { if (errno != EPERM) { errx(1, "sysctlbyname(IPCS_SHM_SYSCTL, op=CONF, &ic, &%ld) datalen=%d failed: %s\n", sizeof(ic), ic.ipcs_datalen, strerror(errno)); @@ -382,9 +366,9 @@ main(argc, argv) ic.ipcs_cursor = 0; /* start */ ic.ipcs_datalen = sizeof(*shmptr); ic.ipcs_data = shmptr; - memset(shmptr, 0, sizeof(shmptr)); + memset(shmptr, 0, sizeof(*shmptr)); - while(!(safe_sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size))) { + while(!(sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size))) { ic.ipcs_data = shmptr; /* xnu workaround */ if (shmptr->shm_perm.mode & 0x0800) { @@ -396,7 +380,7 @@ main(argc, argv) cvt_time(shmptr->shm_ctime, ctime_buf); printf("m %6d 0x%08x %s %8s %8s", - IXSEQ_TO_IPCID(ic.ipcs_cursor-1, shmptr->shm_perm), + IXSEQ_TO_IPCID((ic.ipcs_cursor-1), shmptr->shm_perm), (int)shmptr->shm_perm._key, fmt_perm(shmptr->shm_perm.mode, 'w'), user_from_uid(shmptr->shm_perm.uid, 0), @@ -460,7 +444,7 @@ else ic.ipcs_data = &seminfo; ic.ipcs_datalen = sizeof(seminfo); - if (safe_sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size)) { + if (sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size)) { if (errno != EPERM) { char buffer[1024]; snprintf(buffer, 1024, "sysctlbyname(IPCS_SEM_SYSCTL, op=CONF, &ic, &%ld) datalen=%d", @@ -515,7 +499,7 @@ else memset(semaptr, 0, sizeof(*semaptr)); - while(!(safe_sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size))) { + while(!(sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size))) { ic.ipcs_data = semaptr; /* xnu workaround */ if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0) { @@ -525,7 +509,7 @@ else cvt_time(semaptr->sem_ctime, ctime_buf); printf("s %6d 0x%08x %s %8s %8s", - IXSEQ_TO_IPCID(ic.ipcs_cursor-1, semaptr->sem_perm), + IXSEQ_TO_IPCID((ic.ipcs_cursor-1), semaptr->sem_perm), (int)semaptr->sem_perm._key, fmt_perm(semaptr->sem_perm.mode, 'a'), user_from_uid(semaptr->sem_perm.uid, 0),