bool ipc_client::ipc_connect(void) { struct sockaddr_un remote; int len; if (sfd != -1) return true; if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { supla_log(LOG_ERR, "Socket error %i", errno); return false; } remote.sun_family = AF_UNIX; snprintf(remote.sun_path, sizeof(remote.sun_path), "%s", scfg_string(CFG_IPC_SOCKET_PATH)); len = strnlen(remote.sun_path, 107) + sizeof(remote.sun_family); if (connect(sfd, (struct sockaddr *)&remote, len) == -1) { supla_log(LOG_ERR, "IPC connect error %i", errno); ipc_disconnect(); return false; } if (read() && strcmp(buffer, hello) == 0) return true; ipc_disconnect(); return false; }
ipc_client::ipc_client() { this->sfd = -1; ipc_sauth_key = NULL; int ipc_shmid = -1; key_t key; key = ftok(scfg_string(CFG_IPC_SOCKET_PATH), 'S'); if ((ipc_shmid = shmget(key, IPC_SAUTH_KEY_SIZE, 0)) == -1) return; if ((ipc_sauth_key = (char *)shmat(ipc_shmid, 0, 0)) == (char *)-1) ipc_sauth_key = NULL; }
int scfg_getgid(unsigned char param_id) { char *name = scfg_string(param_id); if( name && strlen(name) > 0 ) { struct group *gr = getgrnam(name); /* don't free, see man getgrnam() for details */ if( gr ) return gr->gr_gid; } return getgid(); }
int scfg_getuid(unsigned char param_id) { char *name = scfg_string(param_id); if( name && strlen(name) > 0 ) { struct passwd *pwd = getpwnam(name); /* don't free, see man getpwnam() for details */ if( pwd ) return pwd->pw_uid; } return getuid(); }
unsigned char devcfg_init(int argc, char* argv[]) { memset(DEVICE_GUID, 0, SUPLA_GUID_SIZE); unsigned char result = 0; scfg_set_callback(devcfg_channel_cfg); // !!! order is important !!! char *s_global = "GLOBAL"; scfg_add_str_param(s_global, "device_guid_file", ""); scfg_add_str_param(s_global, "alt_cfg", ""); scfg_add_str_param(s_global, "state_file", ""); scfg_add_str_param(s_global, "device_name", ""); char *s_server = "SERVER"; scfg_add_str_param(s_server, "host", ""); scfg_add_int_param(s_server, "tcp_port", 2015); scfg_add_int_param(s_server, "ssl_port", 2016); scfg_add_bool_param(s_server, "ssl_enabled", 1); char *s_location = "LOCATION"; scfg_add_int_param(s_location, "ID", 0); scfg_add_str_param(s_location, "PASSWORD", 0); result = scfg_load(argc, argv, "/etc/supla-dev/supla.cfg"); if ( result == 1 && st_file_exists(scfg_string(CFG_ALTCFG_FILE)) == 1 ) { result = scfg_load(argc, argv, scfg_string(CFG_ALTCFG_FILE)); } scfg_names_free(); return result; }
char devcfg_getdev_guid() { return st_read_guid_from_file(scfg_string(CFG_GUID_FILE), DEVICE_GUID, 1); }
char *database::cfg_get_database(void) { return scfg_string(CFG_MYSQL_DB); }
char *database::cfg_get_password(void) { return scfg_string(CFG_MYSQL_PASSWORD); }
char *database::cfg_get_user(void) { return scfg_string(CFG_MYSQL_USER); }
char *database::cfg_get_host(void) { return scfg_string(CFG_MYSQL_HOST); }
int main(int argc, char* argv[]) { void *ssd_ssl = NULL; void *ssd_tcp = NULL; void* ipc = NULL; void *tcp_accept_loop_t = NULL; void *ssl_accept_loop_t = NULL; void *ipc_accept_loop_t = NULL; void *datalogger_loop_t = NULL; //INIT BLOCK supla_log(LOG_DEBUG, "Version 1.2 [Protocol v%i]", SUPLA_PROTO_VERSION); if ( svrcfg_init(argc, argv) == 0 ) return EXIT_FAILURE; if ( run_as_daemon && 0 == st_try_fork() ) { goto exit_fail; } if ( database::mainthread_init() == false ) { goto exit_fail; } #ifdef __OPEN_SSL if ( scfg_bool(CFG_SSL_ENABLED) == 1 ) { if ( 0 == ( ssd_ssl = ssocket_server_init(scfg_string(CFG_SSL_CERT), scfg_string(CFG_SSL_KEY), scfg_int(CFG_SSL_PORT), 1) ) || 0 == ssocket_openlistener(ssd_ssl) ) { goto exit_fail; } } #endif if ( scfg_bool(CFG_TCP_ENABLED) == 1 ) { if ( 0 == ( ssd_tcp = ssocket_server_init("", "", scfg_int(CFG_TCP_PORT), 0) ) || 0 == ssocket_openlistener(ssd_tcp) ) { goto exit_fail; } } if ( 0 == st_set_ug_id(scfg_getuid(CFG_UID), scfg_getgid(CFG_GID)) ) { goto exit_fail; } supla_user::init(); st_setpidfile(pidfile_path); st_mainloop_init(); st_hook_signals(); ipc = ipcsocket_init("/tmp/supla-server-ctrl.sock"); // INI ACCEPT LOOP if ( ssd_ssl != NULL ) ssl_accept_loop_t = sthread_simple_run(accept_loop, ssd_ssl, 0); if ( ssd_tcp != NULL ) tcp_accept_loop_t = sthread_simple_run(accept_loop, ssd_tcp, 0); if ( ipc ) ipc_accept_loop_t = sthread_simple_run(ipc_accept_loop, ipc, 0); // DATA LOGGER datalogger_loop_t = sthread_simple_run(datalogger_loop, NULL, 0); // MAIN LOOP while(st_app_terminate == 0) { st_mainloop_wait(1000000); } // RELEASE BLOCK if ( ipc != NULL ) { ipcsocket_close(ipc); sthread_twf(ipc_accept_loop_t); // ! after ipcsocket_close and before ipcsocket_free ! ipcsocket_free(ipc); } if ( ssd_ssl != NULL ) { ssocket_close(ssd_ssl); sthread_twf(ssl_accept_loop_t); // ! after ssocket_close and before ssocket_free ! ssocket_free(ssd_ssl); } if ( ssd_tcp != NULL ) { ssocket_close(ssd_tcp); sthread_twf(tcp_accept_loop_t); // ! after ssocket_close and before ssocket_free ! ssocket_free(ssd_tcp); } sthread_twf(datalogger_loop_t); st_mainloop_free(); st_delpidfile(pidfile_path); supla_user::free(); database::mainthread_end(); scfg_free(); return EXIT_SUCCESS; exit_fail: ssocket_free(ssd_ssl); ssocket_free(ssd_tcp); scfg_free(); exit(EXIT_FAILURE); }