Home | History | Annotate | Line # | Download | only in ping
ping.c revision 1.1.2.2
      1  1.1.2.2  snj /*	$NetBSD: ping.c,v 1.1.2.2 2015/06/03 06:51:40 snj Exp $	*/
      2  1.1.2.2  snj 
      3  1.1.2.2  snj /*-
      4  1.1.2.2  snj  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1.2.2  snj  * All rights reserved.
      6  1.1.2.2  snj  *
      7  1.1.2.2  snj  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  snj  * modification, are permitted provided that the following conditions
      9  1.1.2.2  snj  * are met:
     10  1.1.2.2  snj  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  snj  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  snj  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  snj  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  snj  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  snj  *
     16  1.1.2.2  snj  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1.2.2  snj  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.2.2  snj  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.2.2  snj  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1.2.2  snj  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.2.2  snj  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.2.2  snj  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.2.2  snj  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.2.2  snj  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.2.2  snj  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.2.2  snj  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  snj  */
     28  1.1.2.2  snj 
     29  1.1.2.2  snj #include <sys/cdefs.h>
     30  1.1.2.2  snj __KERNEL_RCSID(0, "$NetBSD: ping.c,v 1.1.2.2 2015/06/03 06:51:40 snj Exp $");
     31  1.1.2.2  snj 
     32  1.1.2.2  snj #include <sys/param.h>
     33  1.1.2.2  snj #include <sys/conf.h>
     34  1.1.2.2  snj #include <sys/device.h>
     35  1.1.2.2  snj #include <sys/kernel.h>
     36  1.1.2.2  snj #include <sys/module.h>
     37  1.1.2.2  snj 
     38  1.1.2.2  snj #include "ping.h"
     39  1.1.2.2  snj 
     40  1.1.2.2  snj /*
     41  1.1.2.2  snj  * Create a device /dev/ping in order to test this module.
     42  1.1.2.2  snj  *
     43  1.1.2.2  snj  * To use this device you need to do:
     44  1.1.2.2  snj  *     mknod /dev/ping c 210 0
     45  1.1.2.2  snj  *
     46  1.1.2.2  snj  */
     47  1.1.2.2  snj 
     48  1.1.2.2  snj dev_type_open(ping_open);
     49  1.1.2.2  snj dev_type_close(ping_close);
     50  1.1.2.2  snj dev_type_ioctl(ping_ioctl);
     51  1.1.2.2  snj 
     52  1.1.2.2  snj static struct cdevsw ping_cdevsw = {
     53  1.1.2.2  snj 	.d_open = ping_open,
     54  1.1.2.2  snj 	.d_close = ping_close,
     55  1.1.2.2  snj 	.d_read = noread,
     56  1.1.2.2  snj 	.d_write = nowrite,
     57  1.1.2.2  snj 	.d_ioctl = ping_ioctl,
     58  1.1.2.2  snj 	.d_stop = nostop,
     59  1.1.2.2  snj 	.d_tty = notty,
     60  1.1.2.2  snj 	.d_poll = nopoll,
     61  1.1.2.2  snj 	.d_mmap = nommap,
     62  1.1.2.2  snj 	.d_kqfilter = nokqfilter,
     63  1.1.2.2  snj 	.d_discard = nodiscard,
     64  1.1.2.2  snj 	.d_flag = D_OTHER
     65  1.1.2.2  snj };
     66  1.1.2.2  snj 
     67  1.1.2.2  snj 
     68  1.1.2.2  snj struct ping_softc {
     69  1.1.2.2  snj 	int		refcnt;
     70  1.1.2.2  snj };
     71  1.1.2.2  snj 
     72  1.1.2.2  snj static struct ping_softc sc;
     73  1.1.2.2  snj 
     74  1.1.2.2  snj int
     75  1.1.2.2  snj ping_open(dev_t self __unused, int flag __unused, int mode __unused,
     76  1.1.2.2  snj           struct lwp *l __unused)
     77  1.1.2.2  snj {
     78  1.1.2.2  snj 	if (sc.refcnt > 0)
     79  1.1.2.2  snj 		return EBUSY;
     80  1.1.2.2  snj 
     81  1.1.2.2  snj 	++sc.refcnt;
     82  1.1.2.2  snj 
     83  1.1.2.2  snj 	return 0;
     84  1.1.2.2  snj }
     85  1.1.2.2  snj 
     86  1.1.2.2  snj int
     87  1.1.2.2  snj ping_close(dev_t self __unused, int flag __unused, int mode __unused,
     88  1.1.2.2  snj            struct lwp *l __unused)
     89  1.1.2.2  snj {
     90  1.1.2.2  snj 	--sc.refcnt;
     91  1.1.2.2  snj 
     92  1.1.2.2  snj 	return 0;
     93  1.1.2.2  snj }
     94  1.1.2.2  snj 
     95  1.1.2.2  snj int
     96  1.1.2.2  snj ping_ioctl(dev_t self __unused, u_long cmd, void *data, int flag,
     97  1.1.2.2  snj            struct lwp *l __unused)
     98  1.1.2.2  snj {
     99  1.1.2.2  snj 	switch(cmd) {
    100  1.1.2.2  snj 	case CMD_PING:
    101  1.1.2.2  snj 		printf("ping: pong!\n");
    102  1.1.2.2  snj 		return 0;
    103  1.1.2.2  snj 	default:
    104  1.1.2.2  snj 		return 1;
    105  1.1.2.2  snj 	}
    106  1.1.2.2  snj }
    107  1.1.2.2  snj 
    108  1.1.2.2  snj MODULE(MODULE_CLASS_MISC, ping, NULL);
    109  1.1.2.2  snj 
    110  1.1.2.2  snj static int
    111  1.1.2.2  snj ping_modcmd(modcmd_t cmd, void *arg __unused)
    112  1.1.2.2  snj {
    113  1.1.2.2  snj 	/* The major should be verified and changed if needed to avoid
    114  1.1.2.2  snj 	 * conflicts with other devices. */
    115  1.1.2.2  snj 	int cmajor = 210, bmajor = -1;
    116  1.1.2.2  snj 
    117  1.1.2.2  snj 	switch (cmd) {
    118  1.1.2.2  snj 	case MODULE_CMD_INIT:
    119  1.1.2.2  snj 		if (devsw_attach("ping", NULL, &bmajor, &ping_cdevsw, &cmajor))
    120  1.1.2.2  snj 			return ENXIO;
    121  1.1.2.2  snj 		return 0;
    122  1.1.2.2  snj 	case MODULE_CMD_FINI:
    123  1.1.2.2  snj 		if (sc.refcnt > 0)
    124  1.1.2.2  snj 			return EBUSY;
    125  1.1.2.2  snj 
    126  1.1.2.2  snj 		devsw_detach(NULL, &ping_cdevsw);
    127  1.1.2.2  snj 		return 0;
    128  1.1.2.2  snj 	default:
    129  1.1.2.2  snj 		return ENOTTY;
    130  1.1.2.2  snj 	}
    131  1.1.2.2  snj }
    132