Paparazzi UAS  v5.15_devel-99-g2ff7410
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include "mcu.h"
33 #include "boards/disco.h"
34 
35 static int kill_gracefull(char *process_name)
36 {
37  /* TODO: "pidof" always in /bin on Disco firmware tested 1.4.1, no need for "which" */
38  char pidof_commandline[200] = "/bin/pidof ";
39  strcat(pidof_commandline, process_name);
40  /* On Disco Busybox a
41  $ cat /proc/sys/kernel/pid_max
42  Gives max 32768, makes sure it never kills existing other process
43  */
44  char pid[7] = "";
45  int ret = 0; /* Return code of kill system call */
46  FILE *fp;
47 
48  while (ret == 0) {
49  fp = popen(pidof_commandline, "r");
50  if (fp != NULL) { /* Could open the pidof with line */
51  if (fgets(pid, sizeof(pid) - 1, fp) != NULL) {
52  //printf("Process ID deducted: \"%s\"\n", pid);
53  if (atoi(pid) > 0) { /* To make sure we end 0 > There is a real process id found */
54  char kill_command_and_process[200] = "kill -9 "; /* BTW there is no pkill on this Busybox */
55  strcat(kill_command_and_process, pid);
56  ret = system(kill_command_and_process);
57  /* No need to wait, since if it is not closed the next pidof scan still will still find it and try to kill it */
58  }
59  } else {
60  ret = 256; /* Could not get handle */
61  pclose(fp);
62  }
63  } else {
64  ret = 256; /* fp NULL, so no process, just return */
65  return 0;
66  }
67  } /* end while */
68  return 0;
69 }
70 
71 void board_init(void)
72 {
73  /*
74  * Process running by default for firmware >= v1.0.5
75  *
76  * - /bin/sh - /usr/bin/DragonStarter.sh -out2null
77  * - //usr/bin/dragon-prog
78  *
79  * Thus two process to kill, the DragonStarter first
80  * This to make sure OEM program does not get re-started
81  *
82  */
83  int ret __attribute__((unused)) = system("killall -q -15 DragonStarter.sh");
84  usleep(50000); /* Give DragonStarter 50ms time to end on a busy system */
85  kill_gracefull("dragon-prog");
86 }
87 
88 void board_init2(void)
89 {
90 }
void board_init2(void)
Optional board init function called at the end of mcu_init().
Definition: board.c:92
void board_init(void)
Optional board init function called at the start of mcu_init().
Definition: board.c:130
static int kill_gracefull(char *process_name)
Definition: board.c:35
Arch independent mcu ( Micro Controller Unit ) utilities.