Skip to content

Commit 53d3442

Browse files
committed
linuxcncrsh: check for errors when creating listening socket
Thanks to Andy Howell for finding and fixing this bug.
1 parent 42dce43 commit 53d3442

7 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/emc/usr_intf/emcrsh.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,16 +542,26 @@ static void thisQuit()
542542
static int initSockets()
543543
{
544544
int optval = 1;
545-
545+
int err;
546+
546547
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
547548
setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
548549
server_address.sin_family = AF_INET;
549550
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
550551
server_address.sin_port = htons(port);
551552
server_len = sizeof(server_address);
552-
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
553-
listen(server_sockfd, 5);
553+
err = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
554+
if (err) {
555+
rcs_print_error("error initializing sockets: %s\n", strerror(errno));
556+
return err;
557+
}
554558

559+
err = listen(server_sockfd, 5);
560+
if (err) {
561+
rcs_print_error("error listening on socket: %s\n", strerror(errno));
562+
return err;
563+
}
564+
555565
// ignore SIGCHLD
556566
{
557567
struct sigaction act;
@@ -2950,7 +2960,10 @@ int main(int argc, char *argv[])
29502960
}
29512961
// get configuration information
29522962
iniLoad(emc_inifile);
2953-
initSockets();
2963+
if (initSockets()) {
2964+
rcs_print_error("error initializing sockets\n");
2965+
exit(1);
2966+
}
29542967
// init NML
29552968
if (tryNml() != 0) {
29562969
rcs_print_error("can't connect to LinuxCNC\n");

tests/linuxcncrsh-tcp/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
rm -f gcode-output
44

5+
if nc -z localhost 5007; then
6+
echo "Process already listening on port 5007. Exiting"
7+
exit 1
8+
fi
9+
510
linuxcnc -r linuxcncrsh-test.ini &
611

712

tests/linuxcncrsh/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
rm -f gcode-output
44

5+
if nc -z localhost 5007; then
6+
echo "Process already listening on port 5007. Exiting"
7+
exit 1
8+
fi
9+
510
linuxcnc -r linuxcncrsh-test.ini &
611

712

tests/mdi-queue/oword-queue-buster/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
rm -f gcode-output
44

5+
if nc -z localhost 5007; then
6+
echo "Process already listening on port 5007. Exiting"
7+
exit 1
8+
fi
9+
510
linuxcnc -r linuxcncrsh-test.ini &
611

712

tests/mdi-queue/simple-queue-buster/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
rm -f gcode-output
44

5+
if nc -z localhost 5007; then
6+
echo "Process already listening on port 5007. Exiting"
7+
exit 1
8+
fi
9+
510
linuxcnc -r linuxcncrsh-test.ini &
611

712

tests/t0/shared-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ cp tool.tbl.original tool.tbl
99

1010
rm -f gcode-output
1111

12+
if nc -z localhost 5007; then
13+
echo "Process already listening on port 5007. Exiting"
14+
exit 1
15+
fi
16+
1217
linuxcnc -r sim.ini &
1318

1419

tests/toolchanger/toolno-pocket-differ/shared-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ cp ../../simpockets.tbl.original simpockets.tbl
99

1010
rm -f gcode-output
1111

12+
if nc -z localhost 5007; then
13+
echo "Process already listening on port 5007. Exiting"
14+
exit 1
15+
fi
16+
1217
linuxcnc -r sim.ini &
1318

1419

0 commit comments

Comments
 (0)