Browse Source

com a biblioteca e usando systemd

dev
Rodgger 2 years ago
parent
commit
3fa53f42a6
  1. 12
      Makefile
  2. 11
      src/database/database.c
  3. 33
      src/main.c
  4. 4
      systemd/amid.service

12
Makefile

@ -24,14 +24,9 @@ PROGRAM_NAME=amid
DIR_OBJ=build/
DIR_SRC=src/
ifdef systemd
code_systemd=-DSYSTEMD=1
lib_systemd=-lsystemd
endif
CFLAGS= -I $(DIR_SRC) -Wall -g $(code_systemd) -DDEBUG $(shell mariadb_config --cflags --include )
LIBS = $(shell mariadb_config --libs ) $(lib_systemd) -lpthread -lami_c
CFLAGS= -I $(DIR_SRC) -Wall -g -DDEBUG $(shell mariadb_config --cflags --include )
LIBS = $(shell mariadb_config --libs ) -lpthread -lami_c -lsystemd
OBJ = \
build/main.o \
@ -84,9 +79,6 @@ $(DIR_OBJ)%.o: $(DIR_SRC)log/%.c
$(DIR_OBJ)%.o: $(DIR_SRC)command_cli/%.c
$(CC) -c -o $@ $< $(CFLAGS)
.PHONY:systemd
systemd:
@make systemd=1
install: $(PROGRAM_NAME)
install -d $(DESTDIR)/etc/systemd/system/

11
src/database/database.c

@ -195,7 +195,7 @@ int connect_db(s_manager *smanager){
return 0;
}
/*!
/*!
* \breif inicia a thread do banco de dados
* \param param s_manager
*/
@ -210,13 +210,22 @@ void *init_thread(void *param){
connect_db(smanager);
sd_notifyf( 0, "STATUS=Banco de dados conectado" );
if(init_stmt() == -1){
_exit(-1);
}
/* https://manpages.org/sd_notify/3
* Biblioteca libsystemd-dev
* comunicação com o systemd
*/
sd_notify( 0, "READY=1" );
/* inicia as actions para receber os valores básicos para o manager */
action_complete(ZERO_FILL_ACTION, smanager);
sd_notifyf( 0, "STATUS=OK" );
/* Loop principal thread 2 */
int loop = LOOP_ON;
while( loop == LOOP_ON){

33
src/main.c

@ -123,6 +123,7 @@ void read_arguments( s_manager *smanager){
char *str_database_username = "database_username=\"";
char *str_database_password = "database_password=\"";
sd_notifyf( 0, "STATUS=Read arguments" );
file = open( CLIENT_MANAGER_CONF, O_RDONLY );
if( file == -1 ){
@ -311,11 +312,23 @@ int init_daemon(int flags){
int maxfd, fd;
switch (fork()) {
case -1:
/* Comunicar com systemd
* https://manpages.org/sd_notify/3
*/
sd_notifyf( 0, "MAINPID=%d", getpid() );
pid_t pid_child = fork();
switch (pid_child) {
case -1:
return -1;
case 0: break;
default: _exit(EXIT_SUCCESS);
default:
/* https://manpages.org/sd_notify/3
* Biblioteca libsystemd-dev
* comunicação com o systemd que o pid principal mudou
*/
sd_notifyf( 0, "MAINPID=%d", pid_child );
_exit(EXIT_SUCCESS);
}/* processo roda em background */
if (setsid() == -1)
@ -353,18 +366,6 @@ int init_daemon(int flags){
return -1;
}
/* https://manpages.org/sd_notify/3
* Biblioteca libsystemd-dev
* comunicação com o systemd
*/
#ifdef SYSTEMD
int re_systemd = sd_notify( 0, "READY=1" );
_LOG("SYSTEMD READY=1: %d", re_systemd);
pid_t pid= getpid();
re_systemd = sd_notifyf( 0, "MAINPID=%d", pid );
_LOG("SYSTEMD MAINPID%d= %d", pid, re_systemd);
#endif
return 0;
}
@ -417,8 +418,10 @@ int main (int argc, char **argv){
}
attempt++;
}
sd_notifyf( 0, "STATUS=Asterisk conectado" );
_NOTICE("conectado com asterisk");
/* Essa thread é do banco de dados */
pthread_create(&thread, NULL, init_thread, (void *)smanager);

4
systemd/amid.service

@ -3,10 +3,12 @@ Description=Client asterisk AMI da SimplesIP
After=network.target
[Service]
Type=forking
Type=notify
Restart=always
RestartSec=2
ExecStart=/usr/sbin/amid
NotifyAccess=main
TimeoutStartSec=25
[Install]
WantedBy=multi-user.target

Loading…
Cancel
Save