#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
-
#include <tizen.h>
#include "resource-monitor.h"
EXPORT
int pass_resource_monitor_init(void)
{
+ const char *server_socket_path = "/run/.pass-resource-monitor.socket";
struct pass_resource_monitor_client *client;
- struct sockaddr_in server_addr;
+ struct sockaddr_un server_addr;
int ret = TIZEN_ERROR_NO_DATA;
if (!is_privilege_supported(PRIVILEGE_SYSTEMMONITOR))
}
/* open socket to server */
- client->id = socket(AF_INET, SOCK_STREAM, 0);
+ client->id = socket(AF_UNIX, SOCK_STREAM, 0);
if (client->id < 0) {
_E("[libpass] socket creation error");
goto err_out_free;
}
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(REQUEST_SERVER_PORT);
-
- if (inet_pton(AF_INET, REQUEST_SERVER_IP, &server_addr.sin_addr) <= 0) {
- _E("[libpass] invalid address");
- goto err_out_close;
- }
+ bzero(&server_addr, sizeof(server_addr));
+ server_addr.sun_family = AF_UNIX;
+ strncpy(server_addr.sun_path, server_socket_path, sizeof(server_addr.sun_path));
+ server_addr.sun_path[sizeof(server_addr.sun_path) - 1] = '\0';
if (connect(client->id, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <assert.h>
return 0;
}
-static int request_server_func(void *ctx, void **result)
+static int init_unix_socket(int *sock, struct sockaddr_un *address, int *addrlen)
{
- struct sockaddr_in address;
- struct timeval wait;
+ const char *server_unix_socket_path = "/run/.pass-resource-monitor.socket";
int opt = true;
- int server_socket;
- int addrlen;
- int ret;
- fd_set fds;
- if (!g_request_server_run)
- return THREAD_RETURN_DONE;
-
- init_resource_id();
+ if (!sock || !address || !addrlen)
+ return -EINVAL;
- /* 0. initialize server socket */
- server_socket = socket(AF_INET, SOCK_STREAM, 0);
- if (server_socket < 0) {
+ *sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (*sock < 0) {
_E("Failed to initialize socket");
goto error_out;
}
- if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
+ if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt)) < 0) {
_E("Failed to setsockopt");
goto error_out_close;
}
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(REQUEST_SERVER_PORT);
+ bzero(address, sizeof(*address));
+ address->sun_family = AF_UNIX;
+ strncpy(address->sun_path, server_unix_socket_path, sizeof(address->sun_path));
+ address->sun_path[sizeof(address->sun_path) - 1] = '\0';
+
+ if (!access(server_unix_socket_path, F_OK))
+ unlink(server_unix_socket_path);
- if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
+ if (bind(*sock, (struct sockaddr *)address, sizeof(*address)) < 0) {
_E("Failed to bind");
goto error_out_close;
}
- if (listen(server_socket, PENDING_MAX) < 0) {
+ if (listen(*sock, PENDING_MAX) < 0) {
_E("Failed to begin listenning");
goto error_out_close;
}
- addrlen = sizeof(address);
+ *addrlen = sizeof(*address);
+
+ return 0;
+
+error_out_close:
+ close(*sock);
+error_out:
+ return -EIO;
+}
+
+int init_ip_socket(int *sock, struct sockaddr_in *address, int *addrlen)
+{
+ int opt = true;
+
+ if (!sock || !address || !addrlen)
+ return -EINVAL;
+
+ *sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (*sock < 0) {
+ _E("Failed to initialize TCP/IP socket");
+ goto error_out;
+ }
+
+ if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&opt, sizeof(opt)) < 0) {
+ _E("Failed to setsockopt for TCP/IP socket");
+ goto error_out_close;
+ }
+
+ bzero(address, sizeof(*address));
+ address->sin_family = AF_INET;
+ address->sin_addr.s_addr = INADDR_ANY;
+ address->sin_port = htons(REQUEST_SERVER_PORT);
+
+ if (bind(*sock, (struct sockaddr *)address, sizeof(*address)) < 0) {
+ _E("Failed to bind for TCP/IP socket");
+ goto error_out_close;
+ }
+
+ if (listen(*sock, PENDING_MAX) < 0) {
+ _E("Failed to begin listenning for TCP/IP socket");
+ goto error_out_close;
+ }
+
+ *addrlen = sizeof(*address);
+
+ return 0;
+
+error_out_close:
+ close(*sock);
+error_out:
+ return -EIO;
+}
+
+static int request_server_func(void *ctx, void **result)
+{
+ struct sockaddr_un unix_address;
+ struct sockaddr_in ip_address;
+ struct timeval wait;
+ int server_unix_socket;
+ int server_ip_socket;
+ int unix_addrlen;
+ int ip_addrlen;
+ int ret;
+ fd_set fds;
+
+ if (!g_request_server_run)
+ return THREAD_RETURN_DONE;
+
+ init_resource_id();
+
+ /* 0. initialize server socket */
+ ret = init_unix_socket(&server_unix_socket, &unix_address, &unix_addrlen);
+ if (ret < 0)
+ goto error_out;
+
+ /* initialize TCP socket */
+ ret = init_ip_socket(&server_ip_socket, &ip_address, &ip_addrlen);
+ if (ret < 0)
+ goto error_out_close_server_unix_socket;
while (g_request_server_run) {
FD_ZERO(&fds);
- FD_SET(server_socket, &fds);
+ FD_SET(server_unix_socket, &fds);
+ FD_SET(server_ip_socket, &fds);
wait.tv_sec = 1;
wait.tv_usec = 0;
goto error_out_close;
}
- if (FD_ISSET(server_socket, &fds)) {
- int new_socket = accept(server_socket, (struct sockaddr *)&address,
- (socklen_t *)&addrlen);
+ if (FD_ISSET(server_unix_socket, &fds)) {
+ int new_socket = accept(server_unix_socket, (struct sockaddr *)&unix_address,
+ (socklen_t *)&unix_addrlen);
+
+ if (new_socket < 0) {
+ _E("Failed to accept");
+ goto error_out_close;
+ }
+
+ create_request_client(new_socket);
+ }
+
+ if (FD_ISSET(server_ip_socket, &fds)) {
+ int new_socket = accept(server_ip_socket, (struct sockaddr *)&ip_address,
+ (socklen_t *)&ip_addrlen);
if (new_socket < 0) {
_E("Failed to accept");
}
}
- close(server_socket);
+ close(server_ip_socket);
+ close(server_unix_socket);
return THREAD_RETURN_DONE;
error_out_close:
- close(server_socket);
+ close(server_ip_socket);
+error_out_close_server_unix_socket:
+ close(server_unix_socket);
error_out:
return THREAD_RETURN_ERROR;