]> git.ipfire.org Git - people/ms/network.git/blob - src/header-port
header-port: Print errors if config could not be read/written
[people/ms/network.git] / src / header-port
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN"
23
24 # This function is called after a device has been plugged
25 # into the system and got its correct name.
26 # The function is intended to create child ports and things
27 # like that.
28 hook_hotplug() {
29 # If the hook does not handle the hotplug event, it
30 # must return EXIT_NOT_HANDLED.
31 exit ${EXIT_NOT_HANDLED}
32 }
33
34 # This function gets called when a device is plugged in
35 # to determine the right name.
36 # The first argument is the port which should be tested
37 # against the second argument which is the device that
38 # has been plugged in.
39 hook_hotplug_rename() {
40 exit ${EXIT_FALSE}
41 }
42
43 hook_default_new() {
44 if ! hook_parse_cmdline "$@"; then
45 return ${EXIT_ERROR}
46 fi
47
48 assert isset HOOK_PORT_PATTERN
49
50 local port=$(port_find_free ${HOOK_PORT_PATTERN})
51 assert isset port
52
53 port_settings_write "${port}" ${HOOK_SETTINGS}
54
55 exit ${EXIT_OK}
56 }
57
58 hook_new() {
59 hook_default_new "$@"
60 }
61
62 hook_default_edit() {
63 local port=${1}
64 assert isset port
65 shift
66
67 # Read settings
68 if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then
69 error "Could not read settings for port ${port}"
70 return ${EXIT_ERROR}
71 fi
72
73 # Parse command line arguments
74 if ! hook_parse_cmdline "$@"; then
75 return ${EXIT_ERROR}
76 fi
77
78 # Save settings
79 if ! port_settings_write "${port}" ${HOOK_SETTINGS}; then
80 error "Could not write settings for port ${port}"
81 return ${EXIT_ERROR}
82 fi
83
84 return ${EXIT_OK}
85 }
86
87 hook_edit() {
88 hook_default_edit "$@"
89 }
90
91 # Returns a list of all children of this port
92 hook_children() {
93 local port="${1}"
94
95 if ! port_settings_read "${port}" ${HOOK_SETTINGS}; then
96 log ERROR "Could not read port settings: ${port}"
97 return ${EXIT_OK}
98 fi
99
100 print "${SLAVES}"
101 }
102
103 hook_status() {
104 local port="${1}"
105 assert isset port
106
107 cli_device_headline "${port}" --long
108 exit ${EXIT_OK}
109 }
110
111 # Create any virtual devices, but don't bring them up
112 # Must tolerate that the device may already exist
113 hook_create() {
114 cmd_not_implemented
115 }
116
117 # Must tolerate that the device may not exist
118 hook_remove() {
119 cmd_not_implemented
120 }
121
122 # Just bring up the device
123 hook_default_up() {
124 local port="${1}"
125 assert isset port
126
127 if ! device_exists "${port}"; then
128 log ERROR "Port '${port}' does not exist and cannot be brought up"
129 exit ${EXIT_ERROR}
130 fi
131
132 # Bring up the port
133 device_set_up "${port}"
134
135 # Bring up all slaves if the port has any
136 local slave
137 for slave in $(port_get_slaves "${port}"); do
138 port_up "${slave}"
139 done
140 }
141
142 # Depends on the port existing
143 hook_up() {
144 hook_default_up "$@"
145 }
146
147 hook_default_down() {
148 local port="${1}"
149 assert isset port
150
151 if device_exists "${port}"; then
152 device_set_down "${port}"
153 fi
154
155 # Bring down all slaves if the port has any
156 local slave
157 for slave in $(port_get_slaves "${port}"); do
158 port_down "${slave}"
159 done
160 }
161
162 hook_down() {
163 hook_default_down "$@"
164 }