10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52546Scarlsonj * Common Development and Distribution License (the "License").
62546Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9633Sjames.d.carlson@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
27*9633Sjames.d.carlson@sun.com #include <sys/ctfs.h>
28*9633Sjames.d.carlson@sun.com #include <sys/contract/process.h>
290Sstevel@tonic-gate #include <sys/socket.h>
300Sstevel@tonic-gate #include <sys/time.h>
31*9633Sjames.d.carlson@sun.com #include <sys/wait.h>
32*9633Sjames.d.carlson@sun.com #include <fcntl.h>
33*9633Sjames.d.carlson@sun.com #include <libcontract.h>
34*9633Sjames.d.carlson@sun.com #include <libcontract_priv.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
39*9633Sjames.d.carlson@sun.com
400Sstevel@tonic-gate #include "dhcpagent_ipc.h"
410Sstevel@tonic-gate #include "dhcpagent_util.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * Strings returned by dhcp_status_hdr_string() and
450Sstevel@tonic-gate * dhcp_status_reply_to_string(). The first define is the header line, and
460Sstevel@tonic-gate * the second defines line printed underneath.
470Sstevel@tonic-gate * The spacing of fields must match.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate #define DHCP_STATUS_HDR "Interface State Sent Recv Declined Flags\n"
500Sstevel@tonic-gate #define DHCP_STATUS_STR "%-10s %-12s %5d %5d %9d "
510Sstevel@tonic-gate
520Sstevel@tonic-gate static const char *time_to_string(time_t abs_time);
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * dhcp_state_to_string(): given a state, provides the state's name
560Sstevel@tonic-gate *
570Sstevel@tonic-gate * input: DHCPSTATE: the state to get the name of
580Sstevel@tonic-gate * output: const char *: the state's name
590Sstevel@tonic-gate */
600Sstevel@tonic-gate
610Sstevel@tonic-gate const char *
dhcp_state_to_string(DHCPSTATE state)620Sstevel@tonic-gate dhcp_state_to_string(DHCPSTATE state)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate const char *states[] = {
650Sstevel@tonic-gate "INIT",
660Sstevel@tonic-gate "SELECTING",
670Sstevel@tonic-gate "REQUESTING",
682546Scarlsonj "PRE_BOUND",
690Sstevel@tonic-gate "BOUND",
700Sstevel@tonic-gate "RENEWING",
710Sstevel@tonic-gate "REBINDING",
720Sstevel@tonic-gate "INFORMATION",
730Sstevel@tonic-gate "INIT_REBOOT",
740Sstevel@tonic-gate "ADOPTING",
753431Scarlsonj "INFORM_SENT",
763431Scarlsonj "DECLINING",
773431Scarlsonj "RELEASING"
780Sstevel@tonic-gate };
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (state < 0 || state >= DHCP_NSTATES)
810Sstevel@tonic-gate return ("<unknown>");
820Sstevel@tonic-gate
830Sstevel@tonic-gate return (states[state]);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
86*9633Sjames.d.carlson@sun.com static int
init_template(void)87*9633Sjames.d.carlson@sun.com init_template(void)
88*9633Sjames.d.carlson@sun.com {
89*9633Sjames.d.carlson@sun.com int fd;
90*9633Sjames.d.carlson@sun.com int err = 0;
91*9633Sjames.d.carlson@sun.com
92*9633Sjames.d.carlson@sun.com fd = open64(CTFS_ROOT "/process/template", O_RDWR);
93*9633Sjames.d.carlson@sun.com if (fd == -1)
94*9633Sjames.d.carlson@sun.com return (-1);
95*9633Sjames.d.carlson@sun.com
96*9633Sjames.d.carlson@sun.com /*
97*9633Sjames.d.carlson@sun.com * Deliver no events, don't inherit, and allow it to be orphaned.
98*9633Sjames.d.carlson@sun.com */
99*9633Sjames.d.carlson@sun.com err |= ct_tmpl_set_critical(fd, 0);
100*9633Sjames.d.carlson@sun.com err |= ct_tmpl_set_informative(fd, 0);
101*9633Sjames.d.carlson@sun.com err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
102*9633Sjames.d.carlson@sun.com err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
103*9633Sjames.d.carlson@sun.com if (err != 0 || ct_tmpl_activate(fd) != 0) {
104*9633Sjames.d.carlson@sun.com (void) close(fd);
105*9633Sjames.d.carlson@sun.com return (-1);
106*9633Sjames.d.carlson@sun.com }
107*9633Sjames.d.carlson@sun.com
108*9633Sjames.d.carlson@sun.com return (fd);
109*9633Sjames.d.carlson@sun.com }
110*9633Sjames.d.carlson@sun.com
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * dhcp_start_agent(): starts the agent if not already running
1130Sstevel@tonic-gate *
1140Sstevel@tonic-gate * input: int: number of seconds to wait for agent to start (-1 is forever)
1150Sstevel@tonic-gate * output: int: 0 on success, -1 on failure
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate int
dhcp_start_agent(int timeout)1190Sstevel@tonic-gate dhcp_start_agent(int timeout)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate int error;
1220Sstevel@tonic-gate time_t start_time = time(NULL);
1230Sstevel@tonic-gate dhcp_ipc_request_t *request;
1240Sstevel@tonic-gate dhcp_ipc_reply_t *reply;
125*9633Sjames.d.carlson@sun.com int ctfd;
126*9633Sjames.d.carlson@sun.com pid_t childpid;
127*9633Sjames.d.carlson@sun.com ctid_t ct;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * just send a dummy request to the agent to find out if it's
1310Sstevel@tonic-gate * up. we do this instead of directly connecting to it since
1320Sstevel@tonic-gate * we want to make sure we follow its IPC conventions
1330Sstevel@tonic-gate * (otherwise, it will log warnings to syslog).
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_PING, "", NULL, 0,
1370Sstevel@tonic-gate DHCP_TYPE_NONE);
1380Sstevel@tonic-gate if (request == NULL)
1390Sstevel@tonic-gate return (-1);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, 0);
1420Sstevel@tonic-gate if (error == 0) {
1430Sstevel@tonic-gate free(reply);
1440Sstevel@tonic-gate free(request);
1450Sstevel@tonic-gate return (0);
1460Sstevel@tonic-gate }
147*9633Sjames.d.carlson@sun.com if (error != DHCP_IPC_E_CONNECT)
148*9633Sjames.d.carlson@sun.com goto fail;
149*9633Sjames.d.carlson@sun.com
150*9633Sjames.d.carlson@sun.com if ((ctfd = init_template()) == -1)
151*9633Sjames.d.carlson@sun.com goto fail;
1520Sstevel@tonic-gate
153*9633Sjames.d.carlson@sun.com childpid = fork();
1540Sstevel@tonic-gate
155*9633Sjames.d.carlson@sun.com (void) ct_tmpl_clear(ctfd);
156*9633Sjames.d.carlson@sun.com (void) close(ctfd);
157*9633Sjames.d.carlson@sun.com
158*9633Sjames.d.carlson@sun.com switch (childpid) {
1590Sstevel@tonic-gate case -1:
160*9633Sjames.d.carlson@sun.com goto fail;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate case 0:
1630Sstevel@tonic-gate (void) execl(DHCP_AGENT_PATH, DHCP_AGENT_PATH, (char *)0);
1640Sstevel@tonic-gate _exit(EXIT_FAILURE);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate default:
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
170*9633Sjames.d.carlson@sun.com /* wait for the daemon to run and then abandon the contract */
171*9633Sjames.d.carlson@sun.com (void) waitpid(childpid, NULL, 0);
172*9633Sjames.d.carlson@sun.com
173*9633Sjames.d.carlson@sun.com if (contract_latest(&ct) != -1)
174*9633Sjames.d.carlson@sun.com (void) contract_abandon_id(ct);
175*9633Sjames.d.carlson@sun.com
1760Sstevel@tonic-gate while ((timeout != -1) && (time(NULL) - start_time < timeout)) {
1770Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, 0);
1780Sstevel@tonic-gate if (error == 0) {
1790Sstevel@tonic-gate free(reply);
1800Sstevel@tonic-gate free(request);
1810Sstevel@tonic-gate return (0);
1820Sstevel@tonic-gate } else if (error != DHCP_IPC_E_CONNECT)
1830Sstevel@tonic-gate break;
1840Sstevel@tonic-gate (void) sleep(1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
187*9633Sjames.d.carlson@sun.com fail:
1880Sstevel@tonic-gate free(request);
1890Sstevel@tonic-gate return (-1);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * dhcp_status_hdr_string(): Return a string suitable to use as the header
1940Sstevel@tonic-gate * when printing DHCP_STATUS reply.
1950Sstevel@tonic-gate * output: const char *: newline terminated printable string
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate const char *
dhcp_status_hdr_string(void)1980Sstevel@tonic-gate dhcp_status_hdr_string(void)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate return (DHCP_STATUS_HDR);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * time_to_string(): Utility routine for printing time
2050Sstevel@tonic-gate *
2060Sstevel@tonic-gate * input: time_t *: time_t to stringify
2070Sstevel@tonic-gate * output: const char *: printable time
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate static const char *
time_to_string(time_t abs_time)2100Sstevel@tonic-gate time_to_string(time_t abs_time)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate static char time_buf[24];
2130Sstevel@tonic-gate time_t tm = abs_time;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if (tm == DHCP_PERM)
2160Sstevel@tonic-gate return ("Never");
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (strftime(time_buf, sizeof (time_buf), "%m/%d/%Y %R",
2190Sstevel@tonic-gate localtime(&tm)) == 0)
2200Sstevel@tonic-gate return ("<unknown>");
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate return (time_buf);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * dhcp_status_reply_to_string(): Return DHCP IPC reply of type DHCP_STATUS
2270Sstevel@tonic-gate * as a printable string
2280Sstevel@tonic-gate *
2290Sstevel@tonic-gate * input: dhcp_reply_t *: contains the status structure to print
2300Sstevel@tonic-gate * output: const char *: newline terminated printable string
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate const char *
dhcp_status_reply_to_string(dhcp_ipc_reply_t * reply)2330Sstevel@tonic-gate dhcp_status_reply_to_string(dhcp_ipc_reply_t *reply)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate static char str[1024];
2360Sstevel@tonic-gate size_t reply_size;
2370Sstevel@tonic-gate dhcp_status_t *status;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate status = dhcp_ipc_get_data(reply, &reply_size, NULL);
2400Sstevel@tonic-gate if (reply_size < DHCP_STATUS_VER1_SIZE)
2410Sstevel@tonic-gate return ("<Internal error: status msg size>\n");
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate (void) snprintf(str, sizeof (str), DHCP_STATUS_STR,
2440Sstevel@tonic-gate status->if_name, dhcp_state_to_string(status->if_state),
2450Sstevel@tonic-gate status->if_sent, status->if_recv, status->if_bad_offers);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_PRIMARY)
2480Sstevel@tonic-gate (void) strlcat(str, "[PRIMARY] ", sizeof (str));
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_BOOTP)
2510Sstevel@tonic-gate (void) strlcat(str, "[BOOTP] ", sizeof (str));
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_FAILED)
2540Sstevel@tonic-gate (void) strlcat(str, "[FAILED] ", sizeof (str));
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (status->if_dflags & DHCP_IF_BUSY)
2570Sstevel@tonic-gate (void) strlcat(str, "[BUSY] ", sizeof (str));
2580Sstevel@tonic-gate
2593431Scarlsonj if (status->if_dflags & DHCP_IF_V6)
2603431Scarlsonj (void) strlcat(str, "[V6] ", sizeof (str));
2613431Scarlsonj
2620Sstevel@tonic-gate (void) strlcat(str, "\n", sizeof (str));
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate switch (status->if_state) {
2650Sstevel@tonic-gate case BOUND:
2660Sstevel@tonic-gate case RENEWING:
2670Sstevel@tonic-gate case REBINDING:
2680Sstevel@tonic-gate break;
2690Sstevel@tonic-gate default:
2700Sstevel@tonic-gate return (str);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate (void) strlcat(str, "(Began, Expires, Renew) = (", sizeof (str));
2740Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_began), sizeof (str));
2750Sstevel@tonic-gate (void) strlcat(str, ", ", sizeof (str));
2760Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_lease), sizeof (str));
2770Sstevel@tonic-gate (void) strlcat(str, ", ", sizeof (str));
2780Sstevel@tonic-gate (void) strlcat(str, time_to_string(status->if_t1), sizeof (str));
2790Sstevel@tonic-gate (void) strlcat(str, ")\n", sizeof (str));
2800Sstevel@tonic-gate return (str);
2810Sstevel@tonic-gate }
282