@@ -753,7 +753,10 @@ static int handle_command(const std::vector<std::string> &args) {
753753 if (args.size () == 0 ) {
754754 return 0 ;
755755 }
756- if (args.size () == 1 && args[0 ] == " exit" ) {
756+ if (args.size () == 1 && args[0 ] == " start" ) {
757+ rtapi_print_msg (RTAPI_MSG_ERR , " rtapi_app: start received while running\n " );
758+ return 0 ;
759+ } else if (args.size () == 1 && args[0 ] == " exit" ) {
757760 force_exit = 1 ;
758761 return 0 ;
759762 } else if (args.size () >= 2 && args[0 ] == " load" ) {
@@ -771,7 +774,7 @@ static int handle_command(const std::vector<std::string> &args) {
771774 } else if (args.size () == 1 && args[0 ] == " getrt" ) {
772775 return do_getrt_cmd ();
773776 } else {
774- rtapi_print_msg (RTAPI_MSG_ERR , " Unrecognized command starting with %s\n " , args[0 ].c_str ());
777+ rtapi_print_msg (RTAPI_MSG_ERR , " rtapi_app: unrecognized command starting with %s\n " , args[0 ].c_str ());
775778 return -1 ;
776779 }
777780}
@@ -830,12 +833,12 @@ static bool master_process_socket_command(int fd) {
830833 }
831834 close (fd1);
832835 }
833- return !force_exit && instance_count > 0 ;
836+ return !force_exit;
834837}
835838
836839static pthread_t main_thread{};
837840
838- static int master (int fd, const std::vector<std::string> &args ) {
841+ static int master (int fd) {
839842 main_thread = pthread_self ();
840843 int result;
841844 if ((result = pthread_create (&queue_thread, nullptr , &queue_function, nullptr )) != 0 ) {
@@ -846,16 +849,8 @@ static int master(int fd, const std::vector<std::string> &args) {
846849 do_load_cmd (" hal_lib" , std::vector<std::string>());
847850 instance_count = 0 ;
848851 App (); // force rtapi_app to be created
849- if (args.size ()) {
850- result = handle_command (args);
851- if (result != 0 )
852- goto out;
853- if (force_exit || instance_count == 0 )
854- goto out;
855- }
856852 // Process commands as long as master should not exit
857853 while (master_process_socket_command (fd));
858- out:
859854 do_unload_cmd (" hal_lib" );
860855 pthread_cancel (queue_thread);
861856 pthread_join (queue_thread, nullptr );
@@ -946,7 +941,6 @@ int main(int argc, char **argv) {
946941 args.push_back (std::string (argv[i]));
947942 }
948943
949- become_master:
950944 int fd = socket (PF_UNIX , SOCK_STREAM , 0 );
951945 if (fd == -1 ) {
952946 perror (" socket" );
@@ -969,24 +963,37 @@ int main(int argc, char **argv) {
969963 // If exit is called and master is not running, do not start master
970964 // and exit again
971965 if (args.size () == 1 && args[0 ] == " exit" ) {
966+ rtapi_print_msg (RTAPI_MSG_ERR , " rtapi_app: exit received while not running\n " );
972967 return 0 ;
973968 }
974- // If getrt is called and master is not running, do not start master
975- // execute and return
969+ // If getrt is called and master is not running, do not start master.
970+ // Instead, run the command directly
976971 // This is needed for the verify command in the realtime script
977972 // If master is started, this starts up the whole realtime chanin
978973 // and halrun -U is needed to exit again
979974 if (args.size () == 1 && args[0 ] == " getrt" ) {
980975 return do_getrt_cmd ();
981976 }
982- int result = listen (fd, 10 );
983- if (result != 0 ) {
984- perror (" listen" );
985- exit (1 );
977+ // Start a master on start command
978+ if (args.size () == 1 && args[0 ] == " start" ) {
979+ int result = listen (fd, 10 );
980+ if (result != 0 ) {
981+ perror (" listen" );
982+ exit (1 );
983+ }
984+ pid_t pid = fork ();
985+ if (pid < 0 ){
986+ perror (" lisforkten" );
987+ exit (1 );
988+ }
989+ if (pid == 0 ){
990+ setsid (); // create a new session if we can...
991+ result = master (fd);
992+ }
993+ return result;
986994 }
987- setsid (); // create a new session if we can...
988- result = master (fd, args);
989- return result;
995+ fprintf (stderr, " error: No master found. Use realtime start to start one.\n " );
996+ exit (1 );
990997 } else if (errno == EADDRINUSE ) {
991998 struct timespec start, now;
992999 clock_gettime (CLOCK_MONOTONIC , &start);
@@ -1003,7 +1010,7 @@ int main(int argc, char **argv) {
10031010 if (result < 0 && errno == ECONNREFUSED ) {
10041011 fprintf (stderr, " Waited 3 seconds for master. giving up.\n " );
10051012 close (fd);
1006- goto become_master ;
1013+ exit ( 1 ) ;
10071014 }
10081015 if (result < 0 ) {
10091016 fprintf (stderr, " connect %s: %s" , addr.sun_path , strerror (errno));
0 commit comments