source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c @ 8bd38d6

5-freebsd-12
Last change on this file since 8bd38d6 was 8bd38d6, checked in by Sebastian Huber <sebastian.huber@…>, on May 2, 2018 at 6:58:48 AM

dhcpcd: Add rtems_dhcpcd_start()

Use it throughout to start the DHCP client (dhcpcd).

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * Copyright (c) 2013, 2018 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <rtems/netcmds-config.h>
33
34#include <rtems.h>
35#include <rtems/dhcpcd.h>
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41typedef struct {
42        rtems_dhcpcd_config config;
43        char *argv[RTEMS_ZERO_LENGTH_ARRAY];
44} dhcpcd_command_config;
45
46static void
47dhcpcd_command_destroy_config(const rtems_dhcpcd_config *config, int exit_code)
48{
49        char **argv;
50
51        (void)exit_code;
52
53        argv = config->argv;
54        while (*argv != NULL) {
55                free(*argv);
56                ++argv;
57        }
58
59        free(RTEMS_DECONST(rtems_dhcpcd_config *, config));
60}
61
62static int
63dhcpcd_command(int argc, char **argv)
64{
65        dhcpcd_command_config *config;
66        rtems_status_code sc;
67        int i;
68
69        config = calloc(1, sizeof(*config) + (argc + 1) * sizeof(char *));
70        if (config == NULL) {
71                fprintf(stdout, "dhcpcd error: not enough memory\n");
72                return 1;
73        }
74
75        for (i = 0; i < argc; ++i) {
76                config->argv[i] = strdup(argv[i]);
77                if (config->argv[i] == NULL) {
78                        dhcpcd_command_destroy_config(&config->config, 0);
79                        fprintf(stdout, "dhcpcd error: not enough memory\n");
80                        return 1;
81                }
82        }
83
84        config->config.argc = argc;
85        config->config.argv = &config->argv[0];
86        config->config.destroy = dhcpcd_command_destroy_config;
87
88        sc = rtems_dhcpcd_start(&config->config);
89        if (sc != RTEMS_SUCCESSFUL) {
90                dhcpcd_command_destroy_config(&config->config, 0);
91                fprintf(stdout, "dhcpcd start failed: %s\n", rtems_status_text(sc));
92        }
93
94        return 0;
95}
96
97rtems_shell_cmd_t rtems_shell_DHCPCD_Command = {
98  .name = "dhcpcd",
99  .usage = "dhcpcd [args]",
100  .topic = "net",
101  .command = dhcpcd_command
102};
Note: See TracBrowser for help on using the repository browser.