aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-21 07:27:43 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-21 07:27:43 +0000
commit833358798afe256352b4ba62a3d93e5273ce6533 (patch)
tree4792d2ec283170b25e542c0cf8569e7af8aa3f08
parent88976d00d8ea2540f987fcd001e140bae895df0f (diff)
downloadbusybox-w32-833358798afe256352b4ba62a3d93e5273ce6533.tar.gz
busybox-w32-833358798afe256352b4ba62a3d93e5273ce6533.tar.bz2
busybox-w32-833358798afe256352b4ba62a3d93e5273ce6533.zip
slattach: code shrink (Bernhard Fischer <rep.dot.nop@gmail.com>)
-rw-r--r--networking/slattach.c48
1 files changed, 27 insertions, 21 deletions
diff --git a/networking/slattach.c b/networking/slattach.c
index 1c00fddb6..3495df598 100644
--- a/networking/slattach.c
+++ b/networking/slattach.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Stripped down version of net-tools for busybox. 3 * Stripped down version of net-tools for busybox.
3 * 4 *
@@ -13,12 +14,13 @@
13 */ 14 */
14 15
15#include "libbb.h" 16#include "libbb.h"
17#include "libiproute/utils.h" /* invarg() */
16 18
17/* Line discipline code table */ 19/* Line discipline code table */
18static const char *const proto_names[] = { 20static const char *const proto_names[] = {
19 "slip", /* 0 */ 21 "cslip"+1, /* 0 */
20 "cslip", /* 1 */ 22 "cslip", /* 1 */
21 "slip6", /* 2 */ 23 "cslip6"+1, /* 2 */
22 "cslip6", /* 3 */ 24 "cslip6", /* 3 */
23 "adaptive", /* 8 */ 25 "adaptive", /* 8 */
24 NULL 26 NULL
@@ -52,6 +54,18 @@ static void save_state(void)
52 bb_perror_msg_and_die("get discipline"); 54 bb_perror_msg_and_die("get discipline");
53} 55}
54 56
57static int set_termios_state_and_warn(struct termios *state)
58{
59 int ret;
60
61 ret = tcsetattr(handle, TCSANOW, state);
62 if (ret < 0) {
63 bb_perror_msg("set state");
64 return 1; /* used as exitcode */
65 }
66 return 0;
67}
68
55/* 69/*
56 * Restore state and line discipline for ALL managed ttys 70 * Restore state and line discipline for ALL managed ttys
57 * 71 *
@@ -76,18 +90,13 @@ static void restore_state_and_exit(int exitcode)
76 memcpy(&state, &saved_state, sizeof(state)); 90 memcpy(&state, &saved_state, sizeof(state));
77 cfsetispeed(&state, B0); 91 cfsetispeed(&state, B0);
78 cfsetospeed(&state, B0); 92 cfsetospeed(&state, B0);
79 if (tcsetattr(handle, TCSANOW, &state) < 0) { 93 if (set_termios_state_and_warn(&state))
80 bb_perror_msg("set state");
81 exitcode = 1; 94 exitcode = 1;
82 }
83
84 sleep(1); 95 sleep(1);
85 96
86 /* Restore line status */ 97 /* Restore line status */
87 if (tcsetattr(handle, TCSANOW, &saved_state) < 0) { 98 if (set_termios_state_and_warn(&saved_state))
88 bb_perror_msg_and_die("set state"); 99 exit(EXIT_FAILURE);
89 }
90
91 if (ENABLE_FEATURE_CLEAN_UP) 100 if (ENABLE_FEATURE_CLEAN_UP)
92 close(handle); 101 close(handle);
93 102
@@ -102,11 +111,8 @@ static void set_state(struct termios *state, int encap)
102 int disc; 111 int disc;
103 112
104 /* Set line status */ 113 /* Set line status */
105 if (tcsetattr(handle, TCSANOW, state) < 0) { 114 if (set_termios_state_and_warn(state))
106 bb_perror_msg("set state");
107 goto bad; 115 goto bad;
108 }
109
110 /* Set line discliple (N_SLIP always) */ 116 /* Set line discliple (N_SLIP always) */
111 disc = N_SLIP; 117 disc = N_SLIP;
112 if (ioctl(handle, TIOCSETD, &disc) < 0) { 118 if (ioctl(handle, TIOCSETD, &disc) < 0) {
@@ -145,7 +151,7 @@ int slattach_main(int argc, char **argv)
145 OPT_h_watch = 1 << 4, 151 OPT_h_watch = 1 << 4,
146 OPT_m_nonraw = 1 << 5, 152 OPT_m_nonraw = 1 << 5,
147 OPT_L_local = 1 << 6, 153 OPT_L_local = 1 << 6,
148 OPT_F_noflow = 1 << 7, 154 OPT_F_noflow = 1 << 7
149 }; 155 };
150 156
151 INIT_G(); 157 INIT_G();
@@ -161,7 +167,7 @@ int slattach_main(int argc, char **argv)
161 encap = index_in_str_array(proto_names, proto); 167 encap = index_in_str_array(proto_names, proto);
162 168
163 if (encap < 0) 169 if (encap < 0)
164 bb_error_msg_and_die("invalid protocol %s", proto); 170 invarg(proto, "protocol");
165 if (encap > 3) 171 if (encap > 3)
166 encap = 8; 172 encap = 8;
167 173
@@ -169,7 +175,7 @@ int slattach_main(int argc, char **argv)
169 if (opt & OPT_s_baud) { 175 if (opt & OPT_s_baud) {
170 baud_code = tty_value_to_baud(xatoi(baud_str)); 176 baud_code = tty_value_to_baud(xatoi(baud_str));
171 if (baud_code < 0) 177 if (baud_code < 0)
172 bb_error_msg_and_die("invalid baud rate"); 178 invarg(baud_str, "baud rate");
173 } 179 }
174 180
175 /* Trap signals in order to restore tty states upon exit */ 181 /* Trap signals in order to restore tty states upon exit */
@@ -180,10 +186,10 @@ int slattach_main(int argc, char **argv)
180 signal(SIGTERM, sig_handler); 186 signal(SIGTERM, sig_handler);
181 } 187 }
182 188
183 /* Open tty */ 189 /* Open tty */
184 handle = open(*argv, O_RDWR | O_NDELAY); 190 handle = open(*argv, O_RDWR | O_NDELAY);
185 if (handle < 0) { 191 if (handle < 0) {
186 char *buf = xasprintf("/dev/%s", *argv); 192 char *buf = concat_path_file("/dev", *argv);
187 handle = xopen(buf, O_RDWR | O_NDELAY); 193 handle = xopen(buf, O_RDWR | O_NDELAY);
188 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */ 194 /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */
189 free(buf); 195 free(buf);
@@ -192,7 +198,7 @@ int slattach_main(int argc, char **argv)
192 /* Save current tty state */ 198 /* Save current tty state */
193 save_state(); 199 save_state();
194 200
195 /* Confgure tty */ 201 /* Configure tty */
196 memcpy(&state, &saved_state, sizeof(state)); 202 memcpy(&state, &saved_state, sizeof(state));
197 if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */ 203 if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */
198 memset(&state.c_cc, 0, sizeof(state.c_cc)); 204 memset(&state.c_cc, 0, sizeof(state.c_cc));
@@ -217,7 +223,7 @@ int slattach_main(int argc, char **argv)
217 return 0; 223 return 0;
218 224
219 /* If we're not requested to watch, just keep descriptor open 225 /* If we're not requested to watch, just keep descriptor open
220 * till we are killed */ 226 * until we are killed */
221 if (!(opt & OPT_h_watch)) 227 if (!(opt & OPT_h_watch))
222 while (1) 228 while (1)
223 sleep(24*60*60); 229 sleep(24*60*60);