Exemple #1
0
static int
real_open(const char *file, int oflag, int mode)
{
	static int (*r_open)(const char *file, int oflag, ...) = NULL;
	
	if (!r_open)	{
		if ((r_open = get_libc_sym("open")) == NULL)	{
			__set_errno(ENOSYS);
			return -1;
		}
	}
	return r_open(file, oflag, mode);
}
Exemple #2
0
ArchivoRegVars::ArchivoRegVars(char *url){ //ctor
    strcpy(&filename[0],url);
    if(r_file_exists()==1){
        cout << "El archivo no existe!" << endl;
        if(r_create()==0){
            cout << "El archivo ha sido creado con exito!" << endl;
        } else {
            cout << "Error al intentar crear el archivo!" << endl;
            salir("",ERROR_DE_CREACION);
        }
    }
    if(r_open("r+b")==0){
        cout << "El archivo se ha abierto exitosamente!" << endl;
    } else {
        salir("",ERROR_DE_APERTURA);
    }
}
xdr_s_type *xdr_init_common(const char *router, int is_client)
{
    xdr_s_type *xdr = (xdr_s_type *)calloc(1, sizeof(xdr_s_type)); 

    xdr->xops = &xdr_std_xops;

    xdr->fd = r_open(router);
    if (xdr->fd < 0) {
        E("ERROR OPENING [%s]: %s\n", router, strerror(errno));
        free(xdr);
        return NULL;
    }
    xdr->is_client = is_client;

    D("OPENED [%s] fd %d\n", router, xdr->fd);
    return xdr;
}
Exemple #4
0
int main()
{
	char *cmdline, *prompt, **arglist, **cmdlist;
	char rfileName[32];
	int result, fd_out, i, fno, cmdNum, rioFlag;
	int sfd = dup(STDOUT_FILENO);
	void setup();

	prompt = DFL_PROMPT;
	setup();

	cmdlist = emalloc(BUFSIZ);
	while ((cmdline = next_cmd(prompt, stdin)) != NULL)
	{
		/* io redirection */
		rioFlag = getRFileName(cmdline, rfileName);
		if (rioFlag > 0)
			fd_out = r_open(rfileName);
		
		/* pipe test*/
		cmdNum = pipeTest(cmdline, cmdlist);
		if (cmdNum == 1)
		{
			if ((arglist = splitline(cmdline)) != NULL)
			{
				if (buildIn(arglist))
				{
					;
				} else {
					result = execute(arglist);
					freelist(arglist);
				}
			}
		} 
		else if (cmdNum == 2) 
		{
			pipeCommand(cmdlist);
		}
		//freelist(cmdlist);
		free(cmdline);
		if (rioFlag > 0)
			r_close(sfd);
	}
	return 0;
}
/*Function to Handle the file open Call
 *
 * INPUT:
 *      int conn :      Socket connection identifier
 *      char *recvBuff :Buffer containing request
 *
 * OUTPUT:
 *      int :   Success/Failure indicator.
 */
int
open_handler (int conn , char *recvBuff) {
        unsigned int mode;
        int ind = 3 , res = 0 , ret = -1 , p_size;

        char sendBuff[1024] , len[5] , data[1000] , filename[500];
        r_file *file = (r_file *) malloc (sizeof (r_file));

        memset (len , 0 , sizeof (len));
        memcpy (len , &recvBuff[ind] , 2);
        ind += 2;
        /*Name of the file to Open */
        memset (filename , 0 , sizeof (filename));
        memcpy (filename , &recvBuff[ind] , atoi (len));
        ind += atoi (len);
        /*Parse the mode of opening the file */
        memset (data , 0 , sizeof (data));
        memcpy (data , &recvBuff[ind] , 1);
        ind += 1;
        mode = atoi (data);
        res = r_open (filename , mode , &file);
        memset (sendBuff , 0 , sizeof (sendBuff));
        memset (data , 0 , sizeof (data));
        memset (len , 0 , sizeof (len));
        /*Marshal based of success or Failure */
        if (res == 0) {
                sprintf (data , "%d" , res);
                strcat (sendBuff , data);
                memcpy (sendBuff + strlen (sendBuff) , file , sizeof (r_file));
        } else {
                sprintf (data , "%d" , 1);
                strcat (sendBuff , data);
                memset (data , 0 , sizeof (data));
                /*Error code */
                sprintf (data , "%d" , res);
                strcat (sendBuff , data);
        }
        /*Sending the Buffer content to the client */
        write (conn , sendBuff , sizeof(sendBuff)-1);
        ret = 0;
out:
        return ret;
}
/*Client API function to Open the Remote File
 *
 * INPUTS:
 *      const char *filename :  Name of the file to be opened
 *      unsigned int mode :     The mode in which the file is to be opened
 *      struct r_file **file :  Structure that will hold opened file details
 * OUTPUT:
 *      int :   Success/Failure indicator
 */
int
rOpen (const char *filename , unsigned int mode , struct r_file **file) {
        int ret = -1;

        if (*file == NULL || (*file)->inode_number == 0) {
                *file = (r_file *) malloc (sizeof (r_file));
        } else {
                printf ("WARNING: A file is already open\n");
                printf ("Please close the file, before opening new one\n");
                ret = 0;
                goto out;
        }
	if (mode == 1 || mode == 0) {
		ret = r_open (filename , mode , file);
	} else {
		printf ("ERROR: Invalid File open Mode\n");
		goto out;
	}
        if (ret == 0) {
                printf ("File : %s is opened\n" , filename);
                printf ("Operation Successful\n\n");
                goto out;
        } else if (ret == not_a_file) {
                printf ("ERROR: The requested object : %s is not a file\n"
                        , filename);
        } else if (ret == file_not_found) {
                printf ("ERROR: The reqested file : %s is not found\n"
                        , filename);
        } else if (ret == file_already_open) {
		printf ("ERROR: File is in Use\nCould not Open the file\n");
	} else {
                printf ("ERROR: Could not Open the file\n%d\n" , ret);
        }
        printf ("\n=== Operation Failed ===\n\n");
        ret = -1;
out:
        if (*file != NULL && ret != 0) {
                free (*file);
                *file = NULL;
        }
        return ret;
}
int main(int argc, char *argv[])
{
    int uart_tmp_len = 0; 
    char read_buf[256];
    char write_buf[256];
    struct termios opt;
    const char *dev = UART_DEV_NAME;
    int read_time = 0;
    int one_frame_len = 0;

	int len;
	//struct sockaddr_in remote_addr; //服务器端网络地址结构体
	//char buf[BUFSIZ];  //数据传送的缓冲区
    fd_set readfd;
    int maxfd;
    struct timeval timeout = {0,0};

    struct tm timenow;  


    uart_fd = r_open(dev);
    if(uart_fd == -1)
    {
    	  perror("open serial fail\n");
    	  exit(0);
    }
    else
        printf("uart open sucessfully\n");

	//memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
	//remote_addr.sin_family=AF_INET; //设置为IP通信
	//remote_addr.sin_addr.s_addr=inet_addr("54.213.192.93");//服务器IP地址
	//remote_addr.sin_addr.s_addr=inet_addr("115.28.154.195");//服务器IP地址
	//remote_addr.sin_port=htons(8000); //服务器端口号
	
	/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
#if 0
	if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
	{
		perror("socket");
		return 1;
	}
	
	/*将套接字绑定到服务器的网络地址上*/
	if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
	{
		perror("connect");
		return 1;
	}
	printf("connected to server\n");
	//len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息
    //buf[len]='\0';
	//printf("%s",buf); //打印服务器端信息

    //signal(SIGALRM, Client2ServerFn);
    //alarm(MSG_INTERVAL);	
	/*循环的发送接收信息并打印接收信息--recv返回接收到的字节数,send返回发送的字节数*/
#endif

#if 0
	while((len=recv(client_sockfd,buf,BUFSIZ,0))>0)
	{
		/*printf("Enter string to send:");
		scanf("%s",buf);
		if(!strcmp(buf,"quit"))
			break;*/
		//len=send(client_sockfd,buf,strlen(buf),0);
		buf[len]='\0'; 
        printf("Server To Client:%s\n",buf); 
    }
#else
    signal(SIGALRM, Client2ServerFn);
    //alarm(MSG_INTERVAL);	
    while(1)
    {
        FD_ZERO(&readfd);
        //FD_SET(client_sockfd, &readfd);
        FD_SET(uart_fd, &readfd);
        //maxfd = client_sockfd > uart_fd ? client_sockfd + 1: uart_fd + 1;
        maxfd =  uart_fd + 1;
        //only care readfd, in block mode
        switch(select(maxfd, &readfd, NULL, NULL, &timeout))
        {
            case -1:
                //close(client_sockfd);
                close(uart_fd);
                printf("select return -1\n");
                exit(-1); 
                break;
            case 0:
                break;
            default:
#if 0
                if(FD_ISSET(client_sockfd, &readfd))
                {
                    len = recv(client_sockfd, buf, BUFSIZ, 0);
		            //buf[len]='\0';
                    if(len > 0)
                    {
                        printf("Server message, length is %d\n",len); 
                        if(write(uart_fd, buf, len) == len)    //send data to UART
                            printf("write to uart sucessfully!\n");
                        else
                            printf("write to uart fail!\n");
                    }
                }
#endif
                if(FD_ISSET(uart_fd, &readfd))
                {
                    while( (uart_tmp_len = read(uart_fd, read_buf + one_frame_len, sizeof(read_buf) - one_frame_len)) > 0 )
                    {
                        read_time++;
                        one_frame_len += uart_tmp_len; 
                    }
                    if(read_time)
                    {
                        printf("UART message, length is %d\n", one_frame_len); 
                        //send_msg2_server(read_buf, one_frame_len);
                        read_time = 0;
                        one_frame_len = 0;
                    } 
                }
        }
        if(AlarmStatus == ALARM_STATUS_INIT)
        {
            GetTime(&timenow);
            if(IsTimeReach(&timenow))
            {
                sendNodeReport(1);    //send on
                AlarmStatus = ALARM_STATUS_BEGIN;
                alarm(ALARM_TIME_DUR);
            }
        }
#endif
    }
	//close(client_sockfd);//关闭套接字
    close(uart_fd);

    return 0;
}
Exemple #8
0
/*
 * Run as a daemon. Based on Section 13.3 in _Advanced Programming in the
 * UNIX Environment_ by Stevens.
 *
 * Returns 0 on success and -1 on error.
 */
static int daemonize() {
  char *pid_str = NULL;
  int ret = 0, i, open_max, pid_fd = -1;

  /* Fork once to make sure we're not a process group leader. */
  switch (fork()) {
  case -1:
    log_perror(NULL, "Couldn't fork process when daemonizing");
    ret = -1;
    goto cleanup;

  case 0:
    break;

  default:
    _exit(0);
  }

  /* Become the leader of a new session and process group, and change to the
   * root directory. */
  if (setsid() == -1) {
    log_perror(NULL, "Couldn't become a session leader");
    ret = -1;
    goto cleanup;
  }

  if (chdir("/")) {
    log_perror(NULL, "Couldn't change to root directory");
    ret = -1;
    goto cleanup;
  }

  /* Fork again so that we aren't a process group leader anymore, preventing
   * us from acquiring a controlling terminal. */
  switch (fork()) {
  case -1:
    log_perror(NULL, "Couldn't fork process when daemonizing");
    ret = -1;
    goto cleanup;

  case 0:
    break;

  default:
    _exit(0);
  }

  /* Record our new server PID. */
  g_server_pid = getpid();

  /* Set a permissive file mode mask. */
  umask(0);

  /* Create a PID file, and prepare to delete it on exit. */
  if ((pid_fd = r_open(g_config.pid_file, O_WRONLY | O_CREAT | O_EXCL,
          PID_FILE_MODE)) == -1) {
    log_perror(NULL, "Couldn't create PID file for writing");
    ret = -1;
    goto cleanup;
  }

  if (asprintf(&pid_str, "%ld\n", (long) g_server_pid) < 0) {
    log_perror(NULL, "Couldn't allocate PID string");
    ret = -1;
    goto cleanup;
  }

  if (r_write(pid_fd, pid_str, strlen(pid_str)) == -1) {
    log_perror(NULL, "Couldn't write to PID file");
    ret = -1;
    goto cleanup;
  }

  atexit(delete_pid_file);

  /* Close all (possibly) open files. */
  errno = 0;
  if ((open_max = (int) sysconf(_SC_OPEN_MAX)) == -1 && errno) {
    log_perror(NULL, "Couldn't determine maximum file descriptor");
    ret = -1;
    goto cleanup;
  }

  for (i = 0; i < ((open_max != -1) ? open_max : OPEN_MAX); ++i) {
    if (r_close(i) == -1 && errno != EBADF) {
      log_perror(NULL, "Couldn't close file descriptor when daemonizing");
      ret = -1;
      goto cleanup;
    }
  }

  /* Reopen stdin, stdout, and stderr, redirecting them to /dev/null. */
  if (r_open("/dev/null", O_RDONLY) != STDIN_FILENO
      || r_open("/dev/null", O_WRONLY) != STDOUT_FILENO
      || dup(STDOUT_FILENO) != STDERR_FILENO) {
    log_perror(NULL, "Couldn't redirect standard streams to /dev/null");
    ret = -1;
    goto cleanup;
  }

cleanup:
  r_close(pid_fd);
  free(pid_str);

  if (ret == -1 && pid_fd != -1) {
    unlink(g_config.pid_file);
  }

  return ret;
}
Exemple #9
0
void _fl_open (int *w)					{ r_open ((char *)(w[0]), (int)(w[1]), (int)(w[2]), (int *)(w[3])); }
/**
 * @brief Create description for an SDP session
 *
 * @param uri URI of the resource to describe
 *
 * @return A new GString containing the complete description of the
 *         session or NULL if the resource was not found or no demuxer
 *         was found to handle it.
 */
static GString *sdp_session_descr(RTSP_Client *rtsp, RFC822_Request *req)
{
    URI *uri = req->uri;
    GString *descr = NULL;
    double duration;

    float currtime_float, restime_float;

    Resource *resource;

    char *path;

    const char *inet_family;

    if ( rtsp->peer_sa == NULL ) {
        xlog(LOG_ERR, "unable to identify address family for connection");
        return NULL;
    }

    inet_family = rtsp->peer_sa->sa_family == AF_INET6 ? "IP6" : "IP4";
    path = g_uri_unescape_string(uri->path, "/");

    xlog(LOG_DBG, "[SDP] opening %s", path);
    if ( !(resource = r_open(path)) ) {
        xlog(LOG_ERR, "[SDP] %s not found", path);
        g_free(path);
        return NULL;
    }
    g_free(path);

    descr = g_string_new("v=0"SDP_EL);

    /* Near enough approximation to run it now */
    currtime_float = NTP_time(time(NULL));
    restime_float = resource->mtime ? NTP_time(resource->mtime) : currtime_float;

    /* Network type: Internet; Address type: IP4. */
    g_string_append_printf(descr, "o=- %.0f %.0f IN %s %s"SDP_EL,
                           currtime_float, restime_float,
                           inet_family,
                           uri->host);

    /* We might want to provide a better name */
    g_string_append(descr, "s=RTSP Session\r\n");

    g_string_append_printf(descr,
                           "c=IN %s %s"SDP_EL,
                           inet_family,
                           rtsp->local_host);

    g_string_append(descr, "t=0 0"SDP_EL);

    // type attribute. We offer only broadcast
    g_string_append(descr, "a=type:broadcast"SDP_EL);

    /* Server signature; the same as the Server: header */
    g_string_append_printf(descr, "a=tool:%s"SDP_EL,
                           feng_signature);

    // control attribute. We should look if aggregate metod is supported?
    g_string_append(descr, "a=control:*"SDP_EL);

    if ((duration = resource->duration) > 0 &&
        duration != HUGE_VAL)
        g_string_append_printf(descr, "a=range:npt=0-%f"SDP_EL, duration);

    g_list_foreach(resource->tracks,
                   sdp_track_descr,
                   descr);

    r_close(resource);

    xlog(LOG_INF, "[SDP] description:\n%s", descr->str);

    return descr;
}