2005-10-17 21:36:57 +08:00
# include "config.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include <stdlib.h>
# include <stdio.h>
# include <wchar.h>
2006-08-10 06:34:52 +08:00
# include <string.h>
2005-09-20 21:26:39 +08:00
# include <unistd.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/un.h>
# include <pwd.h>
# include <errno.h>
# include <fcntl.h>
2006-01-19 20:22:07 +08:00
2005-10-11 00:12:55 +08:00
# if HAVE_NCURSES_H
# include <ncurses.h>
# else
# include <curses.h>
# endif
2006-01-19 20:22:07 +08:00
# if HAVE_TERM_H
2005-09-20 21:26:39 +08:00
# include <term.h>
2006-01-19 20:22:07 +08:00
# elif HAVE_NCURSES_TERM_H
# include <ncurses/term.h>
# endif
2006-08-10 06:26:05 +08:00
# ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
# endif
2005-09-20 21:26:39 +08:00
# include <signal.h>
2006-02-28 21:17:16 +08:00
# include "fallback.h"
2005-09-20 21:26:39 +08:00
# include "util.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include "common.h"
# include "wutil.h"
# include "env_universal_common.h"
# include "env_universal.h"
/**
Maximum number of times to try to get a new fishd socket
*/
# define RECONNECT_COUNT 32
connection_t env_universal_server ;
/**
Set to 1 after initialization has been performed
*/
static int init = 0 ;
/**
The number of attempts to start fishd
*/
static int get_socket_count = 0 ;
2005-10-03 21:24:46 +08:00
static wchar_t * path ;
static wchar_t * user ;
static void ( * start_fishd ) ( ) ;
2005-10-12 03:23:43 +08:00
static void ( * external_callback ) ( int type , const wchar_t * name , const wchar_t * val ) ;
2005-09-23 04:16:52 +08:00
2005-10-03 21:24:46 +08:00
/**
Flag set to 1 when a barrier reply is recieved
*/
2005-09-20 21:26:39 +08:00
static int barrier_reply = 0 ;
2005-09-23 04:16:52 +08:00
void env_universal_barrier ( ) ;
2005-09-20 21:26:39 +08:00
2006-11-15 20:34:47 +08:00
static int is_dead ( )
{
return env_universal_server . fd < 0 ;
}
2005-09-20 21:26:39 +08:00
/**
Get a socket for reading from the server
*/
static int get_socket ( int fork_ok )
{
int s , len ;
struct sockaddr_un local ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
char * name ;
wchar_t * wdir ;
2011-12-27 11:18:46 +08:00
wchar_t * wuname ;
2005-09-20 21:26:39 +08:00
char * dir = 0 , * uname = 0 ;
get_socket_count + + ;
wdir = path ;
wuname = user ;
2011-12-27 11:18:46 +08:00
if ( ( s = socket ( AF_UNIX , SOCK_STREAM , 0 ) ) = = - 1 )
2005-09-20 21:26:39 +08:00
{
wperror ( L " socket " ) ;
return - 1 ;
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( wdir )
dir = wcs2str ( wdir ) ;
else
dir = strdup ( " /tmp " ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( wuname )
uname = wcs2str ( wuname ) ;
else
{
struct passwd * pw ;
pw = getpwuid ( getuid ( ) ) ;
uname = strdup ( pw - > pw_name ) ;
}
2011-12-27 11:18:46 +08:00
name = ( char * ) malloc ( strlen ( dir ) +
strlen ( uname ) +
strlen ( SOCK_FILENAME ) +
2005-09-20 21:26:39 +08:00
2 ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
strcpy ( name , dir ) ;
strcat ( name , " / " ) ;
strcat ( name , SOCK_FILENAME ) ;
strcat ( name , uname ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
free ( dir ) ;
free ( uname ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
debug ( 3 , L " Connect to socket %s at fd %2 " , name , s ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
local . sun_family = AF_UNIX ;
strcpy ( local . sun_path , name ) ;
free ( name ) ;
2006-01-09 22:44:18 +08:00
len = sizeof ( local ) ;
2011-12-27 11:18:46 +08:00
if ( connect ( s , ( struct sockaddr * ) & local , len ) = = - 1 )
2005-09-20 21:26:39 +08:00
{
close ( s ) ;
2005-11-28 07:22:08 +08:00
if ( fork_ok & & start_fishd )
2005-09-20 21:26:39 +08:00
{
debug ( 2 , L " Could not connect to socket %d, starting fishd " , s ) ;
2011-12-27 11:18:46 +08:00
2005-11-28 07:22:08 +08:00
start_fishd ( ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
return get_socket ( 0 ) ;
}
2011-12-27 11:18:46 +08:00
2006-11-15 20:34:47 +08:00
debug ( 1 , L " Could not connect to universal variable server, already tried manual restart (or no command supplied). You will not be able to share variable values between fish sessions. Is fish properly installed? " ) ;
2005-09-20 21:26:39 +08:00
return - 1 ;
}
2011-12-27 11:18:46 +08:00
if ( ( fcntl ( s , F_SETFL , O_NONBLOCK ) ! = 0 ) | | ( fcntl ( s , F_SETFD , FD_CLOEXEC ) ! = 0 ) )
2005-09-20 21:26:39 +08:00
{
wperror ( L " fcntl " ) ;
2011-12-27 11:18:46 +08:00
close ( s ) ;
2005-09-20 21:26:39 +08:00
return - 1 ;
}
2006-11-15 20:34:47 +08:00
2005-09-20 21:26:39 +08:00
debug ( 3 , L " Connected to fd %d " , s ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
return s ;
}
2005-10-03 21:24:46 +08:00
/**
Callback function used whenever a new fishd message is recieved
*/
2005-09-20 21:26:39 +08:00
static void callback ( int type , const wchar_t * name , const wchar_t * val )
2011-12-27 11:18:46 +08:00
{
2005-09-20 21:26:39 +08:00
if ( type = = BARRIER_REPLY )
{
barrier_reply = 1 ;
}
2005-09-23 04:16:52 +08:00
else
{
2005-10-12 03:23:43 +08:00
if ( external_callback )
2011-12-27 11:18:46 +08:00
external_callback ( type , name , val ) ;
}
2005-09-23 04:16:52 +08:00
}
2005-10-03 21:24:46 +08:00
/**
Make sure the connection is healthy . If not , close it , and try to
establish a new connection .
*/
2005-09-23 04:16:52 +08:00
static void check_connection ( )
{
if ( ! init )
return ;
2011-12-27 11:18:46 +08:00
2005-09-23 04:16:52 +08:00
if ( env_universal_server . killme )
{
2005-11-29 18:13:03 +08:00
debug ( 3 , L " Lost connection to universal variable server. " ) ;
2011-12-27 11:18:46 +08:00
2006-06-21 17:54:30 +08:00
if ( close ( env_universal_server . fd ) )
{
wperror ( L " close " ) ;
}
2011-12-27 11:18:46 +08:00
2005-09-23 04:16:52 +08:00
env_universal_server . fd = - 1 ;
env_universal_server . killme = 0 ;
2011-12-27 11:18:46 +08:00
env_universal_server . input . used = 0 ;
2005-09-23 04:16:52 +08:00
env_universal_read_all ( ) ;
2011-12-27 11:18:46 +08:00
}
2005-09-20 21:26:39 +08:00
}
2006-11-15 20:34:47 +08:00
/**
Remove all universal variables .
*/
static void env_universal_remove_all ( )
{
2011-12-28 10:41:38 +08:00
size_t i ;
2011-12-27 11:18:46 +08:00
2011-12-28 10:41:38 +08:00
wcstring_list_t lst ;
env_universal_common_get_names2 ( lst ,
2006-11-15 20:34:47 +08:00
1 ,
1 ) ;
2011-12-28 10:41:38 +08:00
for ( i = 0 ; i < lst . size ( ) ; i + + )
2006-11-15 20:34:47 +08:00
{
2011-12-28 10:41:38 +08:00
const wcstring & key = lst . at ( i ) ;
2012-02-18 02:52:30 +08:00
env_universal_common_remove ( key ) ;
2006-11-15 20:34:47 +08:00
}
2011-12-27 11:18:46 +08:00
2006-11-15 20:34:47 +08:00
}
2005-10-03 21:24:46 +08:00
/**
Try to establish a new connection to fishd . If successfull , end
with call to env_universal_barrier ( ) , to make sure everything is in
sync .
*/
2005-10-03 21:09:37 +08:00
static void reconnect ( )
{
if ( get_socket_count > = RECONNECT_COUNT )
2005-10-03 21:24:46 +08:00
return ;
2011-12-27 11:18:46 +08:00
2005-11-29 18:13:03 +08:00
debug ( 3 , L " Get new fishd connection " ) ;
2011-12-27 11:18:46 +08:00
2005-10-03 21:09:37 +08:00
init = 0 ;
2006-10-11 06:21:13 +08:00
env_universal_server . buffer_consumed = env_universal_server . buffer_used = 0 ;
2005-10-03 21:09:37 +08:00
env_universal_server . fd = get_socket ( 1 ) ;
init = 1 ;
if ( env_universal_server . fd > = 0 )
{
2006-11-15 20:34:47 +08:00
env_universal_remove_all ( ) ;
2005-10-03 21:09:37 +08:00
env_universal_barrier ( ) ;
}
}
2005-09-20 21:26:39 +08:00
2005-09-23 04:16:52 +08:00
2011-12-27 11:18:46 +08:00
void env_universal_init ( wchar_t * p ,
wchar_t * u ,
2005-10-12 03:23:43 +08:00
void ( * sf ) ( ) ,
void ( * cb ) ( int type , const wchar_t * name , const wchar_t * val ) )
2005-09-20 21:26:39 +08:00
{
path = p ;
user = u ;
2011-12-27 11:18:46 +08:00
start_fishd = sf ;
2005-10-12 03:23:43 +08:00
external_callback = cb ;
2006-10-11 06:21:13 +08:00
2006-10-19 00:44:38 +08:00
connection_init ( & env_universal_server , - 1 ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
env_universal_server . fd = get_socket ( 1 ) ;
env_universal_common_init ( & callback ) ;
2011-12-27 11:18:46 +08:00
env_universal_read_all ( ) ;
init = 1 ;
2005-09-20 21:26:39 +08:00
if ( env_universal_server . fd > = 0 )
{
2005-09-23 04:16:52 +08:00
env_universal_barrier ( ) ;
2005-09-20 21:26:39 +08:00
}
}
void env_universal_destroy ( )
{
/*
Go into blocking mode and send all data before exiting
*/
if ( env_universal_server . fd > = 0 )
{
if ( fcntl ( env_universal_server . fd , F_SETFL , 0 ) ! = 0 )
{
wperror ( L " fcntl " ) ;
}
2011-12-27 11:18:46 +08:00
try_send_all ( & env_universal_server ) ;
2006-06-21 17:54:30 +08:00
}
2006-10-19 00:44:38 +08:00
connection_destroy ( & env_universal_server ) ;
2005-09-20 21:26:39 +08:00
env_universal_server . fd = - 1 ;
env_universal_common_destroy ( ) ;
init = 0 ;
}
/**
Read all available messages from the server .
*/
int env_universal_read_all ( )
{
if ( ! init )
return 0 ;
if ( env_universal_server . fd = = - 1 )
{
2011-12-27 11:18:46 +08:00
reconnect ( ) ;
2005-10-03 21:24:46 +08:00
if ( env_universal_server . fd = = - 1 )
2011-12-27 11:18:46 +08:00
return 0 ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( env_universal_server . fd ! = - 1 )
{
read_message ( & env_universal_server ) ;
2011-12-27 11:18:46 +08:00
check_connection ( ) ;
2005-09-20 21:26:39 +08:00
return 1 ;
}
else
{
debug ( 2 , L " No connection to universal variable server " ) ;
return 0 ;
2011-12-27 11:18:46 +08:00
}
2005-09-20 21:26:39 +08:00
}
wchar_t * env_universal_get ( const wchar_t * name )
{
if ( ! init )
return 0 ;
2005-09-24 07:15:38 +08:00
2006-06-21 18:07:46 +08:00
CHECK ( name , 0 ) ;
2011-12-27 11:18:46 +08:00
2005-09-23 04:16:52 +08:00
return env_universal_common_get ( name ) ;
2005-09-20 21:26:39 +08:00
}
2005-09-23 04:16:52 +08:00
int env_universal_get_export ( const wchar_t * name )
{
2006-06-21 18:07:46 +08:00
if ( ! init )
return 0 ;
CHECK ( name , 0 ) ;
2011-12-27 11:18:46 +08:00
2005-09-23 04:16:52 +08:00
return env_universal_common_get_export ( name ) ;
}
void env_universal_barrier ( )
2005-09-20 21:26:39 +08:00
{
message_t * msg ;
fd_set fds ;
2005-09-23 04:16:52 +08:00
2006-11-15 20:34:47 +08:00
if ( ! init | | is_dead ( ) )
2005-09-23 04:16:52 +08:00
return ;
2005-11-28 07:22:08 +08:00
2005-09-20 21:26:39 +08:00
barrier_reply = 0 ;
2005-09-23 04:16:52 +08:00
/*
Create barrier request
*/
2005-09-20 21:26:39 +08:00
msg = create_message ( BARRIER , 0 , 0 ) ;
msg - > count = 1 ;
2011-12-27 13:39:08 +08:00
env_universal_server . unsent - > push ( msg ) ;
2005-09-20 21:26:39 +08:00
2005-09-23 04:16:52 +08:00
/*
Wait until barrier request has been sent
*/
2005-09-20 21:26:39 +08:00
debug ( 3 , L " Create barrier " ) ;
while ( 1 )
{
2011-12-27 11:18:46 +08:00
try_send_all ( & env_universal_server ) ;
check_connection ( ) ;
2011-12-27 13:39:08 +08:00
if ( env_universal_server . unsent - > empty ( ) )
2005-09-20 21:26:39 +08:00
break ;
2011-12-27 11:18:46 +08:00
2005-10-03 21:09:37 +08:00
if ( env_universal_server . fd = = - 1 )
{
reconnect ( ) ;
2005-11-28 07:22:08 +08:00
debug ( 2 , L " barrier interrupted, exiting " ) ;
2011-12-27 11:18:46 +08:00
return ;
2005-10-03 21:09:37 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
FD_ZERO ( & fds ) ;
FD_SET ( env_universal_server . fd , & fds ) ;
select ( env_universal_server . fd + 1 , 0 , & fds , 0 , 0 ) ;
}
2006-10-11 06:21:13 +08:00
2005-09-23 04:16:52 +08:00
/*
Wait for barrier reply
*/
2005-09-20 21:26:39 +08:00
debug ( 3 , L " Sent barrier request " ) ;
while ( ! barrier_reply )
{
2005-10-03 21:09:37 +08:00
if ( env_universal_server . fd = = - 1 )
{
reconnect ( ) ;
2005-11-28 07:22:08 +08:00
debug ( 2 , L " barrier interrupted, exiting (2) " ) ;
2011-12-27 11:18:46 +08:00
return ;
}
2005-09-20 21:26:39 +08:00
FD_ZERO ( & fds ) ;
2011-12-27 11:18:46 +08:00
FD_SET ( env_universal_server . fd , & fds ) ;
2005-09-20 21:26:39 +08:00
select ( env_universal_server . fd + 1 , & fds , 0 , 0 , 0 ) ;
env_universal_read_all ( ) ;
}
debug ( 3 , L " End barrier " ) ;
}
2011-12-27 11:18:46 +08:00
void env_universal_set ( const wchar_t * name , const wchar_t * value , int exportv )
2005-09-20 21:26:39 +08:00
{
message_t * msg ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( ! init )
return ;
2006-06-21 18:07:46 +08:00
CHECK ( name , ) ;
2011-12-27 11:18:46 +08:00
2005-11-28 07:22:08 +08:00
debug ( 3 , L " env_universal_set( \" %ls \" , \" %ls \" ) " , name , value ) ;
2005-09-20 21:26:39 +08:00
2006-11-15 20:34:47 +08:00
if ( is_dead ( ) )
2005-09-23 04:16:52 +08:00
{
2011-12-27 11:18:46 +08:00
env_universal_common_set ( name , value , exportv ) ;
2006-11-15 20:34:47 +08:00
}
else
{
2011-12-27 11:18:46 +08:00
msg = create_message ( exportv ? SET_EXPORT : SET ,
name ,
2006-11-15 20:34:47 +08:00
value ) ;
if ( ! msg )
{
debug ( 1 , L " Could not create universal variable message " ) ;
return ;
}
2011-12-27 11:18:46 +08:00
2006-11-15 20:34:47 +08:00
msg - > count = 1 ;
2011-12-27 13:39:08 +08:00
env_universal_server . unsent - > push ( msg ) ;
2006-11-15 20:34:47 +08:00
env_universal_barrier ( ) ;
2005-09-23 04:16:52 +08:00
}
2005-09-20 21:26:39 +08:00
}
2006-06-05 04:14:51 +08:00
int env_universal_remove ( const wchar_t * name )
2005-09-20 21:26:39 +08:00
{
2006-06-05 04:14:51 +08:00
int res ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
message_t * msg ;
if ( ! init )
2006-06-05 04:14:51 +08:00
return 1 ;
2011-12-27 11:18:46 +08:00
2006-06-21 18:07:46 +08:00
CHECK ( name , 1 ) ;
2006-06-05 04:14:51 +08:00
res = ! env_universal_common_get ( name ) ;
2005-11-28 07:22:08 +08:00
debug ( 3 ,
L " env_universal_remove( \" %ls \" ) " ,
2005-09-20 21:26:39 +08:00
name ) ;
2011-12-27 11:18:46 +08:00
2006-11-15 20:34:47 +08:00
if ( is_dead ( ) )
{
2012-02-18 02:52:30 +08:00
env_universal_common_remove ( wcstring ( name ) ) ;
2006-11-15 20:34:47 +08:00
}
else
{
msg = create_message ( ERASE , name , 0 ) ;
msg - > count = 1 ;
2011-12-27 13:39:08 +08:00
env_universal_server . unsent - > push ( msg ) ;
2006-11-15 20:34:47 +08:00
env_universal_barrier ( ) ;
}
2011-12-27 11:18:46 +08:00
2006-06-05 04:14:51 +08:00
return res ;
2005-09-23 04:16:52 +08:00
}
2011-12-28 10:41:38 +08:00
void env_universal_get_names2 ( wcstring_list_t & lst ,
int show_exported ,
int show_unexported )
{
if ( ! init )
return ;
env_universal_common_get_names2 ( lst ,
show_exported ,
show_unexported ) ;
}