Created
August 9, 2012 08:20
-
-
Save nonakap/3302296 to your computer and use it in GitHub Desktop.
Fizz Buzz on procfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/sys/miscfs/procfs/procfs.h b/sys/miscfs/procfs/procfs.h | |
index 170368f..e093a59 100644 | |
--- a/sys/miscfs/procfs/procfs.h | |
+++ b/sys/miscfs/procfs/procfs.h | |
@@ -110,6 +110,7 @@ typedef enum { | |
PFSstatm, /* process memory info (if -o linux) */ | |
PFSversion, /* kernel version (if -o linux) */ | |
PFStask, /* task subdirector (if -o linux) */ | |
+ PFSfizzbuzz, /* Fizz Buzz */ | |
#ifdef __HAVE_PROCFS_MACHDEP | |
PROCFS_MACHDEP_NODE_TYPES | |
#endif | |
@@ -230,6 +231,8 @@ int procfs_doemul(struct lwp *, struct proc *, struct pfsnode *, | |
struct uio *); | |
int procfs_doversion(struct lwp *, struct proc *, struct pfsnode *, | |
struct uio *); | |
+int procfs_dofizzbuzz(struct lwp *, struct proc *, struct pfsnode *, | |
+ struct uio *); | |
void procfs_revoke_vnodes(struct proc *, void *); | |
void procfs_hashinit(void); | |
diff --git a/sys/miscfs/procfs/procfs_fizzbuzz.c b/sys/miscfs/procfs/procfs_fizzbuzz.c | |
new file mode 100644 | |
index 0000000..243d7c7 | |
--- /dev/null | |
+++ b/sys/miscfs/procfs/procfs_fizzbuzz.c | |
@@ -0,0 +1,97 @@ | |
+/* $NetBSD$ */ | |
+ | |
+/*- | |
+ * Copyright (C) 2012 NONAKA Kimihiro <[email protected]> | |
+ * All rights reserved. | |
+ * | |
+ * Redistribution and use in source and binary forms, with or without | |
+ * modification, are permitted provided that the following conditions | |
+ * are met: | |
+ * 1. Redistributions of source code must retain the above copyright | |
+ * notice, this list of conditions and the following disclaimer. | |
+ * 2. Redistributions in binary form must reproduce the above copyright | |
+ * notice, this list of conditions and the following disclaimer in the | |
+ * documentation and/or other materials provided with the distribution. | |
+ * | |
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
+ */ | |
+ | |
+#include <sys/cdefs.h> | |
+__KERNEL_RCSID(0, "$NetBSD$"); | |
+ | |
+#include <sys/param.h> | |
+#include <sys/systm.h> | |
+#include <sys/time.h> | |
+#include <sys/kernel.h> | |
+#include <sys/proc.h> | |
+#include <sys/vnode.h> | |
+#include <sys/exec.h> | |
+#include <sys/resource.h> | |
+#include <sys/resourcevar.h> | |
+#include <sys/signal.h> | |
+#include <sys/signalvar.h> | |
+#include <sys/tty.h> | |
+#include <sys/malloc.h> | |
+#include <sys/mount.h> | |
+#include <sys/conf.h> | |
+#include <sys/sysctl.h> | |
+#include <sys/kauth.h> | |
+#include <sys/filedesc.h> | |
+ | |
+#include <miscfs/procfs/procfs.h> | |
+ | |
+#include <uvm/uvm_extern.h> | |
+#include <uvm/uvm.h> | |
+ | |
+extern struct devsw_conv *devsw_conv; | |
+extern int max_devsw_convs; | |
+ | |
+#define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT) | |
+#define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10)) | |
+ | |
+#define LBFSZ (8 * 1024) | |
+ | |
+int | |
+procfs_dofizzbuzz(struct lwp *curl, struct proc *p, | |
+ struct pfsnode *pfs, struct uio *uio) | |
+{ | |
+ char *bf; | |
+ char fizzbuzz[sizeof("あずにゃんぺろぺろ")]; | |
+ int len; | |
+ int error = 0; | |
+ | |
+ bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | |
+ | |
+ sysctl_lock(false); | |
+ | |
+ if (p->p_pid == 0) { | |
+ strlcpy(fizzbuzz, "あずにゃんぺろぺろ", sizeof(fizzbuzz)); | |
+ } else if ((p->p_pid % 15) == 0) { | |
+ strlcpy(fizzbuzz, "あずにゃん", sizeof(fizzbuzz)); | |
+ } else if ((p->p_pid % 3) == 0) { | |
+ strlcpy(fizzbuzz, "あず", sizeof(fizzbuzz)); | |
+ } else if ((p->p_pid % 5) == 0) { | |
+ strlcpy(fizzbuzz, "にゃん", sizeof(fizzbuzz)); | |
+ } else { | |
+ snprintf(fizzbuzz, sizeof(fizzbuzz), "%d", p->p_pid); | |
+ } | |
+ len = snprintf(bf, LBFSZ, "%s\r\n", fizzbuzz); | |
+ | |
+ if (len == 0) | |
+ goto out; | |
+ | |
+ error = uiomove_frombuf(bf, len, uio); | |
+out: | |
+ free(bf, M_TEMP); | |
+ sysctl_unlock(); | |
+ return error; | |
+} | |
diff --git a/sys/miscfs/procfs/procfs_subr.c b/sys/miscfs/procfs/procfs_subr.c | |
index 8b67ced..8886d7c 100644 | |
--- a/sys/miscfs/procfs/procfs_subr.c | |
+++ b/sys/miscfs/procfs/procfs_subr.c | |
@@ -297,6 +297,7 @@ procfs_allocvp(struct mount *mp, struct vnode **vpp, pid_t pid, | |
case PFSloadavg: /* /proc/loadavg = -r--r--r-- */ | |
case PFSstatm: /* /proc/N/statm = -r--r--r-- */ | |
case PFSversion: /* /proc/version = -r--r--r-- */ | |
+ case PFSfizzbuzz: /* /proc/N/fizzbuzz = -r--r--r-- */ | |
pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH; | |
vp->v_type = VREG; | |
break; | |
@@ -475,6 +476,10 @@ procfs_rw(void *v) | |
error = procfs_doversion(curl, p, pfs, uio); | |
break; | |
+ case PFSfizzbuzz: | |
+ error = procfs_dofizzbuzz(curl, p, pfs, uio); | |
+ break; | |
+ | |
#ifdef __HAVE_PROCFS_MACHDEP | |
PROCFS_MACHDEP_NODETYPE_CASES | |
error = procfs_machdep_rw(curl, l, pfs, uio); | |
diff --git a/sys/miscfs/procfs/procfs_vfsops.c b/sys/miscfs/procfs/procfs_vfsops.c | |
index aac4e1e..00a7e6d 100644 | |
--- a/sys/miscfs/procfs/procfs_vfsops.c | |
+++ b/sys/miscfs/procfs/procfs_vfsops.c | |
@@ -360,7 +360,7 @@ procfs_modcmd(modcmd_t cmd, void *arg) | |
sysctl_createv(&procfs_sysctl_log, 0, NULL, NULL, | |
CTLFLAG_PERMANENT, | |
CTLTYPE_NODE, "procfs", | |
- SYSCTL_DESCR("Process file system"), | |
+ SYSCTL_DESCR("Fizz Buzz"), | |
NULL, 0, NULL, 0, | |
CTL_VFS, 12, CTL_EOL); | |
/* | |
diff --git a/sys/miscfs/procfs/procfs_vnops.c b/sys/miscfs/procfs/procfs_vnops.c | |
index 89e41bf..7f9e9ff8 100644 | |
--- a/sys/miscfs/procfs/procfs_vnops.c | |
+++ b/sys/miscfs/procfs/procfs_vnops.c | |
@@ -176,6 +176,7 @@ static const struct proc_target { | |
{ DT_LNK, N("emul"), PFSemul, NULL }, | |
{ DT_REG, N("statm"), PFSstatm, procfs_validfile_linux }, | |
{ DT_DIR, N("task"), PFStask, procfs_validfile_linux }, | |
+ { DT_REG, N("fizzbuzz"),PFSfizzbuzz, NULL }, | |
#ifdef __HAVE_PROCFS_MACHDEP | |
PROCFS_MACHDEP_NODETYPE_DEFNS | |
#endif | |
@@ -745,6 +746,7 @@ procfs_getattr(void *v) | |
case PFScpustat: | |
case PFSloadavg: | |
case PFSversion: | |
+ case PFSfizzbuzz: | |
vap->va_nlink = 1; | |
vap->va_uid = vap->va_gid = 0; | |
break; | |
@@ -865,6 +867,7 @@ procfs_getattr(void *v) | |
case PFSloadavg: | |
case PFSstatm: | |
case PFSversion: | |
+ case PFSfizzbuzz: | |
vap->va_bytes = vap->va_size = 0; | |
break; | |
case PFSmap: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment