@@ -35,7 +35,7 @@ THE SOFTWARE.
3535#include <sys/epoll.h>
3636#include <sys/types.h>
3737#include <signal.h>
38- #include <time.h>
38+ #include <time.h>
3939#include <pthread.h>
4040#include <dirent.h>
4141#include <stdarg.h>
@@ -68,34 +68,36 @@ void log2file(const char *fmt, ...)
6868const char * python_file = "bakebit_nanohat_oled.py" ;
6969static int get_work_path (char * buff , int maxlen ) {
7070 ssize_t len = readlink ("/proc/self/exe" , buff , maxlen );
71- if (len == -1 || len == maxlen ) {
72- return -1 ;
73- }
71+ if (len == -1 || len == maxlen ) {
72+ return -1 ;
73+ }
7474 buff [len ] = '\0' ;
75-
75+
7676 char * pos = strrchr (buff , '/' );
77- if (pos != 0 ) {
78- * pos = '\0' ;
79- }
80-
77+ if (pos != 0 ) {
78+ * pos = '\0' ;
79+ }
80+
8181 return 0 ;
82- }
82+ }
8383static char workpath [255 ];
8484static int py_pids [128 ];
8585static int pid_count = 0 ;
86- extern int find_pid_by_name ( char * ProcName , int * foundpid );
86+ extern int find_pid_by_name ( char * ProcName , const char * proc_argument , int * foundpid );
87+ #define CMDLINE_LENGTH 60
8788void send_signal_to_python_process (int signal ) {
8889 int i , rv ;
8990 if (pid_count == 0 ) {
90- rv = find_pid_by_name ( "python3.5" , py_pids );
91+ rv = find_pid_by_name ( "python3" , python_file , py_pids );
9192 for (i = 0 ; py_pids [i ] != 0 ; i ++ ) {
92- log2file ("found python pid: %d\n" , py_pids [i ]);
93+ log2file ("found python3 pid: %d\n" , py_pids [i ]);
9394 pid_count ++ ;
9495 }
9596 }
9697 if (pid_count > 0 ) {
9798 for (i = 0 ; i < pid_count ; i ++ ) {
9899 if (kill (py_pids [i ], signal ) != 0 ) { //maybe pid is invalid
100+ log2file ("invalid PID" );
99101 pid_count = 0 ;
100102 break ;
101103 }
@@ -125,15 +127,15 @@ int load_python_view() {
125127 return 0 ;
126128}
127129
128- int find_pid_by_name ( char * ProcName , int * foundpid ) {
130+ int find_pid_by_name ( char * ProcName , const char * proc_argument , int * foundpid ) {
129131 DIR * dir ;
130132 struct dirent * d ;
131133 int pid , i ;
132134 char * s ;
133135 int pnlen ;
134136
135137 i = 0 ;
136- foundpid [0 ] = 0 ;
138+ foundpid [i ] = 0 ;
137139 pnlen = strlen (ProcName );
138140
139141 /* Open the /proc directory. */
@@ -149,8 +151,12 @@ int find_pid_by_name( char* ProcName, int* foundpid) {
149151
150152 char exe [PATH_MAX + 1 ];
151153 char path [PATH_MAX + 1 ];
154+ char cmdline_path [PATH_MAX + 1 ];
155+ char * cmdline = (char * )malloc (sizeof (CMDLINE_LENGTH + 1 ));
156+ int bufread ;
152157 int len ;
153158 int namelen ;
159+ FILE * cmdline_fd ;
154160
155161 /* See if this is a process */
156162 if ((pid = atoi (d -> d_name )) == 0 ) continue ;
@@ -171,9 +177,29 @@ int find_pid_by_name( char* ProcName, int* foundpid) {
171177
172178 if (!strncmp (ProcName , s , pnlen )) {
173179 /* to avoid subname like search proc tao but proc taolinke matched */
174- if (s [pnlen ] == ' ' || s [pnlen ] == '\0' ) {
175- foundpid [i ] = pid ;
176- i ++ ;
180+ if (s [pnlen ] == ' ' || s [pnlen ] == '\0' || s [pnlen ] == '.' ) {
181+ /* make sure this is the python instance we are looking for */
182+ snprintf (cmdline_path , sizeof (cmdline_path ), "/proc/%s/cmdline" , d -> d_name );
183+ if ((cmdline_fd = fopen (cmdline_path , "r" )) == NULL )
184+ continue ;
185+
186+ bufread = fread (cmdline , sizeof (char ), CMDLINE_LENGTH , cmdline_fd );
187+
188+ // Skip pythonX portion of cmdline
189+ while (* cmdline ++ != 0 );
190+
191+ // Check argument
192+ while (* cmdline != 0 && * proc_argument != 0 ) {
193+ if (* cmdline != * proc_argument )
194+ break ;
195+ ++ cmdline ;
196+ ++ proc_argument ;
197+ }
198+
199+ if (* proc_argument == 0 ) {
200+ foundpid [i ] = pid ;
201+ i ++ ;
202+ }
177203 }
178204 }
179205 }
0 commit comments