17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 569bb4bb4Scarlsonj * Common Development and Distribution License (the "License"). 669bb4bb4Scarlsonj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*a1196271SJames Carlson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 27*a1196271SJames Carlson #include <sys/ctfs.h> 28*a1196271SJames Carlson #include <sys/contract/process.h> 297c478bd9Sstevel@tonic-gate #include <sys/socket.h> 307c478bd9Sstevel@tonic-gate #include <sys/time.h> 31*a1196271SJames Carlson #include <sys/wait.h> 32*a1196271SJames Carlson #include <fcntl.h> 33*a1196271SJames Carlson #include <libcontract.h> 34*a1196271SJames Carlson #include <libcontract_priv.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 39*a1196271SJames Carlson 407c478bd9Sstevel@tonic-gate #include "dhcpagent_ipc.h" 417c478bd9Sstevel@tonic-gate #include "dhcpagent_util.h" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * Strings returned by dhcp_status_hdr_string() and 457c478bd9Sstevel@tonic-gate * dhcp_status_reply_to_string(). The first define is the header line, and 467c478bd9Sstevel@tonic-gate * the second defines line printed underneath. 477c478bd9Sstevel@tonic-gate * The spacing of fields must match. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate #define DHCP_STATUS_HDR "Interface State Sent Recv Declined Flags\n" 507c478bd9Sstevel@tonic-gate #define DHCP_STATUS_STR "%-10s %-12s %5d %5d %9d " 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static const char *time_to_string(time_t abs_time); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * dhcp_state_to_string(): given a state, provides the state's name 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * input: DHCPSTATE: the state to get the name of 587c478bd9Sstevel@tonic-gate * output: const char *: the state's name 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate const char * 627c478bd9Sstevel@tonic-gate dhcp_state_to_string(DHCPSTATE state) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate const char *states[] = { 657c478bd9Sstevel@tonic-gate "INIT", 667c478bd9Sstevel@tonic-gate "SELECTING", 677c478bd9Sstevel@tonic-gate "REQUESTING", 6869bb4bb4Scarlsonj "PRE_BOUND", 697c478bd9Sstevel@tonic-gate "BOUND", 707c478bd9Sstevel@tonic-gate "RENEWING", 717c478bd9Sstevel@tonic-gate "REBINDING", 727c478bd9Sstevel@tonic-gate "INFORMATION", 737c478bd9Sstevel@tonic-gate "INIT_REBOOT", 747c478bd9Sstevel@tonic-gate "ADOPTING", 75d04ccbb3Scarlsonj "INFORM_SENT", 76d04ccbb3Scarlsonj "DECLINING", 77d04ccbb3Scarlsonj "RELEASING" 787c478bd9Sstevel@tonic-gate }; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate if (state < 0 || state >= DHCP_NSTATES) 817c478bd9Sstevel@tonic-gate return ("<unknown>"); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate return (states[state]); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 86*a1196271SJames Carlson static int 87*a1196271SJames Carlson init_template(void) 88*a1196271SJames Carlson { 89*a1196271SJames Carlson int fd; 90*a1196271SJames Carlson int err = 0; 91*a1196271SJames Carlson 92*a1196271SJames Carlson fd = open64(CTFS_ROOT "/process/template", O_RDWR); 93*a1196271SJames Carlson if (fd == -1) 94*a1196271SJames Carlson return (-1); 95*a1196271SJames Carlson 96*a1196271SJames Carlson /* 97*a1196271SJames Carlson * Deliver no events, don't inherit, and allow it to be orphaned. 98*a1196271SJames Carlson */ 99*a1196271SJames Carlson err |= ct_tmpl_set_critical(fd, 0); 100*a1196271SJames Carlson err |= ct_tmpl_set_informative(fd, 0); 101*a1196271SJames Carlson err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 102*a1196271SJames Carlson err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 103*a1196271SJames Carlson if (err != 0 || ct_tmpl_activate(fd) != 0) { 104*a1196271SJames Carlson (void) close(fd); 105*a1196271SJames Carlson return (-1); 106*a1196271SJames Carlson } 107*a1196271SJames Carlson 108*a1196271SJames Carlson return (fd); 109*a1196271SJames Carlson } 110*a1196271SJames Carlson 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * dhcp_start_agent(): starts the agent if not already running 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * input: int: number of seconds to wait for agent to start (-1 is forever) 1157c478bd9Sstevel@tonic-gate * output: int: 0 on success, -1 on failure 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate int 1197c478bd9Sstevel@tonic-gate dhcp_start_agent(int timeout) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate int error; 1227c478bd9Sstevel@tonic-gate time_t start_time = time(NULL); 1237c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request; 1247c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 125*a1196271SJames Carlson int ctfd; 126*a1196271SJames Carlson pid_t childpid; 127*a1196271SJames Carlson ctid_t ct; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * just send a dummy request to the agent to find out if it's 1317c478bd9Sstevel@tonic-gate * up. we do this instead of directly connecting to it since 1327c478bd9Sstevel@tonic-gate * we want to make sure we follow its IPC conventions 1337c478bd9Sstevel@tonic-gate * (otherwise, it will log warnings to syslog). 1347c478bd9Sstevel@tonic-gate */ 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_PING, "", NULL, 0, 1377c478bd9Sstevel@tonic-gate DHCP_TYPE_NONE); 1387c478bd9Sstevel@tonic-gate if (request == NULL) 1397c478bd9Sstevel@tonic-gate return (-1); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, 0); 1427c478bd9Sstevel@tonic-gate if (error == 0) { 1437c478bd9Sstevel@tonic-gate free(reply); 1447c478bd9Sstevel@tonic-gate free(request); 1457c478bd9Sstevel@tonic-gate return (0); 1467c478bd9Sstevel@tonic-gate } 147*a1196271SJames Carlson if (error != DHCP_IPC_E_CONNECT) 148*a1196271SJames Carlson goto fail; 1497c478bd9Sstevel@tonic-gate 150*a1196271SJames Carlson if ((ctfd = init_template()) == -1) 151*a1196271SJames Carlson goto fail; 1527c478bd9Sstevel@tonic-gate 153*a1196271SJames Carlson childpid = fork(); 154*a1196271SJames Carlson 155*a1196271SJames Carlson (void) ct_tmpl_clear(ctfd); 156*a1196271SJames Carlson (void) close(ctfd); 157*a1196271SJames Carlson 158*a1196271SJames Carlson switch (childpid) { 1597c478bd9Sstevel@tonic-gate case -1: 160*a1196271SJames Carlson goto fail; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate case 0: 1637c478bd9Sstevel@tonic-gate (void) execl(DHCP_AGENT_PATH, DHCP_AGENT_PATH, (char *)0); 1647c478bd9Sstevel@tonic-gate _exit(EXIT_FAILURE); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate default: 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 170*a1196271SJames Carlson /* wait for the daemon to run and then abandon the contract */ 171*a1196271SJames Carlson (void) waitpid(childpid, NULL, 0); 172*a1196271SJames Carlson 173*a1196271SJames Carlson if (contract_latest(&ct) != -1) 174*a1196271SJames Carlson (void) contract_abandon_id(ct); 175*a1196271SJames Carlson 1767c478bd9Sstevel@tonic-gate while ((timeout != -1) && (time(NULL) - start_time < timeout)) { 1777c478bd9Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, 0); 1787c478bd9Sstevel@tonic-gate if (error == 0) { 1797c478bd9Sstevel@tonic-gate free(reply); 1807c478bd9Sstevel@tonic-gate free(request); 1817c478bd9Sstevel@tonic-gate return (0); 1827c478bd9Sstevel@tonic-gate } else if (error != DHCP_IPC_E_CONNECT) 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate (void) sleep(1); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 187*a1196271SJames Carlson fail: 1887c478bd9Sstevel@tonic-gate free(request); 1897c478bd9Sstevel@tonic-gate return (-1); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * dhcp_status_hdr_string(): Return a string suitable to use as the header 1947c478bd9Sstevel@tonic-gate * when printing DHCP_STATUS reply. 1957c478bd9Sstevel@tonic-gate * output: const char *: newline terminated printable string 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate const char * 1987c478bd9Sstevel@tonic-gate dhcp_status_hdr_string(void) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate return (DHCP_STATUS_HDR); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * time_to_string(): Utility routine for printing time 2057c478bd9Sstevel@tonic-gate * 2067c478bd9Sstevel@tonic-gate * input: time_t *: time_t to stringify 2077c478bd9Sstevel@tonic-gate * output: const char *: printable time 2087c478bd9Sstevel@tonic-gate */ 2097c478bd9Sstevel@tonic-gate static const char * 2107c478bd9Sstevel@tonic-gate time_to_string(time_t abs_time) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate static char time_buf[24]; 2137c478bd9Sstevel@tonic-gate time_t tm = abs_time; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate if (tm == DHCP_PERM) 2167c478bd9Sstevel@tonic-gate return ("Never"); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (strftime(time_buf, sizeof (time_buf), "%m/%d/%Y %R", 2197c478bd9Sstevel@tonic-gate localtime(&tm)) == 0) 2207c478bd9Sstevel@tonic-gate return ("<unknown>"); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate return (time_buf); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * dhcp_status_reply_to_string(): Return DHCP IPC reply of type DHCP_STATUS 2277c478bd9Sstevel@tonic-gate * as a printable string 2287c478bd9Sstevel@tonic-gate * 2297c478bd9Sstevel@tonic-gate * input: dhcp_reply_t *: contains the status structure to print 2307c478bd9Sstevel@tonic-gate * output: const char *: newline terminated printable string 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate const char * 2337c478bd9Sstevel@tonic-gate dhcp_status_reply_to_string(dhcp_ipc_reply_t *reply) 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate static char str[1024]; 2367c478bd9Sstevel@tonic-gate size_t reply_size; 2377c478bd9Sstevel@tonic-gate dhcp_status_t *status; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate status = dhcp_ipc_get_data(reply, &reply_size, NULL); 2407c478bd9Sstevel@tonic-gate if (reply_size < DHCP_STATUS_VER1_SIZE) 2417c478bd9Sstevel@tonic-gate return ("<Internal error: status msg size>\n"); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate (void) snprintf(str, sizeof (str), DHCP_STATUS_STR, 2447c478bd9Sstevel@tonic-gate status->if_name, dhcp_state_to_string(status->if_state), 2457c478bd9Sstevel@tonic-gate status->if_sent, status->if_recv, status->if_bad_offers); 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_PRIMARY) 2487c478bd9Sstevel@tonic-gate (void) strlcat(str, "[PRIMARY] ", sizeof (str)); 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_BOOTP) 2517c478bd9Sstevel@tonic-gate (void) strlcat(str, "[BOOTP] ", sizeof (str)); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_FAILED) 2547c478bd9Sstevel@tonic-gate (void) strlcat(str, "[FAILED] ", sizeof (str)); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_BUSY) 2577c478bd9Sstevel@tonic-gate (void) strlcat(str, "[BUSY] ", sizeof (str)); 2587c478bd9Sstevel@tonic-gate 259d04ccbb3Scarlsonj if (status->if_dflags & DHCP_IF_V6) 260d04ccbb3Scarlsonj (void) strlcat(str, "[V6] ", sizeof (str)); 261d04ccbb3Scarlsonj 2627c478bd9Sstevel@tonic-gate (void) strlcat(str, "\n", sizeof (str)); 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate switch (status->if_state) { 2657c478bd9Sstevel@tonic-gate case BOUND: 2667c478bd9Sstevel@tonic-gate case RENEWING: 2677c478bd9Sstevel@tonic-gate case REBINDING: 2687c478bd9Sstevel@tonic-gate break; 2697c478bd9Sstevel@tonic-gate default: 2707c478bd9Sstevel@tonic-gate return (str); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate (void) strlcat(str, "(Began, Expires, Renew) = (", sizeof (str)); 2747c478bd9Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_began), sizeof (str)); 2757c478bd9Sstevel@tonic-gate (void) strlcat(str, ", ", sizeof (str)); 2767c478bd9Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_lease), sizeof (str)); 2777c478bd9Sstevel@tonic-gate (void) strlcat(str, ", ", sizeof (str)); 2787c478bd9Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_t1), sizeof (str)); 2797c478bd9Sstevel@tonic-gate (void) strlcat(str, ")\n", sizeof (str)); 2807c478bd9Sstevel@tonic-gate return (str); 2817c478bd9Sstevel@tonic-gate } 282