2011-07-08 12:07:25 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol client.
|
|
|
|
* Virtual Channel Manager
|
|
|
|
*
|
|
|
|
* Copyright 2009-2011 Jay Sorg
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-07-08 12:07:25 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
2012-05-05 03:36:35 +04:00
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-05-05 03:36:35 +04:00
|
|
|
#include <winpr/windows.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/wait_obj.h>
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct wait_obj
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE event;
|
|
|
|
#else
|
|
|
|
int pipe_fd[2];
|
|
|
|
#endif
|
2011-12-12 18:36:11 +04:00
|
|
|
int attached;
|
2011-07-08 12:07:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wait_obj*
|
|
|
|
wait_obj_new(void)
|
|
|
|
{
|
|
|
|
struct wait_obj* obj;
|
|
|
|
|
2011-07-13 18:23:55 +04:00
|
|
|
obj = xnew(struct wait_obj);
|
2011-07-08 12:07:25 +04:00
|
|
|
|
2011-12-12 18:36:11 +04:00
|
|
|
obj->attached = 0;
|
2011-07-08 12:07:25 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
obj->event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
#else
|
|
|
|
obj->pipe_fd[0] = -1;
|
|
|
|
obj->pipe_fd[1] = -1;
|
|
|
|
if (pipe(obj->pipe_fd) < 0)
|
|
|
|
{
|
|
|
|
printf("wait_obj_new: pipe failed\n");
|
|
|
|
xfree(obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-12-12 18:36:11 +04:00
|
|
|
struct wait_obj* wait_obj_new_with_fd(void* fd)
|
|
|
|
{
|
|
|
|
struct wait_obj* obj;
|
|
|
|
|
|
|
|
obj = xnew(struct wait_obj);
|
|
|
|
|
|
|
|
obj->attached = 1;
|
|
|
|
#ifdef _WIN32
|
|
|
|
obj->event = fd;
|
|
|
|
#else
|
|
|
|
obj->pipe_fd[0] = (int)(long)fd;
|
|
|
|
obj->pipe_fd[1] = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
void
|
|
|
|
wait_obj_free(struct wait_obj* obj)
|
|
|
|
{
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
|
2011-12-12 18:36:11 +04:00
|
|
|
if (obj->attached == 0)
|
2011-07-08 12:07:25 +04:00
|
|
|
{
|
2011-12-12 18:36:11 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (obj->event)
|
|
|
|
{
|
|
|
|
CloseHandle(obj->event);
|
|
|
|
obj->event = NULL;
|
|
|
|
}
|
2011-07-08 12:07:25 +04:00
|
|
|
#else
|
2011-12-12 18:36:11 +04:00
|
|
|
if (obj->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(obj->pipe_fd[0]);
|
|
|
|
obj->pipe_fd[0] = -1;
|
|
|
|
}
|
|
|
|
if (obj->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(obj->pipe_fd[1]);
|
|
|
|
obj->pipe_fd[1] = -1;
|
|
|
|
}
|
2011-07-08 12:07:25 +04:00
|
|
|
#endif
|
2011-12-12 18:36:11 +04:00
|
|
|
}
|
2011-07-08 12:07:25 +04:00
|
|
|
|
|
|
|
xfree(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wait_obj_is_set(struct wait_obj* obj)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (WaitForSingleObject(obj->event, 0) == WAIT_OBJECT_0);
|
|
|
|
#else
|
|
|
|
fd_set rfds;
|
|
|
|
int num_set;
|
|
|
|
struct timeval time;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(obj->pipe_fd[0], &rfds);
|
|
|
|
memset(&time, 0, sizeof(time));
|
|
|
|
num_set = select(obj->pipe_fd[0] + 1, &rfds, 0, 0, &time);
|
|
|
|
return (num_set == 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wait_obj_set(struct wait_obj* obj)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetEvent(obj->event);
|
|
|
|
#else
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (wait_obj_is_set(obj))
|
|
|
|
return;
|
|
|
|
len = write(obj->pipe_fd[1], "sig", 4);
|
|
|
|
if (len != 4)
|
|
|
|
printf("wait_obj_set: error\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wait_obj_clear(struct wait_obj* obj)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2011-08-16 22:41:12 +04:00
|
|
|
ResetEvent(obj->event);
|
2011-07-08 12:07:25 +04:00
|
|
|
#else
|
|
|
|
int len;
|
|
|
|
|
|
|
|
while (wait_obj_is_set(obj))
|
|
|
|
{
|
|
|
|
len = read(obj->pipe_fd[0], &len, 4);
|
|
|
|
if (len != 4)
|
|
|
|
printf("wait_obj_clear: error\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wait_obj_select(struct wait_obj** listobj, int numobj, int timeout)
|
|
|
|
{
|
2012-08-12 09:41:38 +04:00
|
|
|
int index;
|
|
|
|
int status;
|
2011-08-16 22:41:12 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-08 12:07:25 +04:00
|
|
|
int max;
|
|
|
|
int sock;
|
|
|
|
fd_set fds;
|
2011-08-16 22:41:12 +04:00
|
|
|
struct timeval time;
|
|
|
|
struct timeval* ptime;
|
2011-07-08 12:07:25 +04:00
|
|
|
|
|
|
|
ptime = 0;
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
|
|
|
time.tv_sec = timeout / 1000;
|
|
|
|
time.tv_usec = (timeout * 1000) % 1000000;
|
|
|
|
ptime = &time;
|
|
|
|
}
|
2011-08-16 22:41:12 +04:00
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
max = 0;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
if (listobj)
|
|
|
|
{
|
|
|
|
for (index = 0; index < numobj; index++)
|
|
|
|
{
|
|
|
|
sock = listobj[index]->pipe_fd[0];
|
|
|
|
FD_SET(sock, &fds);
|
2011-08-16 22:41:12 +04:00
|
|
|
|
2011-07-08 12:07:25 +04:00
|
|
|
if (sock > max)
|
|
|
|
max = sock;
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 22:41:12 +04:00
|
|
|
status = select(max + 1, &fds, 0, 0, ptime);
|
|
|
|
#else
|
2012-08-12 09:41:38 +04:00
|
|
|
HANDLE *hnds;
|
|
|
|
|
|
|
|
hnds = (HANDLE *) xzalloc(sizeof(HANDLE) * (numobj + 1));
|
|
|
|
for (index = 0; index < numobj; index++)
|
|
|
|
{
|
|
|
|
hnds[index] = listobj[index]->event;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WaitForMultipleObjects(numobj, hnds, FALSE, timeout) == WAIT_FAILED)
|
|
|
|
status = -1;
|
|
|
|
else
|
|
|
|
status = 0;
|
2012-08-13 05:54:50 +04:00
|
|
|
xfree(hnds);
|
2011-08-16 22:41:12 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return status;
|
2011-07-08 12:07:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void wait_obj_get_fds(struct wait_obj* obj, void** fds, int* count)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
fds[*count] = (void*) obj->event;
|
|
|
|
#else
|
|
|
|
if (obj->pipe_fd[0] == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fds[*count] = (void*)(long) obj->pipe_fd[0];
|
|
|
|
#endif
|
|
|
|
(*count)++;
|
|
|
|
}
|