Use socketclose on windows as appropriate; end pid files with newline

svn:r1745
This commit is contained in:
Nick Mathewson 2004-04-28 21:14:56 +00:00
parent dd335d9bb2
commit 9322641710
6 changed files with 20 additions and 14 deletions

View File

@ -930,7 +930,7 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
goto tidy_up_and_fail; goto tidy_up_and_fail;
if (size != sizeof(listen_addr)) if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail; goto abort_tidy_up_and_fail;
close(listener); tor_close_socket(listener);
/* Now check we are talking to ourself by matching port and host on the /* Now check we are talking to ourself by matching port and host on the
two sockets. */ two sockets. */
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1) if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
@ -955,11 +955,11 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
{ {
int save_errno = errno; int save_errno = errno;
if (listener != -1) if (listener != -1)
close(listener); tor_close_socket(listener);
if (connector != -1) if (connector != -1)
close(connector); tor_close_socket(connector);
if (acceptor != -1) if (acceptor != -1)
close(acceptor); tor_close_socket(acceptor);
errno = save_errno; errno = save_errno;
return -1; return -1;
} }
@ -1319,7 +1319,7 @@ void write_pidfile(char *filename) {
log_fn(LOG_WARN, "unable to open %s for writing: %s", filename, log_fn(LOG_WARN, "unable to open %s for writing: %s", filename,
strerror(errno)); strerror(errno));
} else { } else {
fprintf(pidfile, "%d", (int)getpid()); fprintf(pidfile, "%d\n", (int)getpid());
fclose(pidfile); fclose(pidfile);
} }
#endif #endif

View File

@ -56,6 +56,12 @@
} } while (0) } } while (0)
#endif #endif
#ifdef MS_WINDOWS
#define tor_close_socket(s) socketclose(s)
#else
#define tor_close_socket(s) close(s)
#endif
/* legal characters in a filename */ /* legal characters in a filename */
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/" #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"

View File

@ -121,7 +121,7 @@ void connection_free(connection_t *conn) {
if(conn->s >= 0) { if(conn->s >= 0) {
log_fn(LOG_INFO,"closing fd %d.",conn->s); log_fn(LOG_INFO,"closing fd %d.",conn->s);
close(conn->s); tor_close_socket(conn->s);
} }
memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */ memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */
free(conn); free(conn);
@ -150,7 +150,7 @@ void connection_close_immediate(connection_t *conn)
log_fn(LOG_INFO,"Closing connection (fd %d, type %s, state %d) with data on outbuf.", log_fn(LOG_INFO,"Closing connection (fd %d, type %s, state %d) with data on outbuf.",
conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state); conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
} }
close(conn->s); tor_close_socket(conn->s);
conn->s = -1; conn->s = -1;
if(!connection_is_listener(conn)) { if(!connection_is_listener(conn)) {
buf_clear(conn->outbuf); buf_clear(conn->outbuf);
@ -399,7 +399,7 @@ int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_
if(!ERRNO_CONN_EINPROGRESS(errno)) { if(!ERRNO_CONN_EINPROGRESS(errno)) {
/* yuck. kill it. */ /* yuck. kill it. */
log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,strerror(errno)); log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,strerror(errno));
close(s); tor_close_socket(s);
return -1; return -1;
} else { } else {
/* it's in progress. set state appropriately and return. */ /* it's in progress. set state appropriately and return. */

View File

@ -1091,7 +1091,7 @@ int connection_ap_make_bridge(char *address, uint16_t port) {
if(connection_add(conn) < 0) { /* no space, forget it */ if(connection_add(conn) < 0) { /* no space, forget it */
connection_free(conn); /* this closes fd[0] */ connection_free(conn); /* this closes fd[0] */
close(fd[1]); tor_close_socket(fd[1]);
return -1; return -1;
} }
@ -1101,7 +1101,7 @@ int connection_ap_make_bridge(char *address, uint16_t port) {
/* attaching to a dirty circuit is fine */ /* attaching to a dirty circuit is fine */
if (connection_ap_handshake_attach_circuit(conn) < 0) { if (connection_ap_handshake_attach_circuit(conn) < 0) {
connection_mark_for_close(conn, 0); connection_mark_for_close(conn, 0);
close(fd[1]); tor_close_socket(fd[1]);
return -1; return -1;
} }

View File

@ -149,7 +149,7 @@ int cpuworker_main(void *data) {
char tag[TAG_LEN]; char tag[TAG_LEN];
crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL; crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
close(fdarray[0]); /* this is the side of the socketpair the parent uses */ tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
fd = fdarray[1]; /* this side is ours */ fd = fdarray[1]; /* this side is ours */
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
connection_free_all(); /* so the child doesn't hold the parent's fd's open */ connection_free_all(); /* so the child doesn't hold the parent's fd's open */
@ -220,7 +220,7 @@ static int spawn_cpuworker(void) {
spawn_func(cpuworker_main, (void*)fd); spawn_func(cpuworker_main, (void*)fd);
log_fn(LOG_DEBUG,"just spawned a worker."); log_fn(LOG_DEBUG,"just spawned a worker.");
close(fd[1]); /* we don't need the worker's side of the pipe */ tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
conn = connection_new(CONN_TYPE_CPUWORKER); conn = connection_new(CONN_TYPE_CPUWORKER);

View File

@ -455,7 +455,7 @@ int dnsworker_main(void *data) {
int *fdarray = data; int *fdarray = data;
int fd; int fd;
close(fdarray[0]); /* this is the side of the socketpair the parent uses */ tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
fd = fdarray[1]; /* this side is ours */ fd = fdarray[1]; /* this side is ours */
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
connection_free_all(); /* so the child doesn't hold the parent's fd's open */ connection_free_all(); /* so the child doesn't hold the parent's fd's open */
@ -509,7 +509,7 @@ static int spawn_dnsworker(void) {
spawn_func(dnsworker_main, (void*)fd); spawn_func(dnsworker_main, (void*)fd);
log_fn(LOG_DEBUG,"just spawned a worker."); log_fn(LOG_DEBUG,"just spawned a worker.");
close(fd[1]); /* we don't need the worker's side of the pipe */ tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
conn = connection_new(CONN_TYPE_DNSWORKER); conn = connection_new(CONN_TYPE_DNSWORKER);