common exit handler (#19661)

* common signal handle

* fix typo

* rename to ExitHandler

* remove include signal.h

* remove include csignal

* rename variable exit to v

* rebase master
albatross
Dean Lee 2021-01-06 12:19:53 +08:00 committed by GitHub
parent 54679cea58
commit 86fe2d1697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 52 additions and 142 deletions

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
@ -45,11 +44,7 @@ bool fake_send = false;
bool connected_once = false;
bool ignition = false;
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
struct tm get_time(){
time_t rawtime;
time(&rawtime);
@ -525,10 +520,6 @@ int main() {
err = set_core_affinity(3);
LOG("set affinity returns %d", err);
// setup signal handlers
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
// check the environment
if (getenv("STARTED")) {
spoofing_started = true;

View File

@ -1,6 +1,5 @@
#include <thread>
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
@ -22,6 +21,7 @@
#include "common/params.h"
#include "common/swaglog.h"
#include "common/util.h"
#include "common/utilpp.h"
#include "imgproc/utils.h"
const int env_xmin = getenv("XMIN") ? atoi(getenv("XMIN")) : 0;
@ -349,7 +349,7 @@ void set_exposure_target(CameraState *c, const uint8_t *pix_ptr, int x_start, in
camera_autoexposure(c, lum_med / 256.0);
}
extern volatile sig_atomic_t do_exit;
extern ExitHandler do_exit;
void *processing_thread(MultiCameraState *cameras, const char *tname,
CameraState *cs, process_thread_cb callback) {

View File

@ -3,12 +3,12 @@
#include <unistd.h>
#include <cassert>
#include <string.h>
#include <signal.h>
#include <libyuv.h>
#include "messaging.hpp"
#include "common/util.h"
#include "common/utilpp.h"
#include "common/timing.h"
#include "common/swaglog.h"
#include "buffering.h"
@ -17,7 +17,7 @@ extern "C" {
#include <libavcodec/avcodec.h>
}
extern volatile sig_atomic_t do_exit;
extern ExitHandler do_exit;
#define FRAME_WIDTH 1164
#define FRAME_HEIGHT 874

View File

@ -34,7 +34,7 @@
#include "camera_qcom.h"
extern volatile sig_atomic_t do_exit;
extern ExitHandler do_exit;
// global var for AE/AF ops
std::atomic<CameraExpInfo> rear_exp{{0}};

View File

@ -3,7 +3,6 @@
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/ioctl.h>
@ -32,7 +31,7 @@
#define FRAME_STRIDE 2416 // for 10 bit output
extern volatile sig_atomic_t do_exit;
extern ExitHandler do_exit;
// global var for AE ops
std::atomic<CameraExpInfo> cam_exp[3] = {{{0}}};

View File

@ -3,10 +3,10 @@
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include "common/util.h"
#include "common/utilpp.h"
#include "common/timing.h"
#include "common/clutil.h"
#include "common/swaglog.h"
@ -20,7 +20,7 @@
#pragma clang diagnostic pop
extern volatile sig_atomic_t do_exit;
extern ExitHandler do_exit;
#define FRAME_WIDTH 1164
#define FRAME_HEIGHT 874

View File

@ -1,6 +1,5 @@
#include <thread>
#include <stdio.h>
#include <signal.h>
#include <poll.h>
#include <assert.h>
#include <unistd.h>
@ -23,15 +22,12 @@
#include "common/params.h"
#include "common/swaglog.h"
#include "common/util.h"
#include "common/utilpp.h"
#include "common/visionipc.h"
#define MAX_CLIENTS 6
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
struct VisionState;
@ -331,9 +327,6 @@ int main(int argc, char *argv[]) {
set_core_affinity(6);
#endif
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
// TODO: do this for QCOM2 too

View File

@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
@ -18,12 +17,9 @@
#include "messaging.hpp"
#include "common/timing.h"
#include "common/util.h"
#include "common/utilpp.h"
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
#ifdef QCOM
namespace {
@ -37,10 +33,6 @@ namespace {
int main() {
setpriority(PRIO_PROCESS, 0, -13);
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
PubMaster pm({"clocks"});
#ifndef __APPLE__

View File

@ -3,10 +3,6 @@
#include <stdio.h>
#ifndef sighandler_t
typedef void (*sighandler_t)(int sig);
#endif
#ifndef __cplusplus
#define min(a,b) \

View File

@ -2,14 +2,19 @@
#include <cstdio>
#include <unistd.h>
#include <csignal>
#include <string>
#include <memory>
#include <atomic>
#include <sstream>
#include <fstream>
#include <thread>
#include <chrono>
#ifndef sighandler_t
typedef void (*sighandler_t)(int sig);
#endif
namespace util {
inline bool starts_with(std::string s, std::string prefix) {
@ -76,6 +81,22 @@ inline void sleep_for(const int milliseconds) {
}
}
class ExitHandler {
public:
ExitHandler() {
std::signal(SIGINT, (sighandler_t)set_do_exit);
std::signal(SIGTERM, (sighandler_t)set_do_exit);
};
inline operator bool() { return do_exit; }
inline ExitHandler& operator=(bool v) {
do_exit = v;
return *this;
}
private:
static void set_do_exit(int sig) { do_exit = true; }
inline static std::atomic<bool> do_exit = false;
};
struct unique_fd {
unique_fd(int fd = -1) : fd_(fd) {}
unique_fd& operator=(unique_fd&& uf) {

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
@ -14,23 +13,17 @@
#include "messaging.hpp"
#include "common/util.h"
#include "common/utilpp.h"
#include "common/params.h"
#include "common/swaglog.h"
#include "common/timing.h"
#include "ublox_msg.h"
volatile sig_atomic_t do_exit = 0; // Flag for process exit on signal
void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
using namespace ublox;
int ubloxd_main(poll_ubloxraw_msg_func poll_func, send_gps_event_func send_func) {
LOGW("starting ubloxd");
signal(SIGINT, (sighandler_t) set_do_exit);
signal(SIGTERM, (sighandler_t) set_do_exit);
UbloxMsgParser parser;

View File

@ -11,20 +11,13 @@
#include <log/logprint.h>
#include "common/timing.h"
#include "common/utilpp.h"
#include "messaging.hpp"
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
int main() {
int err;
// setup signal handlers
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
struct logger_list *logger_list = android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0);
assert(logger_list);
struct logger *main_logger = android_logger_open(logger_list, LOG_ID_MAIN);

View File

@ -8,20 +8,12 @@
#include <systemd/sd-journal.h>
#include "common/timing.h"
#include "common/utilpp.h"
#include "messaging.hpp"
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
int main(int argc, char *argv[]) {
// setup signal handlers
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
PubMaster pm({"androidLog"});
sd_journal *journal;

View File

@ -3,7 +3,6 @@
#include <cstdint>
#include <cassert>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <poll.h>
#include <string.h>
@ -120,11 +119,7 @@ double randrange(double a, double b) {
return dist(gen);
}
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
static bool file_exists(const std::string& fn) {
std::ifstream f(fn);
@ -541,10 +536,6 @@ int main(int argc, char** argv) {
clear_locks();
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
typedef struct QlogState {
int counter, freq;
} QlogState;

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <cassert>
#include <sys/resource.h>
@ -17,19 +16,12 @@
#endif
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
int main(int argc, char **argv) {
int err;
setpriority(PRIO_PROCESS, 0, -15);
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
PubMaster pm({"driverState"});
// init the models

View File

@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <eigen3/Eigen/Dense>
@ -13,11 +12,7 @@
#include "models/driving.h"
#include "messaging.hpp"
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
// globals
bool run_model;
mat3 cur_transform;
@ -103,9 +98,6 @@ int main(int argc, char **argv) {
set_core_affinity(4);
#endif
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
pthread_mutex_init(&transform_lock, NULL);
// start calibration thread

View File

@ -5,7 +5,6 @@
#include <cstdlib>
#include <climits>
#include <cassert>
#include <csignal>
#include <memory>
#include <utility>
#include <sstream>
@ -20,15 +19,9 @@
#include "common/util.h"
#include "common/utilpp.h"
volatile sig_atomic_t do_exit = 0;
ExitHandler do_exit;
namespace {
static void set_do_exit(int sig) {
do_exit = 1;
}
struct ProcCache {
std::string name;
std::vector<std::string> cmdline;
@ -38,9 +31,6 @@ struct ProcCache {
}
int main() {
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
PubMaster publisher({"procLog"});
double jiffy = sysconf(_SC_CLK_TCK);

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
@ -18,9 +17,10 @@
#include "messaging.hpp"
#include "common/timing.h"
#include "common/utilpp.h"
#include "common/swaglog.h"
volatile sig_atomic_t do_exit = 0;
ExitHandler do_exit;
namespace {
@ -29,10 +29,6 @@ PubMaster *pm;
const GpsInterface* gGpsInterface = NULL;
const AGpsInterface* gAGpsInterface = NULL;
void set_do_exit(int sig) {
do_exit = 1;
}
void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length) {
uint64_t log_time_wall = nanos_since_epoch();
@ -149,9 +145,6 @@ void gps_destroy() {
int main() {
setpriority(PRIO_PROCESS, 0, -13);
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
gps_init();
while(!do_exit) pause();

View File

@ -2,7 +2,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
@ -19,6 +18,7 @@
#include "messaging.hpp"
#include "common/timing.h"
#include "common/utilpp.h"
#include "common/swaglog.h"
// ACCELEROMETER_UNCALIBRATED is only in Android O
@ -32,15 +32,11 @@
#define SENSOR_PROXIMITY 6
#define SENSOR_LIGHT 7
volatile sig_atomic_t do_exit = 0;
ExitHandler do_exit;
volatile sig_atomic_t re_init_sensors = 0;
namespace {
void set_do_exit(int sig) {
do_exit = 1;
}
void sigpipe_handler(int sig) {
LOGE("SIGPIPE received");
re_init_sensors = true;
@ -224,8 +220,6 @@ void sensor_loop() {
int main(int argc, char *argv[]) {
setpriority(PRIO_PROCESS, 0, -13);
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
signal(SIGPIPE, (sighandler_t)sigpipe_handler);
sensor_loop();

View File

@ -1,5 +1,4 @@
#include <vector>
#include <csignal>
#include <chrono>
#include <thread>
#include <sys/resource.h>
@ -7,6 +6,7 @@
#include "messaging.hpp"
#include "common/i2c.h"
#include "common/timing.h"
#include "common/utilpp.h"
#include "common/swaglog.h"
#include "sensors/sensor.hpp"
@ -25,11 +25,7 @@
#define I2C_BUS_IMU 1
volatile sig_atomic_t do_exit = 0;
void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
int sensor_loop() {
I2CBus *i2c_bus_imu;
@ -98,8 +94,5 @@ int sensor_loop() {
int main(int argc, char *argv[]) {
setpriority(PRIO_PROCESS, 0, -13);
signal(SIGINT, set_do_exit);
signal(SIGTERM, set_do_exit);
return sensor_loop();
}

View File

@ -15,11 +15,7 @@
#include "paint.hpp"
#include "android/sl_sound.hpp"
volatile sig_atomic_t do_exit = 0;
static void set_do_exit(int sig) {
do_exit = 1;
}
ExitHandler do_exit;
static void ui_set_brightness(UIState *s, int brightness) {
static int last_brightness = -1;
if (last_brightness != brightness && (s->awake || brightness == 0)) {
@ -114,7 +110,6 @@ static void update_offroad_layout_state(UIState *s, PubMaster *pm) {
int main(int argc, char* argv[]) {
setpriority(PRIO_PROCESS, 0, -14);
signal(SIGINT, (sighandler_t)set_do_exit);
SLSound sound;
UIState uistate = {};