733 if (debug) |
733 if (debug) |
734 fprintf(stdout, "mbsePi-apps thermferm v%s starting\n", VERSION); |
734 fprintf(stdout, "mbsePi-apps thermferm v%s starting\n", VERSION); |
735 |
735 |
736 if (rdconfig()) { |
736 if (rdconfig()) { |
737 fprintf(stderr, "Error reading configuration\n"); |
737 fprintf(stderr, "Error reading configuration\n"); |
738 syslog(LOG_WARNING, "Error reading configuration: halted"); |
738 syslog(LOG_NOTICE, "Error reading configuration: halted"); |
739 return 1; |
739 return 1; |
740 } |
740 } |
741 |
741 |
742 /* |
742 /* |
743 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
743 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
773 * that the child will not be a process group leader. Otherwise, |
773 * that the child will not be a process group leader. Otherwise, |
774 * if the child were to open a terminal, it would become |
774 * if the child were to open a terminal, it would become |
775 * associated with that terminal as its control terminal. |
775 * associated with that terminal as its control terminal. |
776 */ |
776 */ |
777 if ((pgrp = setpgid(0, 0)) == -1) { |
777 if ((pgrp = setpgid(0, 0)) == -1) { |
778 syslog(LOG_WARNING, "setpgpid failed"); |
778 syslog(LOG_NOTICE, "setpgpid failed"); |
779 } |
779 } |
780 |
780 |
781 frk = fork(); |
781 frk = fork(); |
782 switch (frk) { |
782 switch (frk) { |
783 case -1: |
783 case -1: |
784 syslog(LOG_WARNING, "Daemon fork failed: %s", strerror(errno)); |
784 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
785 #ifdef HAVE_WIRINGPI_H |
785 #ifdef HAVE_WIRINGPI_H |
786 stopLCD(); |
786 stopLCD(); |
787 #endif |
787 #endif |
788 exit(1); |
788 exit(1); |
789 case 0: /* |
789 case 0: /* |
790 * Run the daemon |
790 * Run the daemon |
791 */ |
791 */ |
792 fclose(stdin); |
792 fclose(stdin); |
793 if (open("/dev/null", O_RDONLY) != 0) { |
793 if (open("/dev/null", O_RDONLY) != 0) { |
794 syslog(LOG_WARNING, "Reopen of stdin to /dev/null failed"); |
794 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); |
795 _exit(2); |
795 _exit(2); |
796 } |
796 } |
797 fclose(stdout); |
797 fclose(stdout); |
798 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
798 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
799 syslog(LOG_WARNING, "Reopen of stdout to /dev/null failed"); |
799 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); |
800 _exit(2); |
800 _exit(2); |
801 } |
801 } |
802 fclose(stderr); |
802 fclose(stderr); |
803 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
803 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
804 syslog(LOG_WARNING, "Reopen of stderr to /dev/null failed"); |
804 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); |
805 _exit(2); |
805 _exit(2); |
806 } |
806 } |
807 mypid = getpid(); |
807 mypid = getpid(); |
808 rc = server(); |
808 rc = server(); |
809 break; |
809 break; |
842 #endif |
842 #endif |
843 int current_step, valid_step, time_until_now; |
843 int current_step, valid_step, time_until_now; |
844 float previous_target; |
844 float previous_target; |
845 |
845 |
846 if (lockprog((char *)"thermferm")) { |
846 if (lockprog((char *)"thermferm")) { |
847 syslog(LOG_WARNING, "Can't lock"); |
847 syslog(LOG_NOTICE, "Can't lock"); |
848 return 1; |
848 return 1; |
849 } |
849 } |
850 |
850 |
851 rc = devices_detect(); |
851 rc = devices_detect(); |
852 if (rc) { |
852 if (rc) { |
859 #else |
859 #else |
860 rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); |
860 rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); |
861 #endif |
861 #endif |
862 if (rc) { |
862 if (rc) { |
863 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); |
863 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); |
864 syslog(LOG_WARNING, "my_devices_loop thread didn't start rc=%d", rc); |
864 syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); |
865 #ifndef HAVE_WIRINGPI_H |
865 #ifndef HAVE_WIRINGPI_H |
866 } else { |
866 } else { |
867 t++; |
867 t++; |
868 #endif |
868 #endif |
869 } |
869 } |
873 #else |
873 #else |
874 rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); |
874 rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); |
875 #endif |
875 #endif |
876 if (rc) { |
876 if (rc) { |
877 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
877 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
878 syslog(LOG_WARNING, "my_server_loop thread didn't start rc=%d", rc); |
878 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
879 #ifndef HAVE_WIRINGPI_H |
879 #ifndef HAVE_WIRINGPI_H |
880 } else { |
880 } else { |
881 t++; |
881 t++; |
882 #endif |
882 #endif |
883 } |
883 } |
884 |
884 |
885 #ifdef HAVE_WIRINGPI_H |
885 #ifdef HAVE_WIRINGPI_H |
886 rc = piThreadCreate(my_panel_loop); |
886 rc = piThreadCreate(my_panel_loop); |
887 if (rc) { |
887 if (rc) { |
888 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
888 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
889 syslog(LOG_WARNING, "my_panel_loop thread didn't start rc=%d", rc); |
889 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
890 } |
890 } |
891 #endif |
891 #endif |
892 |
892 |
893 |
893 |
894 #ifdef USE_SIMULATOR |
894 #ifdef USE_SIMULATOR |
897 #else |
897 #else |
898 rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); |
898 rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); |
899 #endif |
899 #endif |
900 if (rc) { |
900 if (rc) { |
901 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); |
901 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); |
902 syslog(LOG_WARNING, "my_simulator_loop thread didn't start rc=%d", rc); |
902 syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); |
903 #ifndef HAVE_WIRINGPI_H |
903 #ifndef HAVE_WIRINGPI_H |
904 } else { |
904 } else { |
905 t++; |
905 t++; |
906 #endif |
906 #endif |
907 } |
907 } |