Skip to content

Commit 579f7ed

Browse files
committed
rtapi_app: start command / do not autostart
This gives rtapi_app a deterministic behaivour. No other changes needed exept implementing start / stop in realtime due to all apps run: realtime start at startup realtime stop at exit
1 parent af73e08 commit 579f7ed

2 files changed

Lines changed: 47 additions & 36 deletions

File tree

scripts/realtime.in

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ CheckStatus(){
122122
case $RTPREFIX in
123123
uspace)
124124
if [ -z "$($PS -o stat= -o comm= -C rtapi_app | $GREP -v '^Z')" ]; then
125+
echo Not running
125126
exit 1
126127
else
128+
echo Running
127129
exit 0
128130
fi ;;
129131
*)
@@ -168,14 +170,20 @@ CheckMem(){
168170

169171
Load(){
170172
CheckKernel
171-
for MOD in $MODULES_LOAD ; do
172-
if ! [ -d "/sys/module/$(basename "$MOD" .ko)" ]; then
173-
$INSMOD "$MOD" || return $?
174-
fi
175-
done
176-
if [ "$DEBUG" != "" ] && [ -w /proc/rtapi/debug ] ; then
177-
echo "$DEBUG" > /proc/rtapi/debug
178-
fi
173+
case $RTPREFIX in
174+
uspace)
175+
rtapi_app start
176+
;;
177+
(*rtai*)
178+
for MOD in $MODULES_LOAD ; do
179+
if ! [ -d "/sys/module/$(basename "$MOD" .ko)" ]; then
180+
$INSMOD "$MOD" || return $?
181+
fi
182+
done
183+
if [ "$DEBUG" != "" ] && [ -w /proc/rtapi/debug ] ; then
184+
echo "$DEBUG" > /proc/rtapi/debug
185+
fi
186+
esac
179187
}
180188

181189
CheckLoaded(){

src/rtapi/uspace_rtapi_main.cc

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

836839
static 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) {
@@ -844,18 +847,9 @@ static int master(int fd, const std::vector<std::string> &args) {
844847
return -1;
845848
}
846849
do_load_cmd("hal_lib", std::vector<std::string>());
847-
instance_count = 0;
848850
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-
}
856851
//Process commands as long as master should not exit
857852
while(master_process_socket_command(fd));
858-
out:
859853
do_unload_cmd("hal_lib");
860854
pthread_cancel(queue_thread);
861855
pthread_join(queue_thread, nullptr);
@@ -946,7 +940,6 @@ int main(int argc, char **argv) {
946940
args.push_back(std::string(argv[i]));
947941
}
948942

949-
become_master:
950943
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
951944
if (fd == -1) {
952945
perror("socket");
@@ -966,27 +959,37 @@ int main(int argc, char **argv) {
966959
int result = bind(fd, (sockaddr *)&addr, sizeof(addr));
967960

968961
if (result == 0) {
969-
//If exit is called and master is not running, do not start master
970-
//and exit again
962+
//If exit is called and master is not running, just give a warning
971963
if (args.size() == 1 && args[0] == "exit") {
964+
rtapi_print_msg(RTAPI_MSG_ERR, "rtapi_app: exit received while not running\n");
972965
return 0;
973966
}
974-
//If getrt is called and master is not running, do not start master
975-
//execute and return
967+
//If getrt is called and master is not running, run the command directly
976968
//This is needed for the verify command in the realtime script
977-
//If master is started, this starts up the whole realtime chanin
978-
//and halrun -U is needed to exit again
979969
if (args.size() == 1 && args[0] == "getrt") {
980970
return do_getrt_cmd();
981971
}
982-
int result = listen(fd, 10);
983-
if (result != 0) {
984-
perror("listen");
985-
exit(1);
972+
//Start a master on start command
973+
if (args.size() == 1 && args[0] == "start") {
974+
int result = listen(fd, 10);
975+
if (result != 0) {
976+
perror("listen");
977+
exit(1);
978+
}
979+
//Demonize
980+
pid_t pid = fork();
981+
if (pid < 0){
982+
perror("fork");
983+
exit(1);
984+
}
985+
if(pid == 0){
986+
setsid(); // create a new session if we can...
987+
result = master(fd);
988+
}
989+
return result;
986990
}
987-
setsid(); // create a new session if we can...
988-
result = master(fd, args);
989-
return result;
991+
fprintf(stderr, "error: No master found. Use realtime start to start one.\n");
992+
exit(1);
990993
} else if (errno == EADDRINUSE) {
991994
struct timespec start, now;
992995
clock_gettime(CLOCK_MONOTONIC, &start);
@@ -1003,7 +1006,7 @@ int main(int argc, char **argv) {
10031006
if (result < 0 && errno == ECONNREFUSED) {
10041007
fprintf(stderr, "Waited 3 seconds for master. giving up.\n");
10051008
close(fd);
1006-
goto become_master;
1009+
exit(1);
10071010
}
10081011
if (result < 0) {
10091012
fprintf(stderr, "connect %s: %s", addr.sun_path, strerror(errno));

0 commit comments

Comments
 (0)