static int afp_server_reconnect_loud(struct fuse_client * c, struct afp_server * s) 
{
	char mesg[1024];
	unsigned int l = 2040;
	int rc;

	rc=afp_server_reconnect(s,mesg,&l,l);

	if (rc) 
                log_for_client((void *) c,AFPFSD,LOG_ERR,
                        "%s",mesg);
	return rc;


}
Exemple #2
0
int dsi_send(struct afp_server *server, char * msg, int size,int wait,unsigned char subcommand, void ** other) 
{
	/* For wait:
	 * -1: wait forever
	 *  0: don't wait
	 * x>n: wait for N seconds */

	struct dsi_header  *header = (struct dsi_header *) msg;
	struct dsi_request * new_request, *p;
	int rc=0;
	struct timespec ts;
	struct timeval tv;
 	header->length=htonl(size-sizeof(struct dsi_header));

	if (!server_still_valid(server) || server->fd==0)
		return -1;

	afp_wait_for_started_loop();

	/* Add request to the queue */
	if (!(new_request=malloc(sizeof(struct dsi_request)))) {
		log_for_client(NULL,AFPFSD,LOG_ERR,
			"Could not allocate for new request\n");
		return -1;
	}
	memset(new_request,0,sizeof(struct dsi_request));
	new_request->requestid=server->lastrequestid;
	new_request->subcommand=subcommand;
	new_request->other=other;
	new_request->wait=wait;
	new_request->next=NULL;

	pthread_mutex_lock(&server->request_queue_mutex);
	if (server->command_requests==NULL) {
		server->command_requests=new_request;
	} else {
		for (p=server->command_requests;p->next;p=p->next);
		p->next=new_request;
	}
	server->stats.requests_pending++;
	pthread_mutex_unlock(&server->request_queue_mutex);

	pthread_cond_init(&new_request->condition_cond,NULL);

	if (server->connect_state==SERVER_STATE_DISCONNECTED) {
		char mesg[1024];
		unsigned int l=0; 
		/* Try and reconnect */

		afp_server_reconnect(server,mesg,&l,1024);


	}

	pthread_mutex_lock(&server->send_mutex);
	#ifdef DEBUG_DSI
	printf("*** Sending %d, %s\n",ntohs(header->requestid),
		afp_get_command_name(new_request->subcommand));
	#endif
	if (write(server->fd,msg,size)<0) {
		if ((errno==EPIPE) || (errno==EBADF)) {
			/* The server has closed the connection */
			server->connect_state=SERVER_STATE_DISCONNECTED;
			return -1;

		}
		perror("writing to server");
		rc=-1;
		pthread_mutex_unlock(&server->send_mutex);
		goto out;
	}
	server->stats.tx_bytes+=size;
	pthread_mutex_unlock(&server->send_mutex);

	int tmpwait=new_request->wait;
	#ifdef DEBUG_DSI
	printf("=== Waiting for response for %d %s\n",
		new_request->requestid,
		afp_get_command_name(new_request->subcommand));
	#endif
	if (tmpwait<0) {

		pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
		pthread_mutex_lock(&mutex);

		/* Wait forever */
		#ifdef DEBUG_DSI
		printf("=== Waiting forever for %d, %s\n",
			new_request->requestid,
			afp_get_command_name(new_request->subcommand));
		#endif

		rc=pthread_cond_wait( 
			&new_request->condition_cond, 
				&mutex );
		pthread_mutex_unlock(&mutex);

	} else if (tmpwait>0) {
		pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
		pthread_mutex_lock(&mutex);

		#ifdef DEBUG_DSI
		printf("=== Waiting for %d %s, for %ds\n",
			new_request->requestid,
			afp_get_command_name(new_request->subcommand),
			new_request->wait);
		#endif

		gettimeofday(&tv,NULL);
		ts.tv_sec=tv.tv_sec;
		ts.tv_sec+=new_request->wait;
		ts.tv_nsec=tv.tv_usec *1000;
		if (new_request->wait==0) {
			#ifdef DEBUG_DSI
			printf("=== Changing my mind, no longer waiting for %d\n",
				new_request->requestid);
			#endif
			pthread_mutex_unlock(&mutex);
			goto skip;
		}
		rc=pthread_cond_timedwait( 
			&new_request->condition_cond, 
			&mutex,&ts);
		pthread_mutex_unlock(&mutex);
		if (rc==ETIMEDOUT) {
/* FIXME: should handle this case properly */
			#ifdef DEBUG_DSI
			printf("=== Timedout for %d\n",
				new_request->requestid);
			#endif
			goto out;
		}
	} else {
		#ifdef DEBUG_DSI
		printf("=== Skipping wait altogether for %d\n",new_request->requestid);
		#endif
	}
	#ifdef DEBUG_DSI
	printf("=== Done waiting for %d %s, waiting for %ds,"
		" return %d, DSI return %d\n",
		new_request->requestid,
		afp_get_command_name(new_request->subcommand),
		new_request->wait, 
		rc,new_request->return_code);
	#endif
skip:
	rc=new_request->return_code;
out:
	dsi_remove_from_request_queue(server,new_request);
	return rc;
}