~mdz/network-manager/ubuntu.0.7

« back to all changes in this revision

Viewing changes to src/NetworkManagerUtils.c

  • Committer: dcbw
  • Date: 2004-06-24 14:18:37 UTC
  • Revision ID: vcs-imports@canonical.com-20040624141837-b23a721fd03ea23f
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* NetworkManager -- Network link manager
 
2
 *
 
3
 * Dan Williams <dcbw@redhat.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * (C) Copyright 2004 Red Hat, Inc.
 
20
 */
 
21
 
 
22
#include <glib.h>
 
23
#include <stdio.h>
 
24
#include <sys/types.h>
 
25
#include <sys/socket.h>
 
26
 
 
27
#include "NetworkManager.h"
 
28
#include "NetworkManagerUtils.h"
 
29
 
 
30
extern gboolean debug;
 
31
 
 
32
 
 
33
/*
 
34
 * nm_try_acquire_mutex
 
35
 *
 
36
 * Tries to acquire a given mutex, sleeping a bit between tries.
 
37
 *
 
38
 * Returns:     FALSE if mutex was not acquired
 
39
 *                      TRUE  if mutex was successfully acquired
 
40
 */
 
41
gboolean nm_try_acquire_mutex (GMutex *mutex, const char *func)
 
42
{
 
43
        gint    i = 5;
 
44
 
 
45
        g_return_val_if_fail (mutex != NULL, FALSE);
 
46
 
 
47
        while (i > 0)
 
48
        {
 
49
                if (g_mutex_trylock (mutex))
 
50
                {
 
51
/*
 
52
                        if (func)
 
53
                                NM_DEBUG_PRINT_1 ("MUTEX: %s got mutex\n", func);
 
54
*/
 
55
                        return (TRUE);
 
56
                }
 
57
                usleep (500);
 
58
                i++;
 
59
        }
 
60
 
 
61
        return (FALSE);
 
62
}
 
63
 
 
64
 
 
65
/*
 
66
 * nm_unlock_mutex
 
67
 *
 
68
 * Simply unlocks a mutex, balances nm_try_acquire_mutex()
 
69
 *
 
70
 */
 
71
void nm_unlock_mutex (GMutex *mutex, const char *func)
 
72
{
 
73
        g_return_if_fail (mutex != NULL);
 
74
 
 
75
/*
 
76
        if (func)
 
77
                NM_DEBUG_PRINT_1 ("MUTEX: %s released mutex\n", func);
 
78
*/
 
79
        g_mutex_unlock (mutex);
 
80
}
 
81
 
 
82
 
 
83
/*
 
84
 * nm_null_safe_strcmp
 
85
 *
 
86
 * Doesn't freaking segfault if s1/s2 are NULL
 
87
 *
 
88
 */
 
89
int nm_null_safe_strcmp (const char *s1, const char *s2)
 
90
{
 
91
        if (!s1 && !s2)
 
92
                return 0;
 
93
        if (!s1 && s2)
 
94
                return -1;
 
95
        if (s1 && !s2)
 
96
                return 1;
 
97
                
 
98
        return (strcmp (s1, s2));
 
99
}
 
100
 
 
101
 
 
102
 
 
103
/*
 
104
 * nm_get_network_control_socket
 
105
 *
 
106
 * Get a control socket for network operations.
 
107
 *
 
108
 */
 
109
int nm_get_network_control_socket (void)
 
110
{
 
111
        int     fd;
 
112
 
 
113
        /* Try to grab a control socket */
 
114
        fd = socket(PF_INET, SOCK_DGRAM, 0);
 
115
        if (fd >= 0)
 
116
                return (fd);
 
117
        fd = socket(PF_PACKET, SOCK_DGRAM, 0);
 
118
        if (fd >= 0)
 
119
                return (fd);
 
120
        fd = socket(PF_INET6, SOCK_DGRAM, 0);
 
121
        if (fd >= 0)
 
122
                return (fd);
 
123
 
 
124
        NM_DEBUG_PRINT ("nm_get_network_control_socket() could not get network control socket.\n");
 
125
        return (-1);
 
126
}
 
127
 
 
128
 
 
129
 
 
130
/*
 
131
 * nm_dispose_scan_results
 
132
 *
 
133
 * Free memory used by the wireless scan results structure
 
134
 *
 
135
 */
 
136
void nm_dispose_scan_results (wireless_scan *result_list)
 
137
{
 
138
        wireless_scan *tmp = result_list;
 
139
 
 
140
        while (tmp)
 
141
        {
 
142
                wireless_scan *tmp2 = tmp;
 
143
 
 
144
                tmp = tmp->next;
 
145
                free (tmp2);
 
146
        }
 
147
}