hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ApplicationPattern.cpp
Go to the documentation of this file.
1 #include <ApplicationPattern.h>
3 ApplicationPattern::ApplicationPattern(char **argv, const char *configFile, unsigned int logDirection, const char *ident, const char *title)
4 {
5  this->argv = argv;
6  this->configFile = strdup(configFile);
7  if(!title)title = ident;
8  log = new(std::nothrow) SysLog(title, logDirection | OPT_ISO8601);
9  if(!log)
10  {
11  return;
12  }
13  exitFlag = 0;
14  sig.Attach(SIGQUIT, signalFunc);
15  sig.Attach(SIGINT, signalFunc);
16  sig.Attach(SIGTERM, signalFunc);
17  sig.Attach(SIGPIPE, signalFunc);
18  sig.Attach(SIGUSR1, signalFunc);
19  statisticHandler = NULL;
20  statusHandler = NULL;
21  adminClient = NULL;
22  logHandler = NULL;
23  this->ident = strdup(ident);
24 }
25 
27 {
28  delete log;
30  if(adminClient)
31  {
32  adminClient->stop();
33  adminClient->join();
34  delete adminClient;
35  }
37  {
38  delete statisticHandler;
39  }
40  if(statusHandler)
41  {
42  delete statusHandler;
43  }
44  if(logHandler)
45  {
46  delete logHandler;
47  }
48  free(ident);
49 }
50 
51 bool ApplicationPattern::init(const char *dumpFile)
52 {
53  Config config(configFile);
54  if(!config.isConfigExist())
55  {
56  log->Put(E_CONFIG, LEV_ERR, "Config file don't exists");
57  return false;
58  }
59  statisticHandler = new(std::nothrow) AStorageHandler();
60  if(!statisticHandler)
61  {
62  log->Put(E_MEMORY, LEV_EMERG, "Can't allocate memory");
63  return false;
64  }
65  statusHandler = new(std::nothrow) StatusHandler(statisticHandler);
66  if(!statusHandler)
67  {
68  log->Put(E_MEMORY, LEV_EMERG, "Can't allocate memory");
69  return false;
70  }
71  statisticHandler->setValue("ApplicationState", 1ll);
72  statisticHandler->setValue("StartDate", (long long)time(NULL));
73  statisticHandler->setValue("Revision", (long long)REVISION);
74  int maxLogLevel = 0;
75  config.getAsInt(ident, "log_level", maxLogLevel, 3);
76  log->setMaxLevel(maxLogLevel);
77  logHandler = new(std::nothrow) LogHandler(log);
78  if(!logHandler)
79  {
80  log->Put(E_MEMORY, LEV_EMERG, "Can't allocate memory");
81  return false;
82  }
83  handlerList.pushDown(statisticHandler);
84  handlerList.pushDown(statusHandler);
85  handlerList.pushDown(logHandler);
86  int adminPort = 0;
87  config.getAsInt(ident, "admin_port", adminPort, 10000);
88  char *adminHost = NULL;
89  config.getAsString(ident, "admin_host", adminHost, "0.0.0.0");
90  adminClient = new(std::nothrow) AdminClient(adminHost, adminPort, &handlerList);
91  delete []adminHost;
92  if(!adminClient)
93  {
94  log->Put(E_MEMORY, LEV_EMERG, "Can't allocate memory");
95  return false;
96  }
97  if(adminClient->getLastErr())
98  {
99  log->Put(E_NETWORK, LEV_ERR, "AdminClient: %s", strerror(adminClient->getLastErr()));
100  delete adminClient;
101  adminClient = NULL;
102  return false;
103  }
104  else
105  {
106  adminClient->start();
107  }
108  char *user = NULL;
109  config.getAsString(ident, "user", user, "hce");
110  if(!setUser(user))
111  {
112  delete []user;
113  return false;
114  }
115  delete []user;
116  char *path = NULL;
117  config.getAsString(ident, "log_path", path, "/var/log/hce/");
118  log->setLogPath(path);
119  delete []path;
120  return true;
121 }
122 
123 bool ApplicationPattern::setUser(const char *user)
124 {
125  char *buf = new(std::nothrow) char[sysconf(_SC_GETPW_R_SIZE_MAX)];
126  if(!buf)
127  {
128  return false;
129  }
130  struct passwd pwd;
131  struct passwd *ppwd = NULL;
132  memset(&pwd, 0, sizeof(pwd));
133  int ret = getpwnam_r(user, &pwd, buf, size_t(sysconf(_SC_GETPW_R_SIZE_MAX)), &ppwd);
134  if(ret == 0 && ppwd)
135  {
136  if(setgid(pwd.pw_gid) == 0)
137  {
138  if(setuid(pwd.pw_uid) == 0)
139  {
140  log->Put(E_OK, LEV_INFO, "Change user %s succefull", user);
141  delete []buf;
142  return true;
143  }
144  else
145  {
146  log->Put(E_SETUID, LEV_ERR, "Can't set uid to %d", pwd.pw_gid);
147  }
148  }
149  else
150  {
151  log->Put(E_SETGID, LEV_ERR, "Can't set gid to %d", pwd.pw_gid);
152  }
153  }
154  else
155  {
156  log->Put(E_GETUIDGID, LEV_ERR, "Can't get uid & gid for user: %s", user);
157  }
158  delete []buf;
159  return false;
160 }
161 
162 
163 void ApplicationPattern::signalFunc(int sigNum)
164 {
165  if(sigNum != SIGPIPE && sigNum != SIGUSR1)
166  {
167  exitFlag = 1;
168  }
169 }
170