commit | author | age
|
d6b4a7
|
1 |
/******************************************************************************** |
G |
2 |
* Copyright: (C) 2020 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: util_proc.h |
|
6 |
* Description: This head file is for Linux process/thread API |
|
7 |
* |
|
8 |
* Version: 1.0.0(7/06/2012~) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "7/06/2012 09:21:33 PM" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#ifndef __UTIL_PROC_H_ |
|
15 |
#define __UTIL_PROC_H_ |
|
16 |
|
|
17 |
#include <signal.h> |
|
18 |
#include <time.h> |
|
19 |
|
|
20 |
#define PID_ASCII_SIZE 11 |
|
21 |
|
|
22 |
typedef struct proc_signal_s |
|
23 |
{ |
|
24 |
int signal; |
|
25 |
unsigned stop; /* 0: Not term 1: Stop */ |
|
26 |
} proc_signal_t; |
|
27 |
|
|
28 |
typedef void *(* thread_body_t) (void *thread_arg); |
|
29 |
|
|
30 |
extern proc_signal_t g_signal; |
|
31 |
|
|
32 |
/* install default signal process functions */ |
|
33 |
extern void install_default_signal(void); |
|
34 |
|
|
35 |
/* excute a linux command by system() */ |
|
36 |
extern void exec_system_cmd(const char *format, ...); |
|
37 |
|
|
38 |
/* check program already running or not, if not then run it and record pid into $pidfile */ |
|
39 |
extern int check_set_program_running(int daemon, char *pidfile); |
|
40 |
|
|
41 |
/* check program already running or not from $pid_file */ |
|
42 |
extern int check_daemon_running(const char *pid_file); |
|
43 |
|
|
44 |
/* set program daemon running and record pid in $pid_file */ |
|
45 |
extern int set_daemon_running(const char *pid_file); |
|
46 |
|
|
47 |
/* record proces ID into $pid_file */ |
|
48 |
extern int record_daemon_pid(const char *pid_file); |
|
49 |
|
|
50 |
/* stop program running from $pid_file */ |
|
51 |
extern int stop_daemon_running(const char *pid_file); |
|
52 |
|
|
53 |
/* my implementation for set program running in daemon */ |
|
54 |
extern void daemonize(int nochdir, int noclose); |
|
55 |
|
|
56 |
/* start a new thread to run $thread_workbody point function */ |
|
57 |
extern int thread_start(pthread_t *thread_id, thread_body_t thread_workbody, void *thread_arg); |
|
58 |
|
|
59 |
/* +---------------------+ |
|
60 |
* | Low level API | |
|
61 |
* +---------------------+*/ |
|
62 |
|
|
63 |
/* get daemon process ID from $pid_file */ |
|
64 |
extern pid_t get_daemon_pid(const char *pid_file); |
|
65 |
|
|
66 |
/* +------------------------+ |
|
67 |
* | inline functions API | |
|
68 |
* +------------------------+*/ |
|
69 |
static inline void msleep(unsigned long ms) |
|
70 |
{ |
|
71 |
struct timespec cSleep; |
|
72 |
unsigned long ulTmp; |
|
73 |
|
|
74 |
cSleep.tv_sec = ms / 1000; |
|
75 |
if (cSleep.tv_sec == 0) |
|
76 |
{ |
|
77 |
ulTmp = ms * 10000; |
|
78 |
cSleep.tv_nsec = ulTmp * 100; |
|
79 |
} |
|
80 |
else |
|
81 |
{ |
|
82 |
cSleep.tv_nsec = 0; |
|
83 |
} |
|
84 |
|
|
85 |
nanosleep(&cSleep, 0); |
|
86 |
return ; |
|
87 |
} |
|
88 |
|
|
89 |
#endif |