utx.c revision 272461
11638Srgrimes/*-
252696Sgreen * Copyright (c) 2011-2012 Ed Schouten <ed@FreeBSD.org>
31638Srgrimes * All rights reserved.
41988Swollman *
5124747Sru * Redistribution and use in source and binary forms, with or without
6124747Sru * modification, are permitted provided that the following conditions
7152999Semax * are met:
8124747Sru * 1. Redistributions of source code must retain the above copyright
9124747Sru *    notice, this list of conditions and the following disclaimer.
10124747Sru * 2. Redistributions in binary form must reproduce the above copyright
11124747Sru *    notice, this list of conditions and the following disclaimer in the
12124747Sru *    documentation and/or other materials provided with the distribution.
13146329Skeramida *
14146329Skeramida * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15124747Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16124747Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17124747Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18124747Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19124747Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129759Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21124747Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22124747Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23124747Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24124747Sru * SUCH DAMAGE.
25124747Sru */
26124747Sru
27124747Sru#include <sys/cdefs.h>
28124747Sru__FBSDID("$FreeBSD: releng/10.1/usr.sbin/utx/utx.c 231530 2012-02-11 20:28:42Z ed $");
29124747Sru
30124747Sru#include <sys/time.h>
31124747Sru#include <errno.h>
32124747Sru#include <ctype.h>
33124747Sru#include <stdio.h>
34124747Sru#include <stdlib.h>
35124747Sru#include <string.h>
36124747Sru#include <utmpx.h>
37124747Sru
38124747Srustatic int
39108304Strhodesb16_pton(const char *in, char *out, size_t len)
40124747Sru{
41124747Sru	size_t i;
42124747Sru
43124747Sru	for (i = 0; i < len * 2; i++)
44124747Sru		if (!isxdigit((unsigned char)in[i]))
45124747Sru			return (1);
46150665Sru	for (i = 0; i < len; i++)
47124747Sru		sscanf(&in[i * 2], "%02hhx", &out[i]);
48124747Sru	return (0);
49124747Sru}
50124747Sru
51124747Srustatic int
52124747Srurm(char *id[])
53124747Sru{
54124747Sru	struct utmpx utx = { .ut_type = DEAD_PROCESS };
55124747Sru	size_t len;
56124747Sru	int ret = 0;
57124747Sru
58124747Sru	(void)gettimeofday(&utx.ut_tv, NULL);
591638Srgrimes	for (; *id != NULL; id++) {
60124747Sru		len = strlen(*id);
6132158Sbde		if (len <= sizeof(utx.ut_id)) {
6232158Sbde			/* Identifier as string. */
63108414Strhodes			strncpy(utx.ut_id, *id, sizeof(utx.ut_id));
6477865Sru		} else if (len != sizeof(utx.ut_id) * 2 ||
6572875Snik		    b16_pton(*id, utx.ut_id, sizeof(utx.ut_id)) != 0) {
6632158Sbde			/* Also not hexadecimal. */
6732158Sbde			fprintf(stderr, "%s: Invalid identifier format\n", *id);
6832158Sbde			ret = 1;
69148781Sphk			continue;
70148781Sphk		}
71148781Sphk
72148781Sphk		/* Zap the entry. */
731638Srgrimes		if (pututxline(&utx) == NULL) {
74			perror(*id);
75			ret = 1;
76		}
77	}
78	return (ret);
79}
80
81static int
82boot(short type)
83{
84	struct utmpx utx = { .ut_type = type };
85
86	(void)gettimeofday(&utx.ut_tv, NULL);
87	if (pututxline(&utx) == NULL) {
88		perror("pututxline");
89		return (1);
90	}
91	return (0);
92}
93
94int
95main(int argc, char *argv[])
96{
97
98	if (argc >= 2 && strcmp(getprogname(), "utxrm") == 0)
99		/* For compatibility. */
100		return (rm(&argv[1]));
101	else if (argc == 2 && strcmp(argv[1], "boot") == 0)
102		return (boot(BOOT_TIME));
103	else if (argc == 2 && strcmp(argv[1], "shutdown") == 0)
104		return (boot(SHUTDOWN_TIME));
105	else if (argc >= 3 && strcmp(argv[1], "rm") == 0)
106		return (rm(&argv[2]));
107
108	fprintf(stderr,
109	    "usage: utx boot\n"
110	    "       utx shutdown\n"
111	    "       utx rm identifier ...\n"
112	    "       utxrm identifier ...\n");
113	exit(1);
114}
115