-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathport.c
1114 lines (954 loc) · 26.7 KB
/
port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2020 Corey Minyard <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only
*
* In addition, as a special exception, the copyright holders of
* ser2net give you permission to combine ser2net with free software
* programs or libraries that are released under the GNU LGPL and
* with code included in the standard release of OpenSSL under the
* OpenSSL license (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for ser2net and the licenses of the other code
* concerned, provided that you include the source code of that
* other code when and as the GNU GPL requires distribution of source
* code.
*
* Note that people who make modified versions of ser2net are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "ser2net.h"
#include "port.h"
#include "gbuf.h"
#include <gensio/argvutils.h>
#include <gensio/gensio_err.h>
struct gensio_lock *ports_lock;
port_info_t *ports = NULL; /* Linked list of ports. */
port_info_t *new_ports = NULL; /* New ports during config/reconfig. */
port_info_t *new_ports_end = NULL;
net_info_t *
first_live_net_con(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
return netcon;
}
return NULL;
}
bool
port_in_use(port_info_t *port)
{
return (port->net_to_dev_state != PORT_UNCONNECTED &&
port->net_to_dev_state != PORT_CLOSED);
}
/* Checks to see if some other port has the same device in use. Must
be called with ports_lock held. */
int
is_device_already_inuse(port_info_t *check_port)
{
port_info_t *port = ports;
while (port != NULL) {
if (port != check_port) {
if ((strcmp(port->devname, check_port->devname) == 0)
&& port_in_use(port)) {
return 1;
}
}
port = port->next;
}
return 0;
}
int
num_connected_net(port_info_t *port)
{
net_info_t *netcon;
int count = 0;
for_each_connection(port, netcon) {
if (netcon->net)
count++;
}
return count;
}
gensiods
net_raddr(struct gensio *io, struct sockaddr_storage *addr, gensiods *socklen)
{
*socklen = sizeof(*addr);
#if (defined(gensio_version_major) && (gensio_version_major > 2 || \
(gensio_version_major == 2 && gensio_version_minor > 0)))
return gensio_control(io, GENSIO_CONTROL_DEPTH_FIRST, true,
GENSIO_CONTROL_RADDR_BIN,
(char *) addr, socklen);
#else
return gensio_get_raddr(io, (char *) addr, socklen);
#endif
}
void
reset_timer(net_info_t *netcon)
{
if (netcon->connect_back && netcon->port->connback_timeout_set)
netcon->timeout_left = netcon->port->connback_timeout;
else
netcon->timeout_left = netcon->port->timeout;
netcon->timeout_running = netcon->timeout_left != 0;
}
void
port_start_timer(port_info_t *port)
{
gensio_time timeout;
unsigned int timeout_sec;
switch (port->dev_to_net_state) {
case PORT_UNCONNECTED:
timeout_sec = port->connector_retry_time;
break;
case PORT_CLOSED:
case PORT_NOT_STARTED:
timeout_sec = port->accepter_retry_time;
break;
default:
timeout_sec = 1;
break;
}
#ifdef gensio_version_major
timeout.secs = timeout_sec;
timeout.nsecs = 0;
#else
timeout.tv_sec = timeout_sec;
timeout.tv_usec = 0;
#endif
so->start_timer(port->timer, &timeout);
}
static void
kick_old_user(port_info_t *port, net_info_t *netcon, struct gensio *new_net)
{
char *err = "kicked off, new user is coming\r\n";
/* If another user is waiting for a kick, kick that user. */
if (netcon->new_net) {
gensio_write(netcon->new_net, NULL, err, strlen(err), NULL);
gensio_free(netcon->new_net);
}
/* Wait it to be unconnected and clean, restart the process. */
netcon->new_net = new_net;
shutdown_one_netcon(netcon, err);
}
static void
check_port_new_net(port_info_t *port, net_info_t *netcon)
{
struct gensio *net;
if (!netcon->new_net)
return;
if (netcon->net) {
/* Something snuck in before, kick this one out. */
char *err = "kicked off, new user is coming\r\n";
gensio_write(netcon->new_net, NULL, err, strlen(err), NULL);
gensio_free(netcon->new_net);
netcon->new_net = NULL;
return;
}
net = netcon->new_net;
netcon->new_net = NULL;
handle_new_net(port, net, netcon);
}
static int
port_new_con(port_info_t *port, struct gensio *net)
{
const char *err = NULL;
unsigned int i, j;
struct sockaddr_storage addr;
gensiods socklen;
so->lock(ports_lock); /* For is_device_already_inuse() */
so->lock(port->lock);
if (port->net_to_dev_state == PORT_CLOSING) {
/* Can happen on a race with the callback disable. */
if (!port->enabled) {
err = "Port closing\r\n";
goto out_err;
}
/* We hold off new connections during a close */
goto out;
}
if (!port->enabled) {
err = "Port disabled\r\n";
goto out_err;
}
if (!net_raddr(net, &addr, &socklen)) {
if (!remaddr_check(port->remaddrs,
(struct sockaddr *) &addr, socklen)) {
err = "Accessed denied due to your net address\r\n";
goto out_err;
}
}
for (j = port->max_connections, i = 0; i < port->max_connections; i++) {
if (!port->netcons[i].net && !port->netcons[i].remote_fixed)
break;
if (!port->netcons[i].remote_fixed)
j = i;
}
if (i == port->max_connections) {
if (port->kickolduser_mode && j < port->max_connections) {
/* Kick off the first non-fixed user. */
kick_old_user(port, &port->netcons[j], net);
goto out;
}
err = "Port already in use\r\n";
}
if (!err && is_device_already_inuse(port))
err = "Port's device already in use\r\n";
if (port->connbacks && !port->io_open)
err = "Port's device failed open\r\n";
if (err) {
out_err:
so->unlock(port->lock);
so->unlock(ports_lock);
gensio_write(net, NULL, err, strlen(err), NULL);
gensio_free(net);
return 0;
}
/* We have to hold the ports_lock until after this call so the
device won't get used (from is_device_already_inuse()). */
handle_new_net(port, net, &(port->netcons[i]));
out:
so->unlock(port->lock);
so->unlock(ports_lock);
return 0;
}
/* A connection request has come in on a port. */
static int
handle_port_child_event(struct gensio_accepter *accepter, void *user_data,
int event, void *data)
{
port_info_t *port = user_data;
if (event == GENSIO_ACC_EVENT_LOG) {
do_gensio_log(port->name, data);
return 0;
}
switch (event) {
case GENSIO_ACC_EVENT_NEW_CONNECTION:
return port_new_con(port, data);
#ifdef GENSIO_ACC_EVENT_PARMLOG
case GENSIO_ACC_EVENT_PARMLOG: {
struct gensio_parmlog_data *d = (struct gensio_parmlog_data *) data;
seout.vout(&seout, d->log, d->args);
return 0;
}
#endif
default:
return handle_acc_auth_event(port->authdir, port->pamauth,
port->allowed_users, event, data);
}
}
int
startup_port(struct absout *eout, port_info_t *port)
{
int err;
if (port->dev_to_net_state == PORT_NOT_STARTED) {
port_start_timer(port);
return GE_NOTREADY;
}
if (port->dev_to_net_state != PORT_CLOSED)
return GE_INUSE;
err = gensio_acc_startup(port->accepter);
if (err) {
eout->out(eout, "Unable to startup network port %s: %s",
port->name, gensio_err_to_str(err));
/* Retry in a bit. */
port_start_timer(port);
return err;
}
port->dev_to_net_state = PORT_UNCONNECTED;
port->net_to_dev_state = PORT_UNCONNECTED;
#ifdef DO_MDNS
mdns_setup(&port->mdns_info, port->name, port->accepter, eout);
#endif
if (port->connbacks) {
err = port_dev_enable(port);
if (err) {
eout->out(eout, "Unable to enable port device %s: %s",
port->name, gensio_err_to_str(err));
shutdown_port(port, "Error enabling port connector");
err = 0; /* Don't report an error here, let the shutdown run. */
}
}
return err;
}
static void
call_port_op_done(port_info_t *port)
{
void (*port_op_done)(struct port_info *, void *) = port->port_op_done;
if (port_op_done) {
port->port_op_done = NULL;
port_op_done(port, port->port_op_data);
}
}
static void
finish_shutdown_port(struct gensio_runner *runner, void *cb_data)
{
port_info_t *port = cb_data;
so->lock(ports_lock);
so->lock(port->lock);
if (port->enabled) {
port->net_to_dev_state = PORT_UNCONNECTED;
port->dev_to_net_state = PORT_UNCONNECTED;
if (port->connbacks)
port_start_timer(port);
} else {
port->net_to_dev_state = PORT_CLOSED;
port->dev_to_net_state = PORT_CLOSED;
}
gbuf_reset(&port->net_to_dev);
if (port->devstr) {
gbuf_free(port->devstr);
port->devstr = NULL;
}
gbuf_reset(&port->dev_to_net);
port->dev_bytes_received = 0;
port->dev_bytes_sent = 0;
if (gensio_acc_exit_on_close(port->accepter))
/* This was a zero port (for stdin/stdout), this is only
allowed with one port at a time, and we shut down when it
closes. */
exit(0);
/* If the port has been deleted, then finish the job. */
if (port->deleted) {
port_info_t *curr, *prev, *new;
new = port->new_config;
port->new_config = NULL;
prev = NULL;
for (curr = ports; curr && curr != port; curr = curr->next)
prev = curr;
if (curr) {
if (prev == NULL)
ports = curr->next;
else
prev->next = curr->next;
}
so->unlock(port->lock);
free_port(port);
/* Start the replacement port if it was set. */
if (new) {
so->lock(new->lock);
if (prev) {
new->next = prev->next;
prev->next = new;
} else {
new->next = ports;
ports = new;
}
if (new->enabled)
startup_port(&seout, new);
so->unlock(new->lock);
}
so->unlock(ports_lock);
return; /* We have to return here because we no longer have a port. */
} else if (port->enabled) {
net_info_t *netcon;
gensio_acc_set_accept_callback_enable(port->accepter, true);
for_each_connection(port, netcon)
check_port_new_net(port, netcon);
} else {
/* Port was disabled, shut it down. */
#ifdef DO_MDNS
mdns_shutdown(&port->mdns_info);
#endif
gensio_acc_shutdown(port->accepter, NULL, NULL);
call_port_op_done(port);
}
so->unlock(port->lock);
so->unlock(ports_lock);
}
static void
io_shutdown_done(struct gensio *io, void *cb_data)
{
port_info_t *port = cb_data;
port->io_open = false;
so->run(port->runshutdown);
}
void
shutdown_port_io(port_info_t *port)
{
int err = 1;
shutdown_trace(port);
if (port->io)
err = gensio_close(port->io, io_shutdown_done, port);
if (err) {
port->io_open = false;
so->run(port->runshutdown);
}
}
static void
timer_shutdown_done(struct gensio_timer *timer, void *cb_data)
{
port_info_t *port = cb_data;
so->lock(port->lock);
gensio_set_write_callback_enable(port->io, false);
shutdown_port_io(port);
so->unlock(port->lock);
}
/* Output any pending data and the devstr buffer. */
static void
handle_dev_fd_close_write(port_info_t *port)
{
int err;
if (gbuf_cursize(&port->net_to_dev) != 0)
err = gbuf_write(port, &port->net_to_dev);
else if (port->devstr)
err = gbuf_write(port, port->devstr);
else
goto closeit;
if (err) {
seout.out(&seout, "The dev write(3) for port %s had error: %s",
port->name, gensio_err_to_str(err));
goto closeit;
}
if (gbuf_cursize(&port->net_to_dev) ||
(port->devstr && gbuf_cursize(port->devstr)))
return;
closeit:
if (port->shutdown_timeout_count) {
gensio_set_write_callback_enable(port->io, false);
err = so->stop_timer_with_done(port->timer, timer_shutdown_done, port);
if (err == GE_TIMEDOUT) {
port->shutdown_timeout_count = 0;
shutdown_port_io(port);
}
}
}
static void
start_shutdown_port_io(port_info_t *port)
{
if (!port->io_open) {
so->run(port->runshutdown);
return;
}
if (port->devstr)
gbuf_free(port->devstr);
port->devstr = process_str_to_buf(port, NULL, port->closestr, &seout);
port->dev_write_handler = handle_dev_fd_close_write;
gensio_set_write_callback_enable(port->io, true);
}
static void
netcon_finish_shutdown(net_info_t *netcon)
{
port_info_t *port = netcon->port;
if (netcon->net) {
report_disconnect(port, netcon);
gensio_free(netcon->net);
netcon->net = NULL;
}
netcon->closing = false;
netcon->bytes_received = 0;
netcon->bytes_sent = 0;
netcon->write_pos = 0;
if (netcon->banner) {
gbuf_free(netcon->banner);
netcon->banner = NULL;
}
if (num_connected_net(port) == 0) {
if (port->net_to_dev_state == PORT_CLOSING) {
start_shutdown_port_io(port);
} else if (port->connbacks && port->enabled) {
/* Leave the device open for connect backs. */
gensio_set_write_callback_enable(port->io, true);
port->dev_to_net_state = PORT_WAITING_INPUT;
port->net_to_dev_state = PORT_UNCONNECTED;
check_port_new_net(port, netcon);
} else {
shutdown_port(port, NULL);
}
} else {
check_port_new_net(port, netcon);
}
}
static void
handle_net_fd_closed(struct gensio *net, void *cb_data)
{
net_info_t *netcon = cb_data;
port_info_t *port = netcon->port;
so->lock(port->lock);
netcon_finish_shutdown(netcon);
so->unlock(port->lock);
}
void
shutdown_one_netcon(net_info_t *netcon, const char *reason)
{
int err;
if (netcon->closing)
return;
footer_trace(netcon->port, "netcon", reason);
netcon->close_on_output_done = false;
netcon->closing = true;
err = gensio_close(netcon->net, handle_net_fd_closed, netcon);
if (err)
netcon_finish_shutdown(netcon);
}
static bool
shutdown_all_netcons(port_info_t *port, bool close_on_output_only)
{
net_info_t *netcon;
bool some_to_close = false;
for_each_connection(port, netcon) {
if (netcon->net) {
if (close_on_output_only && !netcon->close_on_output_done)
continue;
some_to_close = true;
netcon->write_pos = port->dev_to_net.cursize;
shutdown_one_netcon(netcon, "port closing");
}
}
return some_to_close;
}
static void
accept_read_disabled(struct gensio_accepter *acc, void *cb_data)
{
port_info_t *port = cb_data;
net_info_t *netcon;
bool some_to_close = false;
so->lock(port->lock);
port->shutdown_started = false;
/*
* At this point we won't receive any more accepts until we re-enable it,
* go into closing state unless it wasn't an error and a new net came in.
*/
if (port->net_to_dev_state != PORT_CLOSING &&
num_connected_net(port) > 0) {
/*
* It wasn't an error close and a new connection came in. Just
* ignore the shutdown.
*/
gensio_acc_set_accept_callback_enable(port->accepter, true);
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, true);
}
gensio_set_read_callback_enable(port->io, true);
goto out_unlock;
}
/* After this point we will shut down the port completely. */
/* FIXME - this should be calculated somehow, not a raw number .*/
port->shutdown_timeout_count = 4;
footer_trace(port, "port", port->shutdown_reason);
for_each_connection(port, netcon) {
if (netcon->net) {
some_to_close = true;
if (netcon->new_net) {
/* Something is waiting for a kick. */
char *err = "port is shutting down\r\n";
gensio_write(netcon->new_net, NULL, err, strlen(err), NULL);
gensio_free(netcon->new_net);
netcon->new_net = NULL;
}
if (port->dev_to_net_state == PORT_WAITING_OUTPUT_CLEAR &&
netcon->write_pos < port->dev_to_net.cursize)
/* Net has data to send, wait until it's done. */
netcon->close_on_output_done = true;
else
shutdown_one_netcon(netcon, "port closing");
}
}
port->dev_to_net_state = PORT_CLOSING;
port->net_to_dev_state = PORT_CLOSING;
if (!some_to_close)
start_shutdown_port_io(port);
out_unlock:
so->unlock(port->lock);
}
int
shutdown_port(port_info_t *port, const char *errreason)
{
net_info_t *netcon;
int err;
if (port->shutdown_started && port->net_to_dev_state != PORT_CLOSING
&& errreason) {
/* An error occurred and we are in a non-err shutdown. Convert it. */
port->shutdown_reason = errreason;
port->net_to_dev_state = PORT_CLOSING;
gensio_set_read_callback_enable(port->io, false);
return 0;
}
if (port->net_to_dev_state == PORT_CLOSING ||
port->net_to_dev_state == PORT_CLOSED ||
(port->enabled && port->dev_to_net_state == PORT_UNCONNECTED) ||
port->shutdown_started)
return GE_INUSE;
if (errreason) {
/* It's an error, force a shutdown. Don't set dev_to_net_state yet. */
port->shutdown_reason = errreason;
port->net_to_dev_state = PORT_CLOSING;
/* Shut down write on an error. */
gensio_set_read_callback_enable(port->io, false);
} else {
port->shutdown_reason = "All users disconnected";
}
port->shutdown_started = true;
err = gensio_acc_set_accept_callback_enable_cb(port->accepter, false,
accept_read_disabled, port);
/* This is bad, it's an out of memory condition. Abort. */
assert(err == 0);
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, false);
}
gensio_set_read_callback_enable(port->io, false);
return 0;
}
static bool
handle_shutdown_timeout(port_info_t *port)
{
/* Something wasn't able to do any writes and locked up the shutdown. */
/* Check the network connections first. */
if (shutdown_all_netcons(port, true))
return true;
shutdown_port_io(port);
return false;
}
static int
port_startup(port_info_t *new_port, struct absout *eout, bool retry)
{
int err;
err = str_to_gensio(new_port->devname, so, handle_dev_event, new_port,
&new_port->io);
if (err) {
if (!retry || new_port->retry_startup_counter % 64 == 0)
eout->out(eout, "device configuration %s invalid: %s",
new_port->devname, gensio_err_to_str(err));
goto out_err;
}
err = str_to_gensio_accepter(new_port->accstr, so,
handle_port_child_event, new_port,
&new_port->accepter);
if (err) {
gensio_free(new_port->io);
new_port->io = NULL;
if (!retry || new_port->retry_startup_counter % 64 == 0)
eout->out(eout, "Invalid accepter port name/number '%s': %s",
new_port->accstr, gensio_err_to_str(err));
goto out_err;
}
if (new_port->enabled && new_port->do_telnet) {
const char *str = "telnet";
struct gensio_accepter *parent;
if (new_port->allow_2217)
str = "telnet(rfc2217=true)";
err = str_to_gensio_accepter_child(new_port->accepter, str,
so,
handle_port_child_event,
new_port, &parent);
if (err) {
gensio_free(new_port->io);
new_port->io = NULL;
gensio_acc_free(new_port->accepter);
new_port->accepter = NULL;
if (!retry || new_port->retry_startup_counter % 64 == 0)
eout->out(eout, "Could not allocate telnet gensio: %s",
gensio_err_to_str(err));
goto out_err;
}
new_port->accepter = parent;
}
new_port->dev_to_net_state = PORT_CLOSED;
if (retry)
eout->out(eout, "port accepter '%s' is now started",
new_port->accstr);
return 0;
out_err:
new_port->retry_startup_counter++;
return err;
}
static void
port_timeout(struct gensio_timer *timer, void *data)
{
port_info_t *port = (port_info_t *) data;
net_info_t *netcon;
int err;
so->lock(port->lock);
if (port->dev_to_net_state == PORT_NOT_STARTED) {
err = port_startup(port, &seout, true);
if (err)
goto out;
}
if (port->dev_to_net_state == PORT_CLOSED) {
if (port->enabled)
startup_port(&seout, port);
goto out_unlock;
}
if (port->dev_to_net_state == PORT_UNCONNECTED) {
if (port->connbacks && !port->io_open) {
err = port_dev_enable(port);
if (err)
goto out;
}
goto out_unlock;
}
if (port->dev_to_net_state == PORT_CLOSING) {
if (port->shutdown_timeout_count <= 1) {
bool dotimer = false;
port->shutdown_timeout_count = 0;
dotimer = handle_shutdown_timeout(port);
so->unlock(port->lock);
if (dotimer)
goto out;
return;
} else {
port->shutdown_timeout_count--;
goto out;
}
}
if (port->nocon_read_enable_time_left) {
port->nocon_read_enable_time_left--;
if (port->nocon_read_enable_time_left == 0) {
gensio_set_read_callback_enable(port->io, true);
port->dev_to_net_state = PORT_WAITING_INPUT;
}
goto out;
}
/*
* Check the timeout on all the ports. If we have a separate
* timeout for connect backs, we need to check that separately.
*/
if (port_in_use(port)) {
for_each_connection(port, netcon) {
if (!netcon->net || !netcon->timeout_running)
continue;
netcon->timeout_left--;
if (netcon->timeout_left < 0)
shutdown_one_netcon(netcon, "timeout");
}
}
out:
port_start_timer(port);
out_unlock:
so->unlock(port->lock);
}
void
apply_new_ports(struct absout *eout)
{
port_info_t *new, *curr, *next, *prev, *new_prev;
so->lock(ports_lock);
/* First turn off all the accepters. */
for (curr = ports; curr; curr = curr->next) {
int err;
if (curr->deleted)
continue;
if (curr->enabled && curr->accepter) {
/*
* This unlock is a little strange, but we don't want to
* do any waiting while holding the ports lock, otherwise
* we might deadlock on a deletion in finish_shutdown_port().
* This is save as long as curr is not deleted, because curr
* will not go away, though curr->next may change, that
* shouldn't matter.
*/
so->unlock(ports_lock);
err = gensio_acc_set_accept_callback_enable_s(curr->accepter,
false);
/* Errors only happen on out of memory. */
assert(err == 0);
so->lock(ports_lock);
curr->accepter_stopped = true;
}
}
/* At this point we can't get any new accepts. */
/*
* See if the port already exists, and link it to this port. We
* put the old port in the new port's place for now.
*/
for (new_prev = NULL, new = new_ports; new;
new_prev = new, new = new->next) {
so->lock(new->lock);
for (prev = NULL, curr = ports; curr; prev = curr, curr = curr->next) {
so->lock(curr->lock);
if (strcmp(curr->name, new->name) == 0) {
if (port_in_use(curr)) {
/* If we are disabling, kick off old users. */
if (!new->enabled && curr->enabled)
shutdown_all_netcons(curr, false);
} else {
if (strcmp(curr->accstr, new->accstr) == 0 &&
curr->enabled) {
/*
* Accepter didn't change and was on, just
* move it over. This avoid issues with a
* connection coming in during a reconfig.
*/
struct gensio_accepter *tmp;
tmp = new->accepter;
new->accepter = curr->accepter;
new->accepter_stopped = true;
curr->accepter = tmp;
curr->accepter_stopped = false;
gensio_acc_set_user_data(curr->accepter, curr);
gensio_acc_set_user_data(new->accepter, new);
}
/* Just let the old one get deleted. */
so->unlock(curr->lock);
break;
}
/* We are reconfiguring this port. */
if (curr->new_config)
free_port(curr->new_config);
curr->new_config = new;
/*
* Put the current entry into the place of the new entry
* in the new_ports array.
*/
if (prev)
prev->next = curr->next;
else
ports = curr->next;
curr->next = new->next;
if (new_prev)
new_prev->next = curr;
else
new_ports = curr;
if (new_ports_end == new)
new_ports_end = curr;
gensio_acc_disable(curr->accepter);
curr->deleted = true;
so->unlock(new->lock);
new = curr;
break;
}
so->unlock(curr->lock);
}
so->unlock(new->lock);
}
/*
* We nuke any old port without a new config. Do this first so
* new ports can use the given port numbers that might be in these.
*/
for (curr = ports; curr; curr = next) {
next = curr->next;
so->lock(curr->lock);
if (curr->accepter_stopped && curr->enabled)
gensio_acc_disable(curr->accepter);
curr->deleted = true;
curr->enabled = false;
if (!port_in_use(curr)) {
so->unlock(curr->lock);
free_port(curr);
} else {
/* Leave it in the new ports for shutdown when the user closes. */
if (new_ports_end)
new_ports_end->next = curr;
curr->next = NULL;
new_ports_end = curr;
so->unlock(curr->lock);
}
}
/* Now start up the new ports. */
ports = new_ports;
new_ports = NULL;
new_ports_end = NULL;
for (curr = ports; curr; curr = curr->next) {
so->lock(curr->lock);
if (!curr->deleted) {
if (curr->accepter_stopped) {