xref: /netbsd-src/sys/modules/examples/ping/cmd_ping.c (revision c3d5222ef6b1417e959c4ad894f2b4d6e4c37b5b)
1*c3d5222eSjoerg /*	$NetBSD: cmd_ping.c,v 1.2 2015/05/13 12:13:38 joerg Exp $	*/
214e25719Spgoyette 
314e25719Spgoyette /*-
414e25719Spgoyette  * Copyright (c) 2015 The NetBSD Foundation, Inc.
514e25719Spgoyette  * All rights reserved.
614e25719Spgoyette  *
714e25719Spgoyette  * Redistribution and use in source and binary forms, with or without
814e25719Spgoyette  * modification, are permitted provided that the following conditions
914e25719Spgoyette  * are met:
1014e25719Spgoyette  * 1. Redistributions of source code must retain the above copyright
1114e25719Spgoyette  *    notice, this list of conditions and the following disclaimer.
1214e25719Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
1314e25719Spgoyette  *    notice, this list of conditions and the following disclaimer in the
1414e25719Spgoyette  *    documentation and/or other materials provided with the distribution.
1514e25719Spgoyette  *
1614e25719Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1714e25719Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1814e25719Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1914e25719Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2014e25719Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2114e25719Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2214e25719Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2314e25719Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2414e25719Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2514e25719Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2614e25719Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
2714e25719Spgoyette  */
2814e25719Spgoyette 
2914e25719Spgoyette #include <sys/cdefs.h>
30*c3d5222eSjoerg __RCSID("$NetBSD: cmd_ping.c,v 1.2 2015/05/13 12:13:38 joerg Exp $");
3114e25719Spgoyette 
3214e25719Spgoyette #include <err.h>
3314e25719Spgoyette #include <fcntl.h>
3414e25719Spgoyette #include <stdio.h>
3514e25719Spgoyette #include <stdlib.h>
3614e25719Spgoyette #include <sys/ioctl.h>
3714e25719Spgoyette #include <unistd.h>
3814e25719Spgoyette 
3914e25719Spgoyette #include "ping.h"
4014e25719Spgoyette 
4114e25719Spgoyette #define _PATH_DEV_PING "/dev/ping"
4214e25719Spgoyette 
main(int argc,char ** argv)4314e25719Spgoyette int main(int argc, char **argv)
4414e25719Spgoyette {
4514e25719Spgoyette 	int devfd;
4614e25719Spgoyette 
4714e25719Spgoyette 	setprogname(argv[0]);
4814e25719Spgoyette 
4914e25719Spgoyette 	if ((devfd = open(_PATH_DEV_PING, O_RDWR)) == -1)
5014e25719Spgoyette 		err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_PING);
5114e25719Spgoyette 
5214e25719Spgoyette 	if (ioctl(devfd, CMD_PING) == -1)
5314e25719Spgoyette 		err(EXIT_FAILURE, "ping failed");
5414e25719Spgoyette 
5514e25719Spgoyette 	printf("%s: ping sent successfully\n", getprogname());
5614e25719Spgoyette 
5714e25719Spgoyette 	if (close(devfd) == -1)
5814e25719Spgoyette 		err(EXIT_FAILURE, "Cannot close %s", _PATH_DEV_PING);
5914e25719Spgoyette 
6014e25719Spgoyette 	return EXIT_SUCCESS;
6114e25719Spgoyette }
62