1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3
* Linux Desktop Testing Project http://ldtp.freedesktop.org
6
* Veerapuram Varadhan <v.varadhan@gmail.com>
8
* Copyright 2004 - 2006 Novell, Inc.
9
* Copyright 2007 - 2008 Nagappan Alagappan
11
* This program is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2 of the License, or (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this program; if not, write to the
23
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24
* Boston, MA 02110, USA.
34
#include <sys/types.h>
35
#include <sys/socket.h>
38
#include <arpa/inet.h>
39
#include <netinet/in.h>
41
#include "ldtp-server.h"
42
#include "ldtp-logger.h"
44
#define LDTP_SCRIPT_ENGINE_PORT 23456
46
static struct sockaddr_un un_myaddr; // server address // sockaddr_un
47
static struct sockaddr_in script_myaddr; // server address // sockaddr_in
48
int script_listener = 0; // listening socket descriptor
51
get_tmp_file (int server_type)
53
if (server_type == LDTP_SCRIPT_SERVER) {
55
return g_strdup_printf ("/tmp/ldtp-%s-%s", getenv ("USER"), getenv ("DISPLAY"));
57
return g_strdup_printf ("/tmp/ldtp-%s-%s", getenv ("LOGNAME"), getenv ("DISPLAY"));
63
init_ldtp_server (int server_type)
65
int yes = 1; // for setsockopt() SO_REUSEADDR, below
66
gchar* tmpfile = NULL;
68
extern gint ldtp_script_port;
69
extern gboolean ldtp_script_service;
71
if (server_type == LDTP_SCRIPT_SERVER &&
72
(ldtp_script_service)) {
74
if ((listener = socket (PF_INET, SOCK_STREAM, 0)) == -1) {
75
ldtp_log ("ERROR:socket() failed with \"%s\"\n", strerror(errno));
81
if ((listener = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
82
ldtp_log ("ERROR:socket() failed with \"%s\"\n", strerror(errno));
86
if (server_type == LDTP_SCRIPT_SERVER)
87
script_listener = listener;
89
if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
90
ldtp_log ("ERROR: setsockopt() failed with \"%s\"\n", strerror (errno));
93
if (server_type == LDTP_SCRIPT_SERVER &&
94
(ldtp_script_service)) {
96
Puneet Mishra: Palm Source Inc
98
script_myaddr.sin_family = AF_INET;
99
g_print ("Port: %d\n", ldtp_script_port);
100
if (ldtp_script_port)
101
script_myaddr.sin_port = htons (ldtp_script_port);
103
script_myaddr.sin_port = htons (LDTP_SCRIPT_ENGINE_PORT);
104
script_myaddr.sin_addr.s_addr = htonl (INADDR_ANY);
105
memset(&(script_myaddr.sin_zero), '\0', 8);
106
g_print ("**Script myaddr.sin_addr.s_addr %s:%d\n",
107
inet_ntoa (script_myaddr.sin_addr), ntohs (script_myaddr.sin_port));
108
if (bind (listener, (struct sockaddr *) &script_myaddr, sizeof (struct sockaddr)) == -1) {
109
ldtp_log ("Script ERROR: bind() failed with \"%s\"\n", strerror (errno));
115
tmpfile = get_tmp_file (server_type);
117
If file already exist, we need to unlink it, otherwise we will get bind error
120
strcpy (un_myaddr.sun_path, tmpfile);
122
un_myaddr.sun_family = AF_UNIX;
123
if (bind (listener, (struct sockaddr *) &un_myaddr, sizeof (un_myaddr)) == -1) {
124
ldtp_log ("ERROR: bind() failed with \"%s\"\n", strerror (errno));
130
if (listen (listener, 10) == -1) {
131
ldtp_log ("ERROR: listen() failed with \"%s\"\n", strerror (errno));
138
get_server_socket (int server_type)
140
if (server_type == LDTP_SCRIPT_SERVER)
141
return script_listener;