Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/emc/usr_intf/emcrsh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <getopt.h>
#include <atomic> // for sessions counter
#include <mutex> // parseCommand is not reentrant

#include "emcglb.h" // EMC_NMLFILE, TRAJ_MAX_VELOCITY, etc.
#include "inifile.hh" // INIFILE
Expand Down Expand Up @@ -449,7 +451,7 @@ int enabledConn = -1;
char pwd[16] = "EMC\0";
char enablePWD[16] = "EMCTOO\0";
char serverName[24] = "EMCNETSVR\0";
int sessions = 0;
std::atomic_int sessions = 0;
int maxSessions = -1;

const char *cmdTokens[] = {
Expand Down Expand Up @@ -479,6 +481,8 @@ struct option longopts[] = {
{"path", 1, NULL, 'd'},
{0,0,0,0}};

std::mutex queue_mtx; // controls access to queue

/* format string to outputbuffer (will be presented to user as result of command) */
#define OUT(...) snprintf(context->outBuf, sizeof(context->outBuf), __VA_ARGS__)

Expand Down Expand Up @@ -524,7 +528,7 @@ static int initSocket()
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(port);
server_len = sizeof(server_address);
err = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
err = bind(server_sockfd, reinterpret_cast<struct sockaddr*>(&server_address), server_len);
if (err) {
rcs_print_error("error initializing sockets: %s\n", strerror(errno));
return err;
Expand Down Expand Up @@ -2917,6 +2921,8 @@ cmdType lookupCommand(char *s)
// handle the linuxcncrsh command in context->inBuf
int parseCommand(connectionRecType *context)
{
std::lock_guard<std::mutex> lck(queue_mtx); // Only one thread enters this function at a time.
// Caveat: Change strtok to strtok_r if this ever changes.
int ret = 0;
char *cmdStr;

Expand Down Expand Up @@ -3041,15 +3047,9 @@ int sockMain()
sessions++;
// enforce limited amount of clients that can connect simultaneously
if ((maxSessions == -1) || (sessions <= maxSessions)) {
pthread_t *thrd;
pthread_t thrd;
connectionRecType *context;

thrd = (pthread_t *)calloc(1, sizeof(pthread_t));
if (!thrd) {
fprintf(stderr, "linuxcncrsh: out of memory\n");
exit(1);
}

context = (connectionRecType *)malloc(sizeof(connectionRecType));
if (!context) {
fprintf(stderr, "linuxcncrsh: out of memory\n");
Expand All @@ -3067,7 +3067,10 @@ int sockMain()
context->commProt = 0;
context->inBuf[0] = 0;

res = pthread_create(thrd, NULL, readClient, (void *)context);
res = pthread_create(&thrd, NULL, readClient, (void *)context);
if (!res) {
pthread_detach(thrd);
}
} else {
fprintf(stderr, "linuxcncrsh: maximum amount of sessions exceeded: %d\n", maxSessions);
res = -1;
Expand Down
Loading
Loading