1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <sys/socket.h>
27 #include "utmpd-private.h"
30 /* Prototypes for the local functions. */
31 static client_connection
*alloc_connection (void);
32 static void free_connection (client_connection
*connection
);
33 static int set_nonblock_flag (int desc
, int value
);
36 /* The head of the connection list. */
37 static client_connection
*connection_list
= NULL
;
40 /* Accept connection on SOCK, with access permissions given by ACCESS.
41 Returns a pointer to a newly allocated client_connection if
42 successful, NULL if not. */
44 accept_connection (int sock
, int access
)
46 client_connection
*connection
;
48 connection
= alloc_connection ();
49 if (connection
== NULL
)
52 connection
->sock
= accept (sock
, NULL
, NULL
);
53 connection
->access
= access
;
54 if (connection
->sock
< 0)
56 free_connection (connection
);
60 if (set_nonblock_flag (connection
->sock
, 1) < 0)
62 close_connection (connection
);
70 /* Close CONNECTION. */
72 close_connection (client_connection
*connection
)
74 close (connection
->sock
);
75 free_connection (connection
);
79 /* Return the connection for SOCK. */
81 find_connection (int sock
)
83 client_connection
*connection
;
85 for (connection
= connection_list
; connection
;
86 connection
= connection
->next
)
88 if (connection
->sock
== sock
)
96 static client_connection
*
97 alloc_connection (void)
99 client_connection
*connection
;
100 size_t read_bufsize
= 1024;
101 size_t write_bufsize
= 1024;
103 connection
= (client_connection
*)malloc (sizeof (client_connection
));
104 if (connection
== NULL
)
107 memset (connection
, 0, sizeof (client_connection
));
109 /* Allocate read buffer. */
110 connection
->read_base
= malloc (read_bufsize
);
111 connection
->read_ptr
= connection
->read_base
;
112 connection
->read_end
= connection
->read_base
+ read_bufsize
;
113 if (connection
->read_base
== NULL
)
119 /* Allocate write buffer. */
120 connection
->write_base
= malloc (write_bufsize
);
121 connection
->write_ptr
= connection
->write_base
;
122 connection
->write_end
= connection
->write_base
+ write_bufsize
;
123 if (connection
->write_base
== NULL
)
125 free (connection
->read_base
);
130 /* Link connection. */
131 connection
->next
= connection_list
;
132 connection_list
= connection
;
133 if (connection
->next
)
134 connection
->next
->prev
= connection
;
141 free_connection (client_connection
*connection
)
143 /* Unlink connection. */
144 if (connection
->next
)
145 connection
->next
->prev
= connection
->prev
;
146 if (connection
->prev
)
147 connection
->prev
->next
= connection
->next
;
149 /* Take care of the head of the list. */
150 if (connection
== connection_list
)
151 connection_list
= connection
->next
;
154 if (connection
->read_base
)
155 free (connection
->read_base
);
156 if (connection
->write_base
)
157 free (connection
->write_base
);
163 /* Set the `O_NONBLOCK' flag of DESC if VALUE is nonzero,
164 or clear the flag if VALUE is 0.
165 Return 0 on success, or -1 on error with `errno' set. */
167 set_nonblock_flag (int desc
, int value
)
169 int oldflags
= fcntl (desc
, F_GETFL
, 0);
170 /* If reading the flags failed, return error indication now. */
173 /* Set just the flag we want to set. */
175 oldflags
|= O_NONBLOCK
;
177 oldflags
&= ~O_NONBLOCK
;
178 /* Store modified flag word in the descriptor. */
179 return fcntl (desc
, F_SETFL
, oldflags
);