utxrm.c revision 218847
1208926Smjacob/*-
2208926Smjacob * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3208926Smjacob * All rights reserved.
4208926Smjacob *
5208926Smjacob * Redistribution and use in source and binary forms, with or without
6208926Smjacob * modification, are permitted provided that the following conditions
7208926Smjacob * are met:
8208926Smjacob * 1. Redistributions of source code must retain the above copyright
9208926Smjacob *    notice, this list of conditions and the following disclaimer.
10208926Smjacob * 2. Redistributions in binary form must reproduce the above copyright
11208926Smjacob *    notice, this list of conditions and the following disclaimer in the
12208926Smjacob *    documentation and/or other materials provided with the distribution.
13208926Smjacob *
14208926Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15208926Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16208926Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17208926Smjacob * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18208926Smjacob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19208926Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20208926Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21208926Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22208926Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23208926Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24208926Smjacob * SUCH DAMAGE.
25208926Smjacob */
26208926Smjacob
27208926Smjacob#include <sys/cdefs.h>
28208926Smjacob__FBSDID("$FreeBSD: head/usr.sbin/utxrm/utxrm.c 218847 2011-02-19 11:44:04Z ed $");
29208926Smjacob
30208926Smjacob#include <sys/time.h>
31208926Smjacob#include <errno.h>
32208926Smjacob#include <ctype.h>
33208926Smjacob#include <stdio.h>
34208926Smjacob#include <string.h>
35208926Smjacob#include <utmpx.h>
36208926Smjacob
37208926Smjacobstatic int
38208926Smjacobb16_pton(const char *in, char *out, size_t len)
39208926Smjacob{
40208926Smjacob	size_t i;
41208926Smjacob
42208926Smjacob	for (i = 0; i < len * 2; i++)
43208926Smjacob		if (!isxdigit((unsigned char)in[i]))
44208926Smjacob			return (1);
45208926Smjacob	for (i = 0; i < len; i++)
46208926Smjacob		sscanf(&in[i * 2], "%02hhx", &out[i]);
47208926Smjacob	return (0);
48208926Smjacob}
49208926Smjacob
50208926Smjacobint
51208926Smjacobmain(int argc, char *argv[])
52208926Smjacob{
53208926Smjacob	struct utmpx utx = { .ut_type = DEAD_PROCESS };
54208926Smjacob	size_t len;
55208926Smjacob	int i, ret = 0;
56208926Smjacob
57208926Smjacob	if (argc < 2) {
58208926Smjacob		fprintf(stderr, "usage: utxrm identifier ...\n");
59208926Smjacob		return (1);
60208926Smjacob	}
61208926Smjacob
62208926Smjacob	gettimeofday(&utx.ut_tv, NULL);
63208926Smjacob	for (i = 1; i < argc; i++) {
64208926Smjacob		len = strlen(argv[i]);
65208926Smjacob		if (len <= sizeof(utx.ut_id)) {
66208926Smjacob			/* Identifier as string. */
67208926Smjacob			strncpy(utx.ut_id, argv[i], sizeof(utx.ut_id));
68208926Smjacob		} else if (len != sizeof(utx.ut_id) * 2 ||
69208926Smjacob		    b16_pton(argv[i], utx.ut_id, sizeof(utx.ut_id)) != 0) {
70208926Smjacob			/* Also not hexadecimal. */
71208926Smjacob			fprintf(stderr, "%s: Invalid identifier format\n",
72208926Smjacob			    argv[i]);
73208926Smjacob			ret = 1;
74208926Smjacob			continue;
75208926Smjacob		}
76208926Smjacob
77208926Smjacob		/* Zap the entry. */
78208926Smjacob		if (pututxline(&utx) == NULL) {
79208926Smjacob			perror(argv[i]);
80208926Smjacob			ret = 1;
81208926Smjacob		}
82208926Smjacob	}
83208926Smjacob	return (ret);
84208926Smjacob}
85208926Smjacob