1218847Sed/*- 2231530Sed * Copyright (c) 2011-2012 Ed Schouten <ed@FreeBSD.org> 3218847Sed * All rights reserved. 4218847Sed * 5218847Sed * Redistribution and use in source and binary forms, with or without 6218847Sed * modification, are permitted provided that the following conditions 7218847Sed * are met: 8218847Sed * 1. Redistributions of source code must retain the above copyright 9218847Sed * notice, this list of conditions and the following disclaimer. 10218847Sed * 2. Redistributions in binary form must reproduce the above copyright 11218847Sed * notice, this list of conditions and the following disclaimer in the 12218847Sed * documentation and/or other materials provided with the distribution. 13218847Sed * 14218847Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15218847Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16218847Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17218847Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18218847Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19218847Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20218847Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21218847Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22218847Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23218847Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24218847Sed * SUCH DAMAGE. 25218847Sed */ 26218847Sed 27218847Sed#include <sys/cdefs.h> 28218847Sed__FBSDID("$FreeBSD: releng/10.3/usr.sbin/utx/utx.c 231530 2012-02-11 20:28:42Z ed $"); 29218847Sed 30218847Sed#include <sys/time.h> 31218847Sed#include <errno.h> 32218847Sed#include <ctype.h> 33218847Sed#include <stdio.h> 34231530Sed#include <stdlib.h> 35218847Sed#include <string.h> 36218847Sed#include <utmpx.h> 37218847Sed 38218847Sedstatic int 39218847Sedb16_pton(const char *in, char *out, size_t len) 40218847Sed{ 41218847Sed size_t i; 42218847Sed 43218847Sed for (i = 0; i < len * 2; i++) 44218847Sed if (!isxdigit((unsigned char)in[i])) 45218847Sed return (1); 46218847Sed for (i = 0; i < len; i++) 47218847Sed sscanf(&in[i * 2], "%02hhx", &out[i]); 48218847Sed return (0); 49218847Sed} 50218847Sed 51231530Sedstatic int 52231530Sedrm(char *id[]) 53218847Sed{ 54218847Sed struct utmpx utx = { .ut_type = DEAD_PROCESS }; 55218847Sed size_t len; 56231530Sed int ret = 0; 57218847Sed 58231530Sed (void)gettimeofday(&utx.ut_tv, NULL); 59231530Sed for (; *id != NULL; id++) { 60231530Sed len = strlen(*id); 61218847Sed if (len <= sizeof(utx.ut_id)) { 62218847Sed /* Identifier as string. */ 63231530Sed strncpy(utx.ut_id, *id, sizeof(utx.ut_id)); 64218847Sed } else if (len != sizeof(utx.ut_id) * 2 || 65231530Sed b16_pton(*id, utx.ut_id, sizeof(utx.ut_id)) != 0) { 66218847Sed /* Also not hexadecimal. */ 67231530Sed fprintf(stderr, "%s: Invalid identifier format\n", *id); 68218847Sed ret = 1; 69218847Sed continue; 70218847Sed } 71218847Sed 72218847Sed /* Zap the entry. */ 73218847Sed if (pututxline(&utx) == NULL) { 74231530Sed perror(*id); 75218847Sed ret = 1; 76218847Sed } 77218847Sed } 78218847Sed return (ret); 79218847Sed} 80231530Sed 81231530Sedstatic int 82231530Sedboot(short type) 83231530Sed{ 84231530Sed struct utmpx utx = { .ut_type = type }; 85231530Sed 86231530Sed (void)gettimeofday(&utx.ut_tv, NULL); 87231530Sed if (pututxline(&utx) == NULL) { 88231530Sed perror("pututxline"); 89231530Sed return (1); 90231530Sed } 91231530Sed return (0); 92231530Sed} 93231530Sed 94231530Sedint 95231530Sedmain(int argc, char *argv[]) 96231530Sed{ 97231530Sed 98231530Sed if (argc >= 2 && strcmp(getprogname(), "utxrm") == 0) 99231530Sed /* For compatibility. */ 100231530Sed return (rm(&argv[1])); 101231530Sed else if (argc == 2 && strcmp(argv[1], "boot") == 0) 102231530Sed return (boot(BOOT_TIME)); 103231530Sed else if (argc == 2 && strcmp(argv[1], "shutdown") == 0) 104231530Sed return (boot(SHUTDOWN_TIME)); 105231530Sed else if (argc >= 3 && strcmp(argv[1], "rm") == 0) 106231530Sed return (rm(&argv[2])); 107231530Sed 108231530Sed fprintf(stderr, 109231530Sed "usage: utx boot\n" 110231530Sed " utx shutdown\n" 111231530Sed " utx rm identifier ...\n" 112231530Sed " utxrm identifier ...\n"); 113231530Sed exit(1); 114231530Sed} 115