This patch converts linprocfs to use sbufs instead of sptrinf(). You'll need the latest sbuf patch (sbuf-20001211b.diff) to build and run the patched linprocfs. -- DES Index: sys/i386/linux/linprocfs/linprocfs_misc.c =================================================================== RCS file: /home/ncvs/src/sys/i386/linux/linprocfs/linprocfs_misc.c,v retrieving revision 1.14 diff -u -r1.14 linprocfs_misc.c --- sys/i386/linux/linprocfs/linprocfs_misc.c 2000/12/09 13:25:54 1.14 +++ sys/i386/linux/linprocfs/linprocfs_misc.c 2000/12/11 19:58:22 @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -86,9 +87,9 @@ struct pfsnode *pfs; struct uio *uio; { + struct sbuf sb; char *ps; - int xlen; - char psbuf[512]; /* XXX - conservative */ + int r, xlen; unsigned long memtotal; /* total memory in bytes */ unsigned long memused; /* used memory in bytes */ unsigned long memfree; /* free memory in bytes */ @@ -140,8 +141,8 @@ buffers = 0; cached = cnt.v_cache_count * PAGE_SIZE; - ps = psbuf; - ps += sprintf(ps, + sbuf_new(&sb, NULL, 512, 0); + sbuf_printf(&sb, " total: used: free: shared: buffers: cached:\n" "Mem: %lu %lu %lu %lu %lu %lu\n" "Swap: %lu %lu %lu\n" @@ -158,11 +159,13 @@ B2K(memshared), B2K(buffers), B2K(cached), B2K(swaptotal), B2K(swapfree)); - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; } int @@ -172,9 +175,9 @@ struct pfsnode *pfs; struct uio *uio; { + struct sbuf sb; char *ps; - int xlen; - char psbuf[512]; /* XXX - conservative */ + int r, xlen; int class, i, fqmhz, fqkhz; /* @@ -215,8 +218,8 @@ break; } - ps = psbuf; - ps += sprintf(ps, + sbuf_new(&sb, NULL, 512, 0); + sbuf_printf(&sb, "processor\t: %d\n" "vendor_id\t: %.20s\n" "cpu family\t: %d\n" @@ -224,7 +227,7 @@ "stepping\t: %d\n", 0, cpu_vendor, class, cpu, cpu_id & 0xf); - ps += sprintf(ps, + sbuf_cat(&sb, "flags\t\t:"); if (!strcmp(cpu_vendor, "AuthenticAMD") && (class < 6)) { @@ -235,22 +238,24 @@ for (i = 0; i < 32; i++) if (cpu_feature & (1 << i)) - ps += sprintf(ps, " %s", flags[i]); - ps += sprintf(ps, "\n"); + sbuf_printf(&sb, " %s", flags[i]); + sbuf_cat(&sb, "\n"); if (class >= 5) { fqmhz = (tsc_freq + 4999) / 1000000; fqkhz = ((tsc_freq + 4999) / 10000) % 100; - ps += sprintf(ps, + sbuf_printf(&sb, "cpu MHz\t\t: %d.%02d\n" "bogomips\t: %d.%02d\n", fqmhz, fqkhz, fqmhz, fqkhz); } - - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; } int @@ -260,12 +265,12 @@ struct pfsnode *pfs; struct uio *uio; { + struct sbuf sb; char *ps; - char psbuf[512]; - int xlen; + int r, xlen; - ps = psbuf; - ps += sprintf(ps, + sbuf_new(&sb, NULL, 512, 0); + sbuf_printf(&sb, "cpu %ld %ld %ld %ld\n" "disk 0 0 0 0\n" "page %u %u\n" @@ -284,11 +289,14 @@ cnt.v_intr, cnt.v_swtch, boottime.tv_sec); - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; } int @@ -298,21 +306,23 @@ struct pfsnode *pfs; struct uio *uio; { + struct sbuf sb; char *ps; - int xlen; - char psbuf[64]; + int r, xlen; struct timeval tv; getmicrouptime(&tv); - ps = psbuf; - ps += sprintf(ps, "%ld.%02ld %ld.%02ld\n", + sbuf_new(&sb, NULL, 64, 0); + sbuf_printf(&sb, "%ld.%02ld %ld.%02ld\n", tv.tv_sec, tv.tv_usec / 10000, T2S(cp_time[CP_IDLE]), T2J(cp_time[CP_IDLE]) % 100); - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; } int @@ -343,13 +353,14 @@ struct uio *uio; { struct eproc ep; - char *ps, psbuf[1024]; - int xlen; + struct sbuf sb; + char *ps; + int r, xlen; fill_eproc(p, &ep); - ps = psbuf; - ps += sprintf(ps, "%d", p->p_pid); -#define PS_ADD(name, fmt, arg) ps += sprintf(ps, " " fmt, arg) + sbuf_new(&sb, NULL, 1024, 0); + sbuf_printf(&sb, "%d", p->p_pid); +#define PS_ADD(name, fmt, arg) sbuf_printf(&sb, " " fmt, arg) PS_ADD("comm", "(%s)", p->p_comm); PS_ADD("statr", "%c", '0'); /* XXX */ PS_ADD("ppid", "%d", p->p_pptr ? p->p_pptr->p_pid : 0); @@ -389,13 +400,15 @@ PS_ADD("exitsignal", "%d", 0); /* XXX */ PS_ADD("processor", "%d", 0); /* XXX */ #undef PS_ADD - ps += sprintf(ps, "\n"); + sbuf_putc(&sb, '\n'); - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; } /* @@ -421,13 +434,14 @@ struct uio *uio; { struct eproc ep; - char *ps, psbuf[1024]; + struct sbuf sb; + char *ps; char *state; - int i, xlen; + int i, r, xlen; segsz_t lsize; - ps = psbuf; - + sbuf_new(&sb, NULL, 1024, 0); + mtx_enter(&sched_lock, MTX_SPIN); if (p->p_stat > sizeof state_str / sizeof *state_str) state = state_str[0]; @@ -436,29 +450,28 @@ mtx_exit(&sched_lock, MTX_SPIN); fill_eproc(p, &ep); -#define PS_ADD ps += sprintf - PS_ADD(ps, "Name:\t%s\n", p->p_comm); /* XXX escape */ - PS_ADD(ps, "State:\t%s\n", state); + sbuf_printf(&sb, "Name:\t%s\n", p->p_comm); /* XXX escape */ + sbuf_printf(&sb, "State:\t%s\n", state); /* * Credentials */ - PS_ADD(ps, "Pid:\t%d\n", p->p_pid); - PS_ADD(ps, "PPid:\t%d\n", p->p_pptr ? p->p_pptr->p_pid : 0); - PS_ADD(ps, "Uid:\t%d %d %d %d\n", p->p_cred->p_ruid, - p->p_ucred->cr_uid, - p->p_cred->p_svuid, - /* FreeBSD doesn't have fsuid */ - p->p_ucred->cr_uid); - PS_ADD(ps, "Gid:\t%d %d %d %d\n", p->p_cred->p_rgid, - p->p_ucred->cr_gid, - p->p_cred->p_svgid, - /* FreeBSD doesn't have fsgid */ - p->p_ucred->cr_gid); - PS_ADD(ps, "Groups:\t"); + sbuf_printf(&sb, "Pid:\t%d\n", p->p_pid); + sbuf_printf(&sb, "PPid:\t%d\n", p->p_pptr ? p->p_pptr->p_pid : 0); + sbuf_printf(&sb, "Uid:\t%d %d %d %d\n", p->p_cred->p_ruid, + p->p_ucred->cr_uid, + p->p_cred->p_svuid, + /* FreeBSD doesn't have fsuid */ + p->p_ucred->cr_uid); + sbuf_printf(&sb, "Gid:\t%d %d %d %d\n", p->p_cred->p_rgid, + p->p_ucred->cr_gid, + p->p_cred->p_svgid, + /* FreeBSD doesn't have fsgid */ + p->p_ucred->cr_gid); + sbuf_printf(&sb, "Groups:\t"); for (i = 0; i < p->p_ucred->cr_ngroups; i++) - PS_ADD(ps, "%d ", p->p_ucred->cr_groups[i]); - PS_ADD(ps, "\n"); + sbuf_printf(&sb, "%d ", p->p_ucred->cr_groups[i]); + sbuf_printf(&sb, "\n"); /* * Memory @@ -471,15 +484,15 @@ * could also compute VmLck, but I don't really care enough to * implement it. Submissions are welcome. */ - PS_ADD(ps, "VmSize:\t%8u kB\n", B2K(ep.e_vm.vm_map.size)); - PS_ADD(ps, "VmLck:\t%8u kB\n", P2K(0)); /* XXX */ - PS_ADD(ps, "VmRss:\t%8u kB\n", P2K(ep.e_vm.vm_rssize)); - PS_ADD(ps, "VmData:\t%8u kB\n", P2K(ep.e_vm.vm_dsize)); - PS_ADD(ps, "VmStk:\t%8u kB\n", P2K(ep.e_vm.vm_ssize)); - PS_ADD(ps, "VmExe:\t%8u kB\n", P2K(ep.e_vm.vm_tsize)); + sbuf_printf(&sb, "VmSize:\t%8u kB\n", B2K(ep.e_vm.vm_map.size)); + sbuf_printf(&sb, "VmLck:\t%8u kB\n", P2K(0)); /* XXX */ + sbuf_printf(&sb, "VmRss:\t%8u kB\n", P2K(ep.e_vm.vm_rssize)); + sbuf_printf(&sb, "VmData:\t%8u kB\n", P2K(ep.e_vm.vm_dsize)); + sbuf_printf(&sb, "VmStk:\t%8u kB\n", P2K(ep.e_vm.vm_ssize)); + sbuf_printf(&sb, "VmExe:\t%8u kB\n", P2K(ep.e_vm.vm_tsize)); lsize = B2P(ep.e_vm.vm_map.size) - ep.e_vm.vm_dsize - ep.e_vm.vm_ssize - ep.e_vm.vm_tsize - 1; - PS_ADD(ps, "VmLib:\t%8u kB\n", P2K(lsize)); + sbuf_printf(&sb, "VmLib:\t%8u kB\n", P2K(lsize)); /* * Signal masks @@ -492,28 +505,29 @@ * supports 64 signals, but this code is a long way from * running on anything but i386, so ignore that for now. */ - PS_ADD(ps, "SigPnd:\t%08x\n", p->p_siglist.__bits[0]); + sbuf_printf(&sb, "SigPnd:\t%08x\n", p->p_siglist.__bits[0]); /* * I can't seem to find out where the signal mask is in * relation to struct proc, so SigBlk is left unimplemented. */ - PS_ADD(ps, "SigBlk:\t%08x\n", 0); /* XXX */ - PS_ADD(ps, "SigIgn:\t%08x\n", p->p_sigignore.__bits[0]); - PS_ADD(ps, "SigCgt:\t%08x\n", p->p_sigcatch.__bits[0]); + sbuf_printf(&sb, "SigBlk:\t%08x\n", 0); /* XXX */ + sbuf_printf(&sb, "SigIgn:\t%08x\n", p->p_sigignore.__bits[0]); + sbuf_printf(&sb, "SigCgt:\t%08x\n", p->p_sigcatch.__bits[0]); /* * Linux also prints the capability masks, but we don't have * capabilities yet, and when we do get them they're likely to * be meaningless to Linux programs, so we lie. XXX */ - PS_ADD(ps, "CapInh:\t%016x\n", 0); - PS_ADD(ps, "CapPrm:\t%016x\n", 0); - PS_ADD(ps, "CapEff:\t%016x\n", 0); -#undef PS_ADD - - xlen = ps - psbuf; - xlen -= uio->uio_offset; - ps = psbuf + uio->uio_offset; + sbuf_printf(&sb, "CapInh:\t%016x\n", 0); + sbuf_printf(&sb, "CapPrm:\t%016x\n", 0); + sbuf_printf(&sb, "CapEff:\t%016x\n", 0); + + sbuf_finish(&sb); + ps = sbuf_data(&sb) + uio->uio_offset; + xlen = sbuf_len(&sb) - uio->uio_offset; xlen = imin(xlen, uio->uio_resid); - return (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + r = (xlen <= 0 ? 0 : uiomove(ps, xlen, uio)); + sbuf_delete(&sb); + return r; }