summaryrefslogtreecommitdiff
path: root/src/Util.cpp
blob: 0473d08c67d7d0b2f6940828d0801bf17d7d0d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Utils.cpp
 *
 *  Created on: 2014. 6. 10.
 *      Author: oskwon
 */

#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
#include <sys/wait.h>

#include <arpa/inet.h>
#include <sys/socket.h>

#include <sstream>
#include <fstream>

#include "Util.h"
#include "Logger.h"

using namespace std;
//----------------------------------------------------------------------

std::string Util::ultostr(int64_t data)
{
	std::stringstream ss;
	ss << data;
	return ss.str();
}
//----------------------------------------------------------------------

int Util::strtollu(std::string data)
{
	long long retval;
	std::stringstream ss;
	try {
		ss.str(data);
		ss >> retval;
	}
	catch(...) {
		return -1;
	}
	return retval;
}
//----------------------------------------------------------------------

std::string Util::trim(std::string& s, const std::string& drop)
{
	std::string r = s.erase(s.find_last_not_of(drop) + 1);
	return r.erase(0, r.find_first_not_of(drop));
}
//----------------------------------------------------------------------

int Util::split(std::string data, const char delimiter, std::vector<string>& tokens)
{
	std::stringstream data_stream(data);
	for(std::string token; std::getline(data_stream, token, delimiter); tokens.push_back(trim(token)));
	return tokens.size();
}
//----------------------------------------------------------------------

bool Util::split_key_value(std::string data, std::string delimiter, std::string &key, std::string &value)
{
	int idx = data.find(delimiter);
	if (idx == string::npos) {
		WARNING("split key & value (data : %s, delimiter : %s)", data.c_str(), delimiter.c_str());
		return false;
	}
	key = data.substr(0, idx);
	value = data.substr(idx+1, data.length()-idx);
	return true;
}
//----------------------------------------------------------------------

void Util::vlog(const char * format, ...) throw()
{
	static char vlog_buffer[MAX_PRINT_LEN];
    memset(vlog_buffer, 0, MAX_PRINT_LEN);

    va_list args;
	va_start(args, format);
	vsnprintf(vlog_buffer, MAX_PRINT_LEN-1, format, args);
	va_end(args);

	WARNING("%s", vlog_buffer);
}
//----------------------------------------------------------------------

std::string Util::host_addr()
{
	std::stringstream ss;
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);

    getpeername(0, (struct sockaddr*)&addr, &addrlen);
    ss << inet_ntoa(addr.sin_addr);

    return ss.str();
}
//-------------------------------------------------------------------------------

std::vector<int> Util::find_process_by_name(std::string name, int mypid)
{
	std::vector<int> pidlist;
	char cmdlinepath[256] = {0};
	DIR* d = opendir("/proc");
	if (d != 0) {
		struct dirent* de;
		while ((de = readdir(d)) != 0) {
			int pid = atoi(de->d_name);
			if (pid > 0) {
				sprintf(cmdlinepath, "/proc/%s/cmdline", de->d_name);

				std::string cmdline;
				std::ifstream cmdlinefile(cmdlinepath);
				std::getline(cmdlinefile, cmdline);
				if (!cmdline.empty()) {
					size_t pos = cmdline.find('\0');
					if (pos != string::npos)
					cmdline = cmdline.substr(0, pos);
					pos = cmdline.rfind('/');
					if (pos != string::npos)
					cmdline = cmdline.substr(pos + 1);
					if ((name == cmdline) && ((mypid != pid) || (mypid == 0))) {
						pidlist.push_back(pid);
					}
				}
			}
		}
		closedir(d);
	}
	return pidlist;
}
//-------------------------------------------------------------------------------

void Util::kill_process(int pid)
{
	int result = kill(pid, SIGINT);
	DEBUG("SEND SIGINT to %d, result : %d", pid, result);
	sleep(1);
}
//----------------------------------------------------------------------