Esempio n. 1
0
int iguana_deinit()
{
	int retval = 1;

	/* close the connection to the iguana daemon */
	if (sendConn != -1)
	{
		iguanaClose(sendConn);
		sendConn = -1;
	}

	/* signal the child process to exit */
	if (child > 0 && (kill(child, SIGTERM) == -1 ||
			  waitpid(child, NULL, 0) != 0))
	{
		retval = 0;
		child = 0;
	}

	return retval;
}
Esempio n. 2
0
static int iguana_deinit()
{
	/* close the connection to the iguana daemon */
	if (sendConn != -1)
	{
		iguanaClose(sendConn);
		sendConn = -1;
	}

	/* signal the child process to exit */
	if (child > 0 && (kill(child, SIGTERM) == -1 ||
			  dowaitpid(child, NULL, 0) != (pid_t) -1))
	{
		child = 0;
	}

	/* close hw.fd since otherwise we leak open files */
	close(hw.fd);
        hw.fd = -1;

	return child == 0;
}
Esempio n. 3
0
static void recv_loop(int fd, int notify)
{
	int conn;

	alarm(0);
	signal(SIGTERM, quitHandler);
	/*    signal(SIGPIPE, SIG_DFL);*/
	signal(SIGINT, quitHandler);
	signal(SIGHUP, SIG_IGN);
	signal(SIGALRM, SIG_IGN);

	/* notify parent by closing notify */
	close(notify);

	conn = iguanaConnect(hw.device);
	if (conn != -1)
	{
		iguanaPacket request, response;
		lirc_t prevCode = -1;

		request = iguanaCreateRequest(IG_DEV_RECVON, 0, NULL);
		if (iguanaWriteRequest(request, conn))
			while(! recvDone)
			{
				/* read from device */
				do
				{
					response = iguanaReadResponse(conn, 1000);
				}
				while (!recvDone &&
					((response == NULL && errno == ETIMEDOUT)
					|| (iguanaResponseIsError(response) && errno == ETIMEDOUT)));

				if (iguanaResponseIsError(response))
				{
					/* be quiet during exit */
					if (! recvDone)
						logprintf(LOG_ERR, "error response: %s\n", strerror(errno));
					break;
				}
				else if (iguanaCode(response) == IG_DEV_RECV)
				{
					uint32_t *code;
					unsigned int length, x, y = 0;
					lirc_t buffer[8]; /* we read 8 bytes max at a time
							   * from the device, i.e. packet
							   * can only contain 8
							   * signals. */

					/* pull the data off the packet */
					code = (uint32_t*)iguanaRemoveData(response, &length);
					length /= sizeof(uint32_t);

					/* translate the code into lirc_t pulses (and make
					 * sure they don't split across iguana packets. */
					for(x = 0; x < length; x++)
					{
						if (prevCode == -1)
						{
							prevCode = (code[x] & IG_PULSE_MASK);
							if(prevCode > PULSE_MASK) prevCode = PULSE_MASK;
							if(code[x] & IG_PULSE_BIT) prevCode |= PULSE_BIT;
						}
						else if (((prevCode & PULSE_BIT) && (code[x]  & IG_PULSE_BIT)) ||
							 (!(prevCode & PULSE_BIT) && !(code[x]  & IG_PULSE_BIT)))
						{
							/* can overflow pulse mask, so just set to
							 * largest possible */
							if ((prevCode & PULSE_MASK) + (code[x] & IG_PULSE_MASK) >
							    PULSE_MASK)
								prevCode = (prevCode & PULSE_BIT) | PULSE_MASK;
							else
								prevCode += code[x] & IG_PULSE_MASK;
						}
						else
						{
							buffer[y] = prevCode;
							y++;

							prevCode = (code[x] & IG_PULSE_MASK);
							if(prevCode > PULSE_MASK) prevCode = PULSE_MASK;
							if(code[x] & IG_PULSE_BIT) prevCode |= PULSE_BIT;
						}
					}

					/* write the data and free it */
					if (y > 0)
						write(fd, buffer, sizeof(lirc_t) * y);
					free(code);
				}

				iguanaFreePacket(response);
			}

		iguanaFreePacket(request);
	}

	iguanaClose(conn);
	close(fd);
}
Esempio n. 4
0
static int startListening(const char *name)
{
    int sockfd, attempt = 0;
    struct sockaddr_un server;
    bool retry = true;

    /* generate the server address */
    server.sun_family = PF_UNIX;
    socketName(name, server.sun_path, sizeof(server.sun_path));

    while(retry)
    {
        retry = false;
        attempt++;

        sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (sockfd == -1)
            message(LOG_ERROR, "failed to create server socket.\n");
        else if (bind(sockfd, (struct sockaddr*)&server,
                      sizeof(struct sockaddr_un)) == -1)
        {
            if (errno == EADDRINUSE)
            {
                /* check that the socket has something listening */
                int testconn;
                testconn = iguanaConnect_internal(name, IG_PROTOCOL_VERSION, false);
                if (testconn == -1 && errno == ECONNREFUSED && attempt == 1)
                {
                    /* if not, try unlinking the pipe and trying again */
                    unlink(server.sun_path);
                    retry = true;
                }
                else
                {
                    /* guess someone is there, whoops, close and complain */
                    iguanaClose(testconn);
                    message(LOG_ERROR, "failed to bind server socket %s.  Is the address currently in use?\n", server.sun_path);
                }
            }
            /* attempt to make the directory if we get ENOENT */
            else if (errno == ENOENT && mkdirs(server.sun_path))
                retry = true;
            else
                message(LOG_ERROR, "failed to bind server socket: %s\n",
                        translateError(errno));
        }
        /* start listening */
        else if (listen(sockfd, 5) == -1)
            message(LOG_ERROR,
                    "failed to put server socket in a listening state.\n");
        /* set the proper permissions */
        else if (chmod(server.sun_path, devMode) != 0)
            message(LOG_ERROR,
                    "failed to set permissions on the server socket.\n");
        else
            return sockfd;
#if DEBUG
printf("CLOSE %d %s(%d)\n", sockfd, __FILE__, __LINE__);
#endif
        close(sockfd);
    }

    return INVALID_PIPE;
}