示例#1
0
static unsigned int data_output_main(int sm_sock_fd, int fix_size)
{
	int sig = 0;
	int r_size;
	int w_size;
	int dev_path = PRNT_PATH;
	unsigned int total = 0; /* total data size (wrote size) */
	char *buf;
	char *ptr;

	/* read buffer get */
	buf = (char *)malloc(MAX_DATA);
	if(!buf)
		exit(0); /* no ram */

	/* print data read(from stdin) and write(to stdout) */
	while((r_size = read_data(buf)) > 0){
		ptr = buf;
		/* data print(output) loop */
		for(;r_size > 0;r_size -= w_size){
			int __attribute__ ((unused)) err;

			signal_block(sigmask);   /* signal block */
			get_printer_sem(sem_id); /* printer lock */

			w_size = p_dvacs->ptdev_write(dev_path, ptr, r_size); /* data write -> printer */
			err = errno;
#ifdef DATA_LOG
                        if(log_hand >=0 && w_size > 0)
                                write(log_hand, ptr, w_size);
#endif

			/* write() error check (power off??) */
			if(w_size <= 0){
				w_size = 0;
				if(check_power(sm_sock_fd, dev_path) == LM_PRN_POWOFF){
#ifdef DEBUG
					write_log("LM(P) into wait_restart()\n");
#endif
					release_printer_sem(sem_id);
					signal_unblock(sigmask);

					/* wait BSCC SUCCESS */
					if(wait_restart() == DETECT_END)
						goto dataout_exit;
					else
						continue;
				}
#ifdef DEBUG
				fprintf(log_path, "LM(P) write error. normal=%x\n",err);
				fflush(log_path);
#endif
				sleep(1);
			}
			else
				total += w_size; /* total write size save */

			/* write() is normal end */
			release_printer_sem(sem_id);
			signal_unblock(sigmask);

			if((sig = check_signals()) != 0){
			/* signal detect */
				if(signal_end(sig)){
					goto dataout_exit; /* SIGTERM */
					}

					/* other signal ignore */
					start_signal = 0;
					last_signal  = 0;
			}
			ptr += w_size;
		}

		/* written data size == request data size ?*/
		if(fix_size && (total >= fix_size) )
			break;
	}
dataout_exit:
	free(buf);

	return total;
}
示例#2
0
文件: grace.c 项目: v2e4lisp/sample
void loop(int port, char *cmd[]) {
    int sockfd; // socket fd
    int ready[2]; // ready pipe
    int rd; // pipe reader fd
    int wr; // pipe writer fd
    int pid; // temp child pid;
    fd_set rset; // fd set for pipe reader
    char *p; // GRACE_START_TIMEOUT
    int rt; // start timeout

    sockfd = createsock(port);
    setup_signals();

    rt = 10;
    if ((p = getenv(GRACE_START_TIMEOUT)) != NULL) {
        rt = atoi(p);
    }
    timeout.tv_sec = rt;
    timeout.tv_usec = 0;

    while(1) {
        if (pipe(ready) < 0) {
            SYSERR("pipe");
        }
        rd = ready[0];
        wr = ready[1];

        pid = fork();
        if (pid < 0) {
            SYSERR("fork");
        }

        if (pid == 0) {
            close(rd);
            close(srd);
            close(swr);
            // sigmask is preserved for child process, so we need to clear it;
            clear_sigmask();
            setenvs(sockfd, wr);
            execvp(cmd[0], cmd);
            SYSERR("execvp");
        }

        // close the write end of the pipe;
        close(wr);

        wait_child(pid, rd);

        close(rd);

        // successfully started; then kill the old one.
        if (childpid) {
            kill(childpid, SIGTERM);
        }
        childpid = pid;

        wait_restart();

        if (termniate) {
            if (childpid) {
                kill(childpid, SIGTERM);
            }
            exit(0);
        }
    }
}