~ubuntu-branches/ubuntu/maverick/ldtp/maverick

« back to all changes in this revision

Viewing changes to src/ldtp-server.c

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2010-02-04 10:36:08 UTC
  • mfrom: (1.4.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20100204103608-dhqdo7jk10ygwt40
Tags: 2.0.2-1
* New upstream release:
  + Packaging is based on Ubuntu packages, Thanks Ubuntu!
  + LDTPv2 is a complete rewrite of LDTPv1 in Python
  + LTFX is completely removed in LDTP v2 in favor of wnck
* debian/control:
  + Updated to Standards-Version 3.8.4 (no changes needed)
  + Fixed typo in description python->Python
  + ldtp is now arch: all package
* debian/rules:
  + Using dh to make it simple
* Removed unused manpages
* Updated package to use new source format 3.0 (quilt)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
 
/*
3
 
 * Linux Desktop Testing Project http://ldtp.freedesktop.org
4
 
 *
5
 
 * Author:
6
 
 *    Veerapuram Varadhan <v.varadhan@gmail.com>
7
 
 *
8
 
 * Copyright 2004 - 2006 Novell, Inc.
9
 
 * Copyright 2007 - 2008 Nagappan Alagappan
10
 
 *
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.
15
 
 *
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.
20
 
 *
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.
25
 
 */
26
 
 
27
 
#include <stdio.h>
28
 
#include <unistd.h>
29
 
#include <errno.h>
30
 
#include <fcntl.h>
31
 
#include <stdlib.h>
32
 
#include <string.h>
33
 
#include <sys/stat.h>
34
 
#include <sys/types.h>
35
 
#include <sys/socket.h>
36
 
#include <sys/un.h>
37
 
#include <glib.h>
38
 
#include <arpa/inet.h>
39
 
#include <netinet/in.h>
40
 
 
41
 
#include "ldtp-server.h"
42
 
#include "ldtp-logger.h"
43
 
 
44
 
#define LDTP_SCRIPT_ENGINE_PORT 23456
45
 
 
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
49
 
 
50
 
char*
51
 
get_tmp_file (int server_type)
52
 
{
53
 
        if (server_type == LDTP_SCRIPT_SERVER) {
54
 
                if (getenv ("USER"))
55
 
                        return g_strdup_printf ("/tmp/ldtp-%s-%s", getenv ("USER"), getenv ("DISPLAY"));
56
 
                else
57
 
                        return g_strdup_printf ("/tmp/ldtp-%s-%s", getenv ("LOGNAME"), getenv ("DISPLAY"));
58
 
        } else
59
 
                return NULL;
60
 
}
61
 
 
62
 
int
63
 
init_ldtp_server (int server_type)
64
 
{
65
 
        int yes = 1;        // for setsockopt() SO_REUSEADDR, below
66
 
        gchar* tmpfile = NULL;
67
 
        int listener = 0;
68
 
        extern gint ldtp_script_port;
69
 
        extern gboolean ldtp_script_service;
70
 
 
71
 
        if (server_type == LDTP_SCRIPT_SERVER &&
72
 
            (ldtp_script_service)) {
73
 
                // get the listener
74
 
                if ((listener = socket (PF_INET, SOCK_STREAM, 0)) == -1) {
75
 
                        ldtp_log ("ERROR:socket() failed with \"%s\"\n", strerror(errno));
76
 
                        exit (-1);
77
 
                }
78
 
        }
79
 
        else {
80
 
                // get the listener
81
 
                if ((listener = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
82
 
                        ldtp_log ("ERROR:socket() failed with \"%s\"\n", strerror(errno));
83
 
                        exit (-1);
84
 
                }
85
 
        }
86
 
        if (server_type == LDTP_SCRIPT_SERVER)
87
 
                script_listener = listener;
88
 
 
89
 
        if (setsockopt (listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
90
 
                ldtp_log ("ERROR: setsockopt() failed with \"%s\"\n", strerror (errno));
91
 
                exit (-1);
92
 
        }
93
 
        if (server_type == LDTP_SCRIPT_SERVER &&
94
 
            (ldtp_script_service)) {
95
 
                /*
96
 
                  Puneet Mishra: Palm Source Inc
97
 
                 */
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);
102
 
                else
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));
110
 
                        exit (-1);
111
 
                }
112
 
        }
113
 
        else {
114
 
                // bind
115
 
                tmpfile = get_tmp_file (server_type);
116
 
                /*
117
 
                  If file already exist, we need to unlink it, otherwise we will get bind error
118
 
                */
119
 
                unlink (tmpfile);
120
 
                strcpy (un_myaddr.sun_path, tmpfile);
121
 
                g_free (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));
125
 
                        exit (-1);
126
 
                }
127
 
        }
128
 
 
129
 
        // listen
130
 
        if (listen (listener, 10) == -1) {
131
 
                ldtp_log ("ERROR: listen() failed with \"%s\"\n", strerror (errno));
132
 
                exit (-1);
133
 
        }
134
 
        return listener;
135
 
}
136
 
 
137
 
int
138
 
get_server_socket (int server_type)
139
 
{
140
 
        if (server_type == LDTP_SCRIPT_SERVER)
141
 
                return script_listener;
142
 
        else
143
 
                return 0;
144
 
}