示例#1
0
文件: plain-file.cpp 项目: hmic/hhvm
bool PlainFile::truncate(int64_t size) {
  assert(valid());
  return ftruncate(getFd(), size) == 0;
}
示例#2
0
int main( int argc, char *argv[] )
{
    const char *fn = "main()";
    char f_randfile[ PATH_MAX ];
    int listensd;                      /* socket descriptor we'll bind to */
    int clientsd;                      /* incoming socket descriptor */
    int sockaddrlen;                       
    struct sockaddr_storage srvaddr;
    struct sockaddr_storage cliaddr;
    pthread_t ThreadId;                /* thread id of each incoming conn */
    pthread_t RecycleThread;           /* used just for the recycle thread */
    pthread_attr_t attr;               /* generic thread attribute struct */
    int rc, i, fd;
    unsigned int ui;
    struct linger lingerstruct;        /* for the socket reuse stuff */
    int flag;                          /* for the socket reuse stuff */
    ICC_Struct *ICC_tptr;             
    extern char *optarg;
    extern int optind;
    char ConfigFile[ MAXPATHLEN ];     /* path to our config file */
    char PidFile[ MAXPATHLEN ];		/* path to our pidfile */
#ifdef HAVE_LIBWRAP
    struct request_info r;             /* request struct for libwrap */
#endif
    struct addrinfo aihints, *ai;
    int gaierrnum;

    flag = 1;
    ConfigFile[0] = '\0';
    strncpy( PidFile, DEFAULT_PID_FILE, sizeof PidFile -1 );

    /*
     * Ignore signals we don't want to die from but we don't care enough
     * about to catch.
     */
    signal( SIGPIPE, SIG_IGN );
    signal( SIGHUP, SIG_IGN );
    

    while (( i = getopt( argc, argv, "f:p:h" ) ) != EOF )
    {
	switch( i )
	{
	case 'f':
	    /* user specified a config filename */
	    strncpy( ConfigFile, optarg, sizeof ConfigFile -1 );
	    ConfigFile[ sizeof ConfigFile - 1 ] = '\0';
	    syslog( LOG_INFO, "%s: Using configuration file '%s'",
		    fn, ConfigFile );
	    break;
        
        case 'p':
            /* user specified a pidfile */
            strncpy( PidFile, optarg, sizeof PidFile -1 );
            PidFile[ sizeof PidFile - 1 ] = '\0';
            syslog( LOG_INFO, "%s: Using pidfile '%s'",
            fn, PidFile );
            break;
        
	case 'h':
	    Usage();
	    exit( 0 );

	case '?':
	    Usage();
	    exit( 1 );
	}
    }


    /* 
     * Make sure we know which config file to use and then set our config
     * options.
     */
    if ( ! ConfigFile[0] )
    {
	strncpy( ConfigFile, DEFAULT_CONFIG_FILE, sizeof ConfigFile -1 );
	ConfigFile[ sizeof ConfigFile - 1 ] = '\0';
	syslog( LOG_INFO, "%s: Using default configuration file '%s'.",
		fn, ConfigFile );
    }

    SetDefaultConfigValues(&PC_Struct);
    SetConfigOptions( ConfigFile );
    SetLogOptions();

    /*
     * Just for logging purposes, are we doing SELECT caching or not?
     */
    if ( PC_Struct.enable_select_cache )
	syslog( LOG_INFO, "%s: SELECT caching is enabled", fn );
    else
	syslog( LOG_INFO, "%s: SELECT caching is disabled", fn );
	
    /*
     * Just for logging purposes, are the admin commands enabled or not?
     */
     if ( PC_Struct.enable_admin_commands )
	 syslog( LOG_INFO, "%s: Internal admin commands are enabled", fn );
     else
	 syslog( LOG_INFO, "%s: Internal admin commands are disabled", fn );
     

#ifdef HAVE_LIBWRAP
    /*
     * Set our tcpd service name
     */
    if (service = strrchr(argv[0], '/'))
	    service++;
    else
	    service = argv[0];
#endif

    /*
     * Initialize some stuff.
     */
    rc = pthread_mutex_init(&mp, NULL);
    if ( rc )
    {
	syslog(LOG_ERR, "%s: pthread_mutex_init() returned error [%d] initializing main mutex.  Exiting.", fn, rc );
	exit( 1 );
    }

    rc = pthread_mutex_init(&trace, NULL);
    if ( rc )
    {
	syslog(LOG_ERR, "%s: pthread_mutex_init() returned error [%d] initializing trace mutex.  Exiting.", fn, rc );
	exit( 1 );
    }

    TraceUser[0] = '\0';
    
    syslog( LOG_INFO, "%s: Allocating %d IMAP connection structures.", 
	    fn, PC_Struct.cache_size );

    ICC_free = (ICC_Struct *)malloc( ( sizeof ( ICC_Struct ) ) 
		       * PC_Struct.cache_size );
    
    if ( ! ICC_free )
    {
	syslog(LOG_ERR, "%s: malloc() failed to allocate [%d] IMAPConnectionContext structures: %s", fn, PC_Struct.cache_size, strerror( errno ) );
	exit( 1 );
    }
    
    memset( ICC_free, 0, sizeof ( ICC_Struct ) * PC_Struct.cache_size );
    
    ICC_tptr = ICC_free;

    /*
     * Bug fixed by Gary Mills <mills@cc.UManitoba.CA>.  I was pre-incrementing
     * ICC_tptr and then assigning.  I guess gcc evaluates the expression
     * incorrectly, since I never had a problem with this.  Gary had the
     * problem with cc, so it's fixed here.
     */
    for ( ui = 0; ui < PC_Struct.cache_size - 1; ui++ )
    {
	ICC_tptr->next = ICC_tptr + 1;
	ICC_tptr++;
    }
    
    memset( ICC_HashTable, 0, sizeof ICC_HashTable );


#if HAVE_LIBSSL
    /* Initialize SSL_CTX */
    syslog( LOG_INFO, "%s: Enabling openssl library.", fn );
    SSL_library_init();

    /* Set up OpenSSL thread protection */
    ssl_thread_setup(fn);

    /* Need to seed PRNG, too! */
    if ( RAND_egd( ( RAND_file_name( f_randfile, sizeof( f_randfile ) ) == f_randfile ) ? f_randfile : "/.rnd" ) )
    {
	/* Not an EGD, so read and write it. */
	if ( RAND_load_file( f_randfile, -1 ) )
	RAND_write_file( f_randfile );
    }

    SSL_load_error_strings();
    tls_ctx = SSL_CTX_new( TLSv1_client_method() );
    if ( tls_ctx == NULL )
    { 
	syslog(LOG_ERR, "%s: Failed to create new SSL_CTX.  Exiting.", fn);
	exit( 1 );
    }
 
    /* Work around all known bugs */
    SSL_CTX_set_options( tls_ctx, SSL_OP_ALL );
 
    if ( PC_Struct.tls_ca_file != NULL || PC_Struct.tls_ca_path != NULL )
    {
	rc = SSL_CTX_load_verify_locations( tls_ctx,
					    PC_Struct.tls_ca_file,
					    PC_Struct.tls_ca_path );
    }
    else
    {
	rc = SSL_CTX_set_default_verify_paths( tls_ctx );
    }
    if ( rc == 0 )
    { 
	syslog(LOG_ERR, "%s: Failed to load CA data.  Exiting.", fn);
	exit( 1 );
    }
 
    if ( ! set_cert_stuff( tls_ctx,
			    PC_Struct.tls_cert_file,
			    PC_Struct.tls_key_file ) )
    { 
	syslog(LOG_ERR, "%s: Failed to load cert/key data.  Exiting.", fn);
	exit( 1 );
    }

    SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_NONE, verify_callback);
#endif /* HAVE_LIBSSL */


    ServerInit();
    
    /* Daemonize() would go here */

    SetBannerAndCapability();
    

    /*
     * We don't need to check PC_Struct.support_starttls since we
     * probably have refetched the capability list after a STARTTLS
     * if we did one; it won't ever be supported at this point.
     *
     * It also makes no difference to check PC_Struct.force_tls now
     * because we've either done a STARTTLS or we haven't - all that
     * matters is if we got LOGINDISABLED or not.
     *
     * Note that all these things *ARE* tested when checking the
     * server capabilities (in fact, the following check is probably
     * a duplicate).
     */
    if ( PC_Struct.login_disabled )
    {
	/* We're screwed!  We can't login */
	syslog(LOG_ERR,
		"%s: IMAP server has LOGINDISABLED.  Exiting.",
		fn);
	exit( 1 );
    }


    memset( &aihints, 0, sizeof aihints );
    aihints.ai_family = AF_UNSPEC;
    aihints.ai_socktype = SOCK_STREAM;
    aihints.ai_flags = AI_PASSIVE;

    if ( ( gaierrnum = getaddrinfo( PC_Struct.listen_addr,
				    PC_Struct.listen_port,
				    &aihints, &ai ) ) )
	{
	    syslog( LOG_ERR, "%s: bad bind address: '%s' specified in config file.  Exiting.", fn, PC_Struct.listen_addr );
	    exit( 1 );
	}

    syslog( LOG_INFO, "%s: Binding to tcp %s:%s", fn,
	    PC_Struct.listen_addr ? PC_Struct.listen_addr : "*",
	    PC_Struct.listen_port );

    for ( ; ai != NULL; ai = ai->ai_next )
    {
	listensd = socket( ai->ai_family, ai->ai_socktype, ai->ai_protocol );
    if ( listensd == -1 )
    {
	    syslog(LOG_WARNING, "%s: socket() failed: %s", fn, strerror(errno));
	    continue;
    }

    setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void *)&flag, 
	       sizeof(flag));
    lingerstruct.l_onoff = 1;
    lingerstruct.l_linger = 5;
    setsockopt(listensd, SOL_SOCKET, SO_LINGER, (void *)&lingerstruct, 
	       sizeof(lingerstruct));

    if ( PC_Struct.send_tcp_keepalives )
    {
	lingerstruct.l_onoff = 1;
	syslog( LOG_INFO, "%s: Enabling SO_KEEPALIVE.", fn );
	setsockopt( listensd, SOL_SOCKET, SO_KEEPALIVE, (void *)&lingerstruct.l_onoff, sizeof lingerstruct.l_onoff );
    }

	memcpy( &srvaddr, ai->ai_addr, ai->ai_addrlen );
	if ( bind( listensd, (struct sockaddr *)&srvaddr, ai->ai_addrlen ) < 0 )
    {
	    syslog(LOG_WARNING, "%s: bind() failed: %s", fn, strerror(errno) );
	    continue;
	}
	else break;
    }
    if ( ai == NULL )
    {
	syslog( LOG_ERR, "%s: no useable addresses to bind to", fn );
	exit( EXIT_FAILURE);
    }

    /*
     * Create and mmap() our stat file while we're still root.  Since it's
     * configurable, we want to make sure we do this as root so there's the
     * greatest possibility that we'll have permission to write where we
     * need to.
     */
    syslog( LOG_INFO, "%s: Using global statistics file '%s'", fn,
	    PC_Struct.stat_filename );
    
    fd = open( PC_Struct.stat_filename, O_RDWR | O_CREAT, S_IREAD | S_IWRITE );
    if ( fd == -1 )
    {
	syslog(LOG_ERR, "%s: open() failed for '%s': %s -- Exiting.", fn, 
	       PC_Struct.stat_filename, strerror( errno ) );
	exit( 1 );
    }
    
    if ( ( ftruncate( fd, sizeof( IMAPCounter_Struct ) ) ) == -1 )
    {
	syslog(LOG_ERR, "%s: ftruncate() failed: %s -- Exiting.", 
	       fn, strerror( errno ) );
	exit( 1 );
    }
    
    IMAPCount = ( IMAPCounter_Struct *)mmap( 0, sizeof( IMAPCounter_Struct ), 
		    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
    
    if ( IMAPCount == MAP_FAILED )
    {
	syslog(LOG_ERR, "%s: mmap() failed: %s -- Exiting.", 
	       fn, strerror( errno ) );
	exit( 1 );
    }
    
    memset( IMAPCount, 0, sizeof( IMAPCounter_Struct ) );
    IMAPCount->StartTime = time( 0 );
    IMAPCount->CountTime = time( 0 );

    /*
     * Daemonize as late as possible, so that connection failures can be caught
     * and startup aborted before dettaching from parent
     */
    Daemonize( PidFile );

    if ( BecomeNonRoot() )
	exit( 1 );

    /* some misc thread setup */
    rc = pthread_attr_init( &attr );
    if ( rc )
    {
	syslog(LOG_ERR, "%s: pthread_attr_init() failed: [%d]\n", fn, rc);
	exit( 1 );
    }
    
    rc = pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
    if ( rc )
    {
	syslog(LOG_ERR, "%s: pthread_attr_setdetachstate() failed: [%d]\n", 
	       fn, rc);
	exit( 1 );
    }

    /* launch a recycle thread before we loop */
    pthread_create( &RecycleThread, &attr, (void *)ICC_Recycle_Loop, NULL );

    syslog(LOG_INFO, "%s: Launched ICC recycle thread with id %d", 
	   fn, (int)RecycleThread );

    /*
     * Now start listening and accepting connections.
     */
    if ( listen(listensd, MAX_CONN_BACKLOG) < 0)
    {
	syslog( LOG_ERR, "%s: listen() failed: %s -- Exiting", 
	       fn, strerror(errno));
	exit( 1 );
    }

    syslog( LOG_INFO, "%s: squirrelmail-imap_proxy version %s normal server startup.", fn, IMAP_PROXY_VERSION );

    /*
     * Main server loop
     */
    for ( ;; )
    {
	/*
	 * Bug fixed by Gary Mills <mills@cc.UManitoba.CA>.  I forgot
	 * to initialize sockaddrlen.
	 */
	sockaddrlen = sizeof cliaddr;
	clientsd = accept( listensd, (struct sockaddr *)&cliaddr,
			   &sockaddrlen );
	if ( clientsd == -1 )
	{
	    syslog(LOG_WARNING, "%s: accept() failed: %s -- retrying", 
		   fn, strerror(errno));
	    sleep( 1 );
	    continue;
	}

#ifdef HAVE_LIBWRAP
	request_init(&r, RQ_DAEMON, service, 0);
	request_set(&r, RQ_FILE, clientsd, 0);
	sock_host(&r);
	if (!hosts_access(&r))
	{
	    shutdown(clientsd, SHUT_RDWR);
	    close(clientsd);
	    syslog(deny_severity, "refused connection from %s", eval_client(&r));
	    continue;
	}
#endif

	IMAPCount->TotalClientConnectionsAccepted++;
	IMAPCount->CurrentClientConnections++;
	
	if ( IMAPCount->CurrentClientConnections > 
	     IMAPCount->PeakClientConnections )
	    IMAPCount->PeakClientConnections = IMAPCount->CurrentClientConnections;
	
	rc = pthread_create( &ThreadId, &attr, (void *)HandleRequest, (void *)clientsd );
	if (rc != 0) {
	    syslog(LOG_ERR, "%s: pthread_create() returned error [%d] for HandleRequest.", fn, rc );
	    close(clientsd);
	}
	
    }
}
示例#3
0
bool AutoLockFile::truncateFile(size_t Length) {
  return ftruncate(FileDescriptor, Length) == 0;
}
示例#4
0
static int vox_trunc(struct ast_filestream *fs)
{
     return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
}
示例#5
0
文件: mmap_sanity.c 项目: LLNL/lustre
/* mmap write to a file form client1 then mmap read from client2 */
static int mmap_tst6(char *mnt)
{
        char mmap_file[256], mmap_file2[256];
        char *ptr = NULL, *ptr2 = NULL;
        int fd = 0, fd2 = 0, rc = 0;

        sprintf(mmap_file, "%s/%s", mnt, "mmap_file6");
        sprintf(mmap_file2, "%s/%s", dir2, "mmap_file6");
        if (unlink(mmap_file) && errno != ENOENT) {
                perror("unlink()");
                return -errno;
        }

        fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
        if (fd < 0) {
                perror(mmap_file);
                return -errno;
        }
        if (ftruncate(fd, page_size) < 0) {
                perror("ftruncate()");
                rc = -errno;
                goto out;
        }

        fd2 = open(mmap_file2, O_RDWR, 0600);
        if (fd2 < 0) {
                perror(mmap_file2);
                rc = -errno;
                goto out;
        }

        ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
        if (ptr == MAP_FAILED) {
                perror("mmap()");
                rc = -errno;
                goto out;
        }

        ptr2 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
        if (ptr2 == MAP_FAILED) {
                perror("mmap()");
                rc = -errno;
                goto out;
        }

        rc = cancel_lru_locks("osc");
        if (rc)
                goto out;

        memcpy(ptr, "blah", strlen("blah"));
        if (strncmp(ptr, ptr2, strlen("blah"))) {
                fprintf(stderr, "client2 mmap mismatch!\n");
                rc = -EFAULT;
                goto out;
        }
        memcpy(ptr2, "foo", strlen("foo"));
        if (strncmp(ptr, ptr2, strlen("foo"))) {
                fprintf(stderr, "client1 mmap mismatch!\n");
                rc = -EFAULT;
        }
out:
        if (ptr2)
                munmap(ptr2, page_size);
        if (ptr)
                munmap(ptr, page_size);
        if (fd2 > 0)
                close(fd2);
        if (fd > 0)
                close(fd);
        unlink(mmap_file);
        return rc;
}
示例#6
0
文件: spool.c 项目: halgandd/bacula
/*
 * NB! This routine locks the device, but if committing will
 *     not unlock it. If not committing, it will be unlocked.
 */
static bool despool_data(DCR *dcr, bool commit)
{
   DEVICE *rdev;
   DCR *rdcr;
   bool ok = true;
   DEV_BLOCK *block;
   JCR *jcr = dcr->jcr;
   int stat;
   char ec1[50];

   Dmsg0(100, "Despooling data\n");
   /*
    * Commit means that the job is done, so we commit, otherwise, we
    *  are despooling because of user spool size max or some error  
    *  (e.g. filesystem full).
    */
   if (commit) {
      Jmsg(jcr, M_INFO, 0, _("Committing spooled data to Volume \"%s\". Despooling %s bytes ...\n"),
         jcr->dcr->VolumeName,
         edit_uint64_with_commas(jcr->dcr->job_spool_size, ec1));
      set_jcr_job_status(jcr, JS_DataCommitting);
   } else {
      Jmsg(jcr, M_INFO, 0, _("Writing spooled data to Volume. Despooling %s bytes ...\n"),
         edit_uint64_with_commas(jcr->dcr->job_spool_size, ec1));
      set_jcr_job_status(jcr, JS_DataDespooling);
   }
   set_jcr_job_status(jcr, JS_DataDespooling);
   dir_send_job_status(jcr);
   dcr->despool_wait = true;
   dcr->spooling = false;
   /*
    * We work with device blocked, but not locked so that
    *  other threads -- e.g. reservations can lock the device
    *  structure.
    */
   dcr->dblock(BST_DESPOOLING);
   dcr->despool_wait = false;
   dcr->despooling = true;

   /*
    * This is really quite kludgy and should be fixed some time.
    * We create a dev structure to read from the spool file
    * in rdev and rdcr.
    */
   rdev = (DEVICE *)malloc(sizeof(DEVICE));
   memset(rdev, 0, sizeof(DEVICE));
   rdev->dev_name = get_memory(strlen(spool_name)+1);
   bstrncpy(rdev->dev_name, spool_name, sizeof(rdev->dev_name));
   rdev->errmsg = get_pool_memory(PM_EMSG);
   *rdev->errmsg = 0;
   rdev->max_block_size = dcr->dev->max_block_size;
   rdev->min_block_size = dcr->dev->min_block_size;
   rdev->device = dcr->dev->device;
   rdcr = new_dcr(jcr, NULL, rdev);
   rdcr->spool_fd = dcr->spool_fd;
   block = dcr->block;                /* save block */
   dcr->block = rdcr->block;          /* make read and write block the same */

   Dmsg1(800, "read/write block size = %d\n", block->buf_len);
   lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */

#if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
   posix_fadvise(rdcr->spool_fd, 0, 0, POSIX_FADV_WILLNEED);
#endif

   /* Add run time, to get current wait time */
   int32_t despool_start = time(NULL) - jcr->run_time;

   set_new_file_parameters(dcr);

   for ( ; ok; ) {
      if (job_canceled(jcr)) {
         ok = false;
         break;
      }
      stat = read_block_from_spool_file(rdcr);
      if (stat == RB_EOT) {
         break;
      } else if (stat == RB_ERROR) {
         ok = false;
         break;
      }
      ok = write_block_to_device(dcr);
      if (!ok) {
         Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
               dcr->dev->print_name(), dcr->dev->bstrerror());
         Dmsg2(000, "Fatal append error on device %s: ERR=%s\n",
               dcr->dev->print_name(), dcr->dev->bstrerror());
      }
      Dmsg3(800, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
   }

   if (!dir_create_jobmedia_record(dcr)) {
      Jmsg2(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
         dcr->VolCatInfo.VolCatName, jcr->Job);
   }
   /* Set new file/block parameters for current dcr */
   set_new_file_parameters(dcr);

   /*
    * Subtracting run_time give us elapsed time - wait_time since 
    * we started despooling. Note, don't use time_t as it is 32 or 64
    * bits depending on the OS and doesn't edit with %d
    */
   int32_t despool_elapsed = time(NULL) - despool_start - jcr->run_time;

   if (despool_elapsed <= 0) {
      despool_elapsed = 1;
   }

   Jmsg(dcr->jcr, M_INFO, 0, _("Despooling elapsed time = %02d:%02d:%02d, Transfer rate = %s bytes/second\n"),
         despool_elapsed / 3600, despool_elapsed % 3600 / 60, despool_elapsed % 60,
         edit_uint64_with_suffix(jcr->dcr->job_spool_size / despool_elapsed, ec1));

   dcr->block = block;                /* reset block */

   lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
   if (ftruncate(rdcr->spool_fd, 0) != 0) {
      berrno be;
      Jmsg(dcr->jcr, M_ERROR, 0, _("Ftruncate spool file failed: ERR=%s\n"),
         be.bstrerror());
      /* Note, try continuing despite ftruncate problem */
   }

   P(mutex);
   if (spool_stats.data_size < dcr->job_spool_size) {
      spool_stats.data_size = 0;
   } else {
      spool_stats.data_size -= dcr->job_spool_size;
   }
   V(mutex);
   P(dcr->dev->spool_mutex);
   dcr->dev->spool_size -= dcr->job_spool_size;
   dcr->job_spool_size = 0;            /* zap size in input dcr */
   V(dcr->dev->spool_mutex);
   free_memory(rdev->dev_name);
   free_pool_memory(rdev->errmsg);
   /* Be careful to NULL the jcr and free rdev after free_dcr() */
   rdcr->jcr = NULL;
   rdcr->dev = NULL;
   free_dcr(rdcr);
   free(rdev);
   dcr->spooling = true;           /* turn on spooling again */
   dcr->despooling = false;

   /* 
    * We are done, so unblock the device, but if we have done a 
    *  commit, leave it locked so that the job cleanup does not
    *  need to wait to release the device (no re-acquire of the lock).
    */
   dcr->dlock();
   unblock_device(dcr->dev);
   /* If doing a commit, leave the device locked -- unlocked in release_device() */
   if (!commit) {
      dcr->dunlock();
   }
   set_jcr_job_status(jcr, JS_Running);
   dir_send_job_status(jcr);
   return ok;
}
示例#7
0
文件: memory.c 项目: martell/h2o
h2o_iovec_t h2o_buffer_reserve(h2o_buffer_t **_inbuf, size_t min_guarantee)
{
    h2o_buffer_t *inbuf = *_inbuf;
    h2o_iovec_t ret;

    if (inbuf->bytes == NULL) {
        h2o_buffer_prototype_t *prototype = H2O_STRUCT_FROM_MEMBER(h2o_buffer_prototype_t, _initial_buf, inbuf);
        if (min_guarantee <= prototype->_initial_buf.capacity) {
            min_guarantee = prototype->_initial_buf.capacity;
            inbuf = h2o_mem_alloc_recycle(&prototype->allocator, offsetof(h2o_buffer_t, _buf) + min_guarantee);
        } else {
            inbuf = h2o_mem_alloc(offsetof(h2o_buffer_t, _buf) + min_guarantee);
        }
        *_inbuf = inbuf;
        inbuf->size = 0;
        inbuf->bytes = inbuf->_buf;
        inbuf->capacity = min_guarantee;
        inbuf->_prototype = prototype;
        inbuf->_fd = -1;
    } else {
        if (min_guarantee <= inbuf->capacity - inbuf->size - (inbuf->bytes - inbuf->_buf)) {
            /* ok */
        } else if ((inbuf->size + min_guarantee) * 2 <= inbuf->capacity) {
            /* the capacity should be less than or equal to 2 times of: size + guarantee */
            memmove(inbuf->_buf, inbuf->bytes, inbuf->size);
            inbuf->bytes = inbuf->_buf;
        } else {
            size_t new_capacity = inbuf->capacity;
            do {
                new_capacity *= 2;
            } while (new_capacity - inbuf->size < min_guarantee);
            if (inbuf->_prototype->mmap_settings != NULL && inbuf->_prototype->mmap_settings->threshold <= new_capacity) {
                size_t new_allocsize = topagesize(new_capacity);
                int fd;
                h2o_buffer_t *newp;
                if (inbuf->_fd == -1) {
                    char *tmpfn = alloca(strlen(inbuf->_prototype->mmap_settings->fn_template) + 1);
                    strcpy(tmpfn, inbuf->_prototype->mmap_settings->fn_template);
                    if ((fd = mkstemp(tmpfn)) == -1) {
                        fprintf(stderr, "failed to create temporary file:%s:%s\n", tmpfn, strerror(errno));
                        goto MapError;
                    }
                    unlink(tmpfn);
                } else {
                    fd = inbuf->_fd;
                }
                if (ftruncate(fd, new_allocsize) != 0) {
                    perror("failed to resize temporary file");
                    goto MapError;
                }
                if ((newp = mmap(NULL, new_allocsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
                    perror("mmap failed");
                    goto MapError;
                }
                if (inbuf->_fd == -1) {
                    /* copy data (moving from malloc to mmap) */
                    newp->size = inbuf->size;
                    newp->bytes = newp->_buf;
                    newp->capacity = new_capacity;
                    newp->_prototype = inbuf->_prototype;
                    newp->_fd = fd;
                    memcpy(newp->_buf, inbuf->bytes, inbuf->size);
                    h2o_buffer__do_free(inbuf);
                    *_inbuf = inbuf = newp;
                } else {
                    /* munmap */
                    size_t offset = inbuf->bytes - inbuf->_buf;
                    munmap(inbuf, topagesize(inbuf->capacity));
                    *_inbuf = inbuf = newp;
                    inbuf->capacity = new_capacity;
                    inbuf->bytes = newp->_buf + offset;
                }
            } else {
                h2o_buffer_t *newp = h2o_mem_alloc(offsetof(h2o_buffer_t, _buf) + new_capacity);
                newp->size = inbuf->size;
                newp->bytes = newp->_buf;
                newp->capacity = new_capacity;
                newp->_prototype = inbuf->_prototype;
                newp->_fd = -1;
                memcpy(newp->_buf, inbuf->bytes, inbuf->size);
                h2o_buffer__do_free(inbuf);
                *_inbuf = inbuf = newp;
            }
        }
    }

    ret.base = inbuf->bytes + inbuf->size;
    ret.len = inbuf->_buf + inbuf->capacity - ret.base;

    return ret;

MapError:
    ret.base = NULL;
    ret.len = 0;
    return ret;
}
示例#8
0
int main(int argc, char *argv[]) {
        sd_id128_t id, id2;
        char t[33], q[37];
        _cleanup_free_ char *b = NULL;
        _cleanup_close_ int fd = -1;

        assert_se(sd_id128_randomize(&id) == 0);
        printf("random: %s\n", sd_id128_to_string(id, t));

        assert_se(sd_id128_from_string(t, &id2) == 0);
        assert_se(sd_id128_equal(id, id2));

        if (sd_booted() > 0) {
                assert_se(sd_id128_get_machine(&id) == 0);
                printf("machine: %s\n", sd_id128_to_string(id, t));

                assert_se(sd_id128_get_boot(&id) == 0);
                printf("boot: %s\n", sd_id128_to_string(id, t));
        }

        printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
        assert_se(streq(t, STR_WALDI));

        assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32);
        printf("waldi2: %s\n", b);
        assert_se(streq(t, b));

        printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
        assert_se(streq(q, UUID_WALDI));

        b = mfree(b);
        assert_se(asprintf(&b, ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36);
        printf("waldi4: %s\n", b);
        assert_se(streq(q, b));

        assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0);
        assert_se(sd_id128_equal(id, ID128_WALDI));

        assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0);
        assert_se(sd_id128_equal(id, ID128_WALDI));

        assert_se(sd_id128_from_string("", &id) < 0);
        assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0);
        assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0);
        assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
        assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);

        assert_se(id128_is_valid(STR_WALDI));
        assert_se(id128_is_valid(UUID_WALDI));
        assert_se(!id128_is_valid(""));
        assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101"));
        assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-"));
        assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10"));
        assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10"));

        fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
        assert_se(fd >= 0);

        /* First, write as UUID */
        assert_se(sd_id128_randomize(&id) >= 0);
        assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        /* Second, write as plain */
        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(ftruncate(fd, 0) >= 0);

        assert_se(sd_id128_randomize(&id) >= 0);
        assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        /* Third, write plain without trailing newline */
        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(ftruncate(fd, 0) >= 0);

        assert_se(sd_id128_randomize(&id) >= 0);
        assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        /* Third, write UUID without trailing newline */
        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(ftruncate(fd, 0) >= 0);

        assert_se(sd_id128_randomize(&id) >= 0);
        assert_se(write(fd, id128_to_uuid_string(id, t), 36) == 36);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);

        assert_se(lseek(fd, 0, SEEK_SET) == 0);
        assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
        assert_se(sd_id128_equal(id, id2));

        return 0;
}
示例#9
0
文件: io.c 项目: ericfode/radare2
R_API int r_io_resize(struct r_io_t *io, ut64 newsize) {
	if (io->plugin && io->plugin->resize)
		return io->plugin->resize (io, io->fd, newsize);
	else ftruncate (io->fd->fd, newsize);
	return R_FALSE;
}
示例#10
0
static void
extractFile(const char * path, const struct cdir_entry *entry, void * data)
{
  uint32_t size = letoh32(entry->uncompressed_size);

  struct stat status;
  if (!stat(path, &status) &&
      status.st_size == size &&
      apk_mtime < status.st_mtime)
    return;

  int fd = open(path, O_CREAT | O_NOATIME | O_TRUNC | O_RDWR, S_IRWXU);
  if (fd == -1) {
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Couldn't open %s to decompress library", path);
    return;
  }

  if (ftruncate(fd, size) == -1) {
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Couldn't ftruncate %s to decompress library", path);
    close(fd);
    return;
  }

  void * buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
                    MAP_SHARED, fd, 0);
  if (buf == (void *)-1) {
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Couldn't mmap decompression buffer");
    close(fd);
    return;
  }

  z_stream strm = {
    next_in: (Bytef *)data,
    avail_in: letoh32(entry->compressed_size),
    total_in: 0,

    next_out: (Bytef *)buf,
    avail_out: size,
    total_out: 0
  };

  int ret;
  ret = inflateInit2(&strm, -MAX_WBITS);
  if (ret != Z_OK)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflateInit failed: %s", strm.msg);

  if (inflate(&strm, Z_FINISH) != Z_STREAM_END)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflate failed: %s", strm.msg);

  if (strm.total_out != size)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "extracted %d, expected %d!", strm.total_out, size);

  ret = inflateEnd(&strm);
  if (ret != Z_OK)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflateEnd failed: %s", strm.msg);

  close(fd);
#ifdef ANDROID_ARM_LINKER
  /* We just extracted data that is going to be executed in the future.
   * We thus need to ensure Instruction and Data cache coherency. */
  cacheflush((unsigned) buf, (unsigned) buf + size, 0);
#endif
  munmap(buf, size);
}

static void
extractLib(const struct cdir_entry *entry, void * data, void * dest)
{
  z_stream strm = {
    next_in: (Bytef *)data,
    avail_in: letoh32(entry->compressed_size),
    total_in: 0,

    next_out: (Bytef *)dest,
    avail_out: letoh32(entry->uncompressed_size),
    total_out: 0
  };

  int ret;
  ret = inflateInit2(&strm, -MAX_WBITS);
  if (ret != Z_OK)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflateInit failed: %s", strm.msg);

  ret = inflate(&strm, Z_SYNC_FLUSH);
  if (ret != Z_STREAM_END)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflate failed: %s", strm.msg);

  ret = inflateEnd(&strm);
  if (ret != Z_OK)
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "inflateEnd failed: %s", strm.msg);

  if (strm.total_out != letoh32(entry->uncompressed_size))
    __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "File not fully uncompressed! %d / %d", strm.total_out, letoh32(entry->uncompressed_size));
}
示例#11
0
int db_clean()
{
  int user_fd;
  struct flock user_lock;
  int user_fd_bak;
  int poi_fd;
  int poi_fd_bak;
  struct flock poi_lock;
  char buff[sizeof(char)*10240]; // 10Kb buffer
  USER tmp_user;
  POI tmp_poi;
  int saved_users[MAX_USER_LIMIT];
  int good_user_count=0;
  int poiCount=0;
  unsigned int now;
  int i=0;
  int good=0;
  long poi_file_len=0;
  long user_file_len=0;
  size_t byte_read=0;
  size_t byte_written=0;


  now=time(NULL);
  fprintf(stderr,"\n ***** Cleaning routines started at %u. ***** \n",now);

  // Open both user and poi files
  // Acquire locks on both of them
  user_fd = open(USER_DB_PATH,O_RDWR|O_CREAT,S_IRWXU);
  if (user_fd<0)
    {
      // Error occurred
      perror("Error opening USER DB file.");
      return DB_OPEN_ERROR;
    }

  poi_fd = open(POI_DB_PATH,O_RDWR|O_CREAT,S_IRWXU);
  if (poi_fd<0)
    {
      // Error occurred
      perror("Error opening POI DB file.");
      return DB_OPEN_ERROR;
    }

  // Copy both the file
  poi_fd_bak = open(POI_DB_PATH_BAK,O_RDWR|O_CREAT,S_IRWXU);
  if (poi_fd_bak<0)
    {
      // Error occurred
      perror("Error opening POI BAK DB file.");
      return DB_OPEN_ERROR;
    }

  user_fd_bak = open(USER_DB_PATH_BAK,O_RDWR|O_CREAT,S_IRWXU);
  if (user_fd_bak<0)
    {
      // Error occurred
      perror("Error opening USER BAK DB file.");
      return DB_OPEN_ERROR;
    }

  memset (&user_lock, 0, sizeof(user_lock));
  user_lock.l_type = F_WRLCK;
  memset (&poi_lock, 0, sizeof(poi_lock));
  poi_lock.l_type = F_WRLCK;

  // Get the lock on both of them
  fcntl (user_fd, F_SETLKW, &user_lock);
  fcntl (poi_fd, F_SETLKW, &poi_lock);

  /*----- COPING USER DB FILE-----*/
  //fprintf(stderr,"Copying %s -> %s\n", USER_DB_PATH,USER_DB_PATH_BAK);
  while ((byte_read=read(user_fd,buff,sizeof(buff)))>0)
    {
      byte_written=0;
      //fprintf(stderr,"->Read buffer of %u bytes\n", byte_read);
      while(byte_written<byte_read)
        {
          i=write(user_fd_bak,buff+byte_written,byte_read-byte_written);
          if (i<1)
            {
              perror("I was unable to copty USER.DAT file");
              user_lock.l_type = F_UNLCK;
              poi_lock.l_type = F_UNLCK;
              fcntl (user_fd, F_SETLKW, &user_lock);
              fcntl (poi_fd, F_SETLKW, &poi_lock);
              close(user_fd_bak);
              close(poi_fd_bak);
              close(user_fd);
              close(poi_fd);
              unlink(USER_DB_PATH_BAK);
              unlink(POI_DB_PATH_BAK);
              return DB_ERROR;

            }
          byte_written+=i;
        }
    }

  /*----- COPING POI DB FILE-----*/
  //fprintf(stderr,"Copying %s -> %s\n", POI_DB_PATH,POI_DB_PATH_BAK);
  while ((byte_read=read(poi_fd,buff,sizeof(buff)))>0)
    {
      byte_written=0;
      //fprintf(stderr,"->Read buffer of %u bytes\n", byte_read);
      while(byte_written<byte_read)
        {
          i+=write(poi_fd_bak,buff+byte_written,byte_read-byte_written);
          if (i<1)
            {
              perror("I was unable to copty POI.DAT file");
              user_lock.l_type = F_UNLCK;
              poi_lock.l_type = F_UNLCK;
              fcntl (user_fd, F_SETLKW, &user_lock);
              fcntl (poi_fd, F_SETLKW, &poi_lock);
              close(user_fd_bak);
              close(poi_fd_bak);
              close(user_fd);
              close(poi_fd);
              unlink(USER_DB_PATH_BAK);
              unlink(POI_DB_PATH_BAK);
              return DB_ERROR;
            }
          byte_written+=i;
          //fprintf(stderr,"\n-- Written %u bytes\n", byte_written);
        }
    }
  //fprintf(stderr,"Copied %u bytes\n", byte_written);

  // Reset FD pointer
  lseek(user_fd,0,SEEK_SET);
  lseek(user_fd_bak,0,SEEK_SET);
  lseek(poi_fd,0,SEEK_SET);
  lseek(poi_fd_bak,0,SEEK_SET);

  // >>>>>>>>>>>>>>>>>>>> Users cleaning <<<<<<<<<<<<<<<<<<<<
  good_user_count=0;
  // For each line you read in users bak file, if the timestamp is older than the limit, do not copy the record in the new file. Remember the good users.
  do {
    // Read timestamp
    if (!read_uinteger(&tmp_user.ts,user_fd_bak))
      {
        // This has failed, because there are no more lines. So break
        break;
      }

    // Read the User Id
    if (!read_integer(&tmp_user.user_id,user_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The user.dat file contains an invalid row: I was unable to read user id");
        return DB_ERROR;
      }

    // Read the username length
    if (!read_integer(&tmp_user.uname_len,user_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The user.dat file contains an invalid row: I was unable to read username len");
        return DB_ERROR;
      }

    // Read the password length
    if (!read_integer(&tmp_user.pass_len,user_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The user.dat file contains an invalid row: I was unable to read password len");
        return DB_ERROR;
      }

    // Read the username
    if (!read_string(tmp_user.username,tmp_user.uname_len,user_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The user.dat file contains an invalid row: I was unable to read the first string (username).");
        return DB_ERROR;
      }

    // Read the password
    if (!read_string(tmp_user.pass,tmp_user.pass_len,user_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The user.dat file contains an invalid row: I was unable to read the first string (password).");
        return DB_ERROR;
      }

    // Check if timestamp is older than the limit
    // If yes, write it to the user_fd file directly
    if((now-tmp_user.ts)<DATA_VALIDITY_INTERVAL)
      {
        //fprintf(stderr,"\n--- Found 1 good record to preserve:\nTIMESTAMP:%u\nUSERID: %d\nLEN USER: %d\nLEN PASS: %d\nUsername: %s \n Password: %s",tmp_user.ts,tmp_user.user_id,tmp_user.uname_len,tmp_user.pass_len,tmp_user.username, tmp_user.pass);

        // Write the timestamp
        if(!write_integer(tmp_user.ts,user_fd))
          {perror("0");}
        // Write the user id
        if(!write_integer(tmp_user.user_id,user_fd))
          {perror("1");}
        // Write the length of username
        if(!write_integer(tmp_user.uname_len,user_fd))
          {perror("2");}
        // Write the length of password
        if(!write_integer(tmp_user.pass_len,user_fd))
          {perror("3");}
        // Write the username
        if(!write_string(tmp_user.username,tmp_user.uname_len,user_fd))
          {perror("4");}
        // Write the password
        if(!write_string(tmp_user.pass, tmp_user.pass_len,user_fd))
          {perror("5");}
        user_file_len+=sizeof(int)*4+tmp_user.uname_len*sizeof(char)+tmp_user.pass_len*sizeof(char);

        saved_users[good_user_count]=tmp_user.user_id;
        ++good_user_count;
      }
  }
  while(1);

  //fprintf(stderr,"\nSaved %d records to USER.DAT.",good_user_count);

  // >>>>>>>>>>>>>>>>>>>> Poi cleaning <<<<<<<<<<<<<<<<<<<<
  // Same policy for poi: delete all the POI matching a trashed user
  // Scan the poi file, save only pois matching good user ids, skip the others
  poiCount=0;
  do {
    // Read the Timsetamp
    if (!read_integer(&tmp_poi.timestamp,poi_fd_bak))
      {
        // This has failed, because there are no more lines. So break
        break;
      }

    // Read the SpotId
    if (!read_integer(&tmp_poi.spotId,poi_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The poi.dat file contains an invalid row: I was unable to read spotId integer");
        return DB_ERROR;
      }

    // Read the description length
    if (!read_integer(&tmp_poi.descriptionLen,poi_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The poi.dat file contains an invalid row: I was unable to read desc len");
        return DB_ERROR;
      }

    // Read the userId
    if (!read_integer(&tmp_poi.userId,poi_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The poi.dat file contains an invalid row: I was unable to read userid");
        return DB_ERROR;
      }

    // If the user ID is the same of the userid is marked as valid, then procced with reading other stuff. Otherwise,
    // simply seek ahead without incrementing the counter
    good=0;
    for (i=0;i<good_user_count;i++)
      {
        if (saved_users[i]==tmp_poi.userId)
          {
            // Found! it is good and must be kept
            good=1;
          }
      }
    if (good==0)
      {
        // I have to seek of sizeof(double)*2 + sizeof(char)*descrLen
        lseek(poi_fd_bak,sizeof(double)*2 + sizeof(char)*tmp_poi.descriptionLen,SEEK_CUR);
        continue;
      }

    // Read the latitude
    if (!read_double(&tmp_poi.lat,poi_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        // DB file corrupted
        perror("The poi.dat file contains an invalid row: I was unable to read the first double (latitude).");
        return DB_ERROR;
      }

    // Read the longitude
    if (!read_double(&tmp_poi.lon,poi_fd_bak))
      {
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd); // DB file corrupted
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        perror("The poi.dat file contains an invalid row: I was unable to read the second double (longitude).");
        return DB_ERROR;
      }

    // Read the description
    tmp_poi.descr[0]='\0';
    if (!read_string(tmp_poi.descr,tmp_poi.descriptionLen,poi_fd_bak))
      {
        // DB file corrupted
        perror("The poi.dat file contains an invalid row: I was unable to read the poi description.");
        // Unlock and close the file
        user_lock.l_type = F_UNLCK;
        poi_lock.l_type = F_UNLCK;
        fcntl (user_fd, F_SETLKW, &user_lock);
        fcntl (poi_fd, F_SETLKW, &poi_lock);
        close(user_fd_bak);
        close(poi_fd_bak);
        close(user_fd);
        close(poi_fd);
        unlink(USER_DB_PATH_BAK);
        unlink(POI_DB_PATH_BAK);

        return DB_ERROR;
      }

    // Write to the poi db
    // Write the timestamp
    if(!write_integer(tmp_poi.timestamp,poi_fd))
      {perror("0");}
    // Write the spotId
    if(!write_integer(tmp_poi.spotId,poi_fd))
      {perror("1");}
    // Write the length of description
    if(!write_integer(tmp_poi.descriptionLen,poi_fd))
      {perror("2");}
    // Write the user_id
    if(!write_integer(tmp_poi.userId,poi_fd))
      {perror("3");}
    // Write the latitude
    if(!write_double(tmp_poi.lat,poi_fd))
      {perror("4");}
    // Write the longitude
    if(!write_double(tmp_poi.lon, poi_fd))
      {perror("5");}
    // Write the description
    if(!write_string(tmp_poi.descr,tmp_poi.descriptionLen, poi_fd))
      {perror("5");}
    poi_file_len+=sizeof(int)*4+sizeof(double)*2+sizeof(char)*tmp_poi.descriptionLen;

    ++poiCount;
  }
  while(1);

  //fprintf(stderr,"\nSaved %d records to POI.DAT.",poiCount);

  ftruncate(poi_fd,poi_file_len);
  ftruncate(user_fd,user_file_len);

  // Release both the locks
  // close both the files
  user_lock.l_type = F_UNLCK;
  poi_lock.l_type = F_UNLCK;
  fcntl (user_fd, F_SETLKW, &user_lock);
  fcntl (poi_fd, F_SETLKW, &poi_lock);
  close(user_fd_bak);
  close(poi_fd_bak);
  close(user_fd);
  close(poi_fd);

  i = unlink(USER_DB_PATH_BAK);
  if (i!=0)
    perror("It was impossible to delete the USER.DAT.BAK file");


  i = unlink(POI_DB_PATH_BAK);
  if (i!=0)
    perror("It was impossible to delete the USER.DAT.BAK file");

  now = time(NULL);
  fprintf(stderr,"\n ***** Cleaning routines ended at %u. ***** \n",now);

  return RESULT_OK;

}
示例#12
0
/* Write the append only file buffer on disk.
 *
 * Since we are required to write the AOF before replying to the client,
 * and the only way the client socket can get a write is entering when the
 * the event loop, we accumulate all the AOF writes in a memory
 * buffer and write it on disk using this function just before entering
 * the event loop again.
 *
 * About the 'force' argument:
 *
 * When the fsync policy is set to 'everysec' we may delay the flush if there
 * is still an fsync() going on in the background thread, since for instance
 * on Linux write(2) will be blocked by the background fsync anyway.
 * When this happens we remember that there is some aof buffer to be
 * flushed ASAP, and will try to do that in the serverCron() function.
 *
 * However if force is set to 1 we'll write regardless of the background
 * fsync. */
void flushAppendOnlyFile(int force) {
    ssize_t nwritten;
    int sync_in_progress = 0;

    if (sdslen(server.aof_buf) == 0) return;

    if (server.aof_fsync == AOF_FSYNC_EVERYSEC)
        sync_in_progress = bioPendingJobsOfType(REDIS_BIO_AOF_FSYNC) != 0;

    if (server.aof_fsync == AOF_FSYNC_EVERYSEC && !force) {
        /* With this append fsync policy we do background fsyncing.
         * If the fsync is still in progress we can try to delay
         * the write for a couple of seconds. */
        if (sync_in_progress) {
            if (server.aof_flush_postponed_start == 0) {
                /* No previous write postponinig, remember that we are
                 * postponing the flush and return. */
                server.aof_flush_postponed_start = server.unixtime;
                return;
            } else if (server.unixtime - server.aof_flush_postponed_start < 2) {
                /* We were already waiting for fsync to finish, but for less
                 * than two seconds this is still ok. Postpone again. */
                return;
            }
            /* Otherwise fall trough, and go write since we can't wait
             * over two seconds. */
            server.aof_delayed_fsync++;
            redisLog(REDIS_NOTICE,"Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down Redis.");
        }
    }
    /* If you are following this code path, then we are going to write so
     * set reset the postponed flush sentinel to zero. */
    server.aof_flush_postponed_start = 0;

    /* We want to perform a single write. This should be guaranteed atomic
     * at least if the filesystem we are writing is a real physical one.
     * While this will save us against the server being killed I don't think
     * there is much to do about the whole server stopping for power problems
     * or alike */
    nwritten = write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf));
    if (nwritten != (signed)sdslen(server.aof_buf)) {
        /* Ooops, we are in troubles. The best thing to do for now is
         * aborting instead of giving the illusion that everything is
         * working as expected. */
        if (nwritten == -1) {
            redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
        } else {
            redisLog(REDIS_WARNING,"Exiting on short write while writing to "
                                   "the append-only file: %s (nwritten=%ld, "
                                   "expected=%ld)",
                                   strerror(errno),
                                   (long)nwritten,
                                   (long)sdslen(server.aof_buf));

            if (ftruncate(server.aof_fd, server.aof_current_size) == -1) {
                redisLog(REDIS_WARNING, "Could not remove short write "
                         "from the append-only file.  Redis may refuse "
                         "to load the AOF the next time it starts.  "
                         "ftruncate: %s", strerror(errno));
            }
        }
        exit(1);
    }
    server.aof_current_size += nwritten;

    /* Re-use AOF buffer when it is small enough. The maximum comes from the
     * arena size of 4k minus some overhead (but is otherwise arbitrary). */
    if ((sdslen(server.aof_buf)+sdsavail(server.aof_buf)) < 4000) {
        sdsclear(server.aof_buf);
    } else {
        sdsfree(server.aof_buf);
        server.aof_buf = sdsempty();
    }

    /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are
     * children doing I/O in the background. */
    if (server.aof_no_fsync_on_rewrite &&
        (server.aof_child_pid != -1 || server.rdb_child_pid != -1))
            return;

    /* Perform the fsync if needed. */
    if (server.aof_fsync == AOF_FSYNC_ALWAYS) {
        /* aof_fsync is defined as fdatasync() for Linux in order to avoid
         * flushing metadata. */
        aof_fsync(server.aof_fd); /* Let's try to get this data on the disk */
        server.aof_last_fsync = server.unixtime;
    } else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC &&
                server.unixtime > server.aof_last_fsync)) {
        if (!sync_in_progress) aof_background_fsync(server.aof_fd);
        server.aof_last_fsync = server.unixtime;
    }
}
示例#13
0
文件: dri.c 项目: 4eremuxa/xserver
/*
 * This creates a shared memory buffer for use with GLXPixmaps
 * and AppleSGLX.
 */
Bool DRICreatePixmap(ScreenPtr pScreen, Drawable id,
		     DrawablePtr pDrawable, char *path,
		     size_t pathmax) 
{
    DRIPixmapBufferPtr shared;
    PixmapPtr pPix;
    
    if(pDrawable->type != DRAWABLE_PIXMAP)
	return FALSE;

    pPix = (PixmapPtr)pDrawable;

    shared = malloc(sizeof(*shared));
    if(NULL == shared) {
        FatalError("failed to allocate DRIPixmapBuffer in %s\n", __func__);
    }
        
    shared->pDrawable = pDrawable;
    shared->refCount = 1;

    if(pDrawable->bitsPerPixel >= 24) {
	shared->bytesPerPixel = 4;
    } else if(pDrawable->bitsPerPixel <= 16) {
	shared->bytesPerPixel = 2;
    }
    
    shared->width = pDrawable->width;
    shared->height = pDrawable->height;
    
    if(-1 == snprintf(shared->shmPath, sizeof(shared->shmPath),
                      "%d_0x%lx", getpid(),
                      (unsigned long)id)) {
        FatalError("buffer overflow in %s\n", __func__);
    }
    
    shared->fd = shm_open(shared->shmPath, 
                          O_RDWR | O_EXCL | O_CREAT, 
                          S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
    
    if(-1 == shared->fd) {
	free(shared);
        return FALSE;
    }   
    
    shared->length = shared->width * shared->height * shared->bytesPerPixel;
    
    if(-1 == ftruncate(shared->fd, shared->length)) {
	ErrorF("failed to ftruncate (extend) file.");
	shm_unlink(shared->shmPath);
	close(shared->fd);
	free(shared);
	return FALSE;
    }

    shared->buffer = mmap(NULL, shared->length,
                          PROT_READ | PROT_WRITE,
                          MAP_FILE | MAP_SHARED, shared->fd, 0);
    
    if(MAP_FAILED == shared->buffer) {
	ErrorF("failed to mmap shared memory.");
	shm_unlink(shared->shmPath);
	close(shared->fd);
	free(shared);
	return FALSE;
    }
    
    strncpy(path, shared->shmPath, pathmax);
    path[pathmax - 1] = '\0';
    
    dixSetPrivate(&pPix->devPrivates, DRIPixmapBufferPrivKey, shared);

    AddResource(id, DRIDrawablePrivResType, (pointer)pDrawable);

    return TRUE;
}
示例#14
0
int
main(int argc, char *argv[])
{
	uint64_t headerSize = 0;
	uint64_t imageSize = 0;
	const char *file = NULL;
	const char *uuid = NULL;
	bool headerOnly = false;
	bool clearImage = false;
	bool dumpOnly = false;

	if (sizeof(SparseExtentHeader) != 512) {
		fprintf(stderr, "compilation error: struct size is %u byte\n",
			(unsigned)sizeof(SparseExtentHeader));
		exit(EXIT_FAILURE);
	}

	while (1) {
		int c;
		static struct option long_options[] = {
			{"dump", no_argument, 0, 'd'},
		 	{"headersize", required_argument, 0, 'h'},
			{"imagesize", required_argument, 0, 'i'},
			{"file", required_argument, 0, 'f'},
			{"uuid", required_argument, 0, 'u'},
			{"clear-image", no_argument, 0, 'c'},
			{"header-only", no_argument, 0, 'H'},
			{0, 0, 0, 0}
		};

		opterr = 0; /* don't print errors */
		c = getopt_long(argc, argv, "dh:i:u:cHf:", long_options, NULL);
		if (c == -1)
			break;

		switch (c) {
			case 'd':
				dumpOnly = true;
				break;

			case 'h':
				headerSize = strtoull(optarg, NULL, 10);
				if (strchr(optarg, 'G') || strchr(optarg, 'g'))
					headerSize *= 1024 * 1024 * 1024;
				else if (strchr(optarg, 'M') || strchr(optarg, 'm'))
					headerSize *= 1024 * 1024;
				else if (strchr(optarg, 'K') || strchr(optarg, 'k'))
					headerSize *= 1024;
				break;

			case 'i':
				imageSize = strtoull(optarg, NULL, 10);
				if (strchr(optarg, 'G') || strchr(optarg, 'g'))
					imageSize *= 1024 * 1024 * 1024;
				else if (strchr(optarg, 'M') || strchr(optarg, 'm'))
					imageSize *= 1024 * 1024;
				else if (strchr(optarg, 'K') || strchr(optarg, 'k'))
					imageSize *= 1024;
				break;

			case 'u':
				uuid = optarg;
				if (!is_valid_uuid(uuid)) {
					fprintf(stderr, "Error: invalid UUID given (use "
						"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format only).\n");
					exit(EXIT_FAILURE);
				}
				break;

			case 'f':
				file = optarg;
				break;

			case 'c':
				clearImage = true;
				break;

			case 'H':
				headerOnly = true;
				break;

			default:
				print_usage();
		}
	}

	if (file == NULL && optind == argc - 1)
		file = argv[optind];

	if (dumpOnly && file != NULL) {
		dump_image_info(file);
		return 0;
	}

	if (!headerSize || !imageSize || !file)
		print_usage();

	char desc[1024];
	SparseExtentHeader header;

	if (headerSize < sizeof(desc) + sizeof(header)) {
		fprintf(stderr, "Error: header size must be at least %u byte\n",
			(unsigned)(sizeof(desc) + sizeof(header)));
		exit(EXIT_FAILURE);
	}

	if (headerSize % 512) {
		fprintf(stderr, "Error: header size must be a multiple of 512 bytes\n");
		exit(EXIT_FAILURE);
	}

	if (imageSize % 512) {
		fprintf(stderr, "Error: image size must be a multiple of 512 bytes\n");
		exit(EXIT_FAILURE);
	}

	// arbitrary 1 GB limitation
	if (headerSize > 0x40000000ULL) {
		fprintf(stderr, "Error: header size too large\n");
		exit(EXIT_FAILURE);
	}

	// arbitrary 4 GB limitation
	if (imageSize > 0x100000000ULL) {
		fprintf(stderr, "Error: image size too large\n");
		exit(EXIT_FAILURE);
	}

	const char *name = strrchr(file, '/');
	name = name ? (name + 1) : file;

//	printf("headerSize %llu\n", headerSize);
//	printf("imageSize %llu\n", imageSize);
//	printf("file %s\n", file);

	uint64_t sectors;
	uint64_t heads;
	uint64_t cylinders;

	// TODO: fixme!
	sectors = 63;
	heads = 16;
	cylinders = imageSize / (sectors * heads * 512);
	while (cylinders > 1024) {
		cylinders /= 2;
		heads *= 2;
	}
	off_t actualImageSize = (off_t)cylinders * sectors * heads * 512;

	memset(desc, 0, sizeof(desc));
	memset(&header, 0, sizeof(header));

	header.magicNumber = VMDK_SPARSE_MAGICNUMBER;
	header.version = VMDK_SPARSE_VERSION;
	header.flags = 1;
	header.capacity = 0;
	header.grainSize = 16;
	header.descriptorOffset = 1;
	header.descriptorSize = (sizeof(desc) + 511) / 512;
	header.numGTEsPerGT = 512;
	header.rgdOffset = 0;
	header.gdOffset = 0;
	header.overHead = headerSize / 512;
	header.uncleanShutdown = 0;
	header.singleEndLineChar = '\n';
	header.nonEndLineChar = ' ';
	header.doubleEndLineChar1 = '\r';
	header.doubleEndLineChar2 = '\n';

	// Generate UUID for the image by hashing its full path
	uint64_t uuid1 = 0, uuid2 = 0, uuid3 = 0, uuid4 = 0, uuid5 = 0;
	if (uuid == NULL) {
		char fullPath[PATH_MAX + 6];
		strcpy(fullPath, "Antares");

		if (realpath(file, fullPath + 5) == NULL)
			strncpy(fullPath + 5, file, sizeof(fullPath) - 5);

		size_t pathLength = strlen(fullPath);
		for (size_t i = pathLength; i < 42; i++) {
			// fill rest with some numbers
			fullPath[i] = i % 10 + '0';
		}
		if (pathLength < 42)
			fullPath[42] = '\0';

		uuid1 = hash_string(fullPath);
		uuid2 = hash_string(fullPath + 5);
		uuid3 = hash_string(fullPath + 13);
		uuid4 = hash_string(fullPath + 19);
		uuid5 = hash_string(fullPath + 29);
	}

	// Create embedded descriptor
	strcat(desc,
		"# Disk Descriptor File\n"
		"version=1\n"
		"CID=fffffffe\n"
		"parentCID=ffffffff\n"
		"createType=\"monolithicFlat\"\n");
	sprintf(desc + strlen(desc),
		"# Extent Description\n"
		"RW %llu FLAT \"%s\" %llu\n",
		(unsigned long long)actualImageSize / 512, name,
		(unsigned long long)headerSize / 512);
	sprintf(desc + strlen(desc),
		"# Disk Data Base\n"
		"ddb.toolsVersion = \"0\"\n"
		"ddb.virtualHWVersion = \"3\"\n"
		"ddb.geometry.sectors = \"%llu\"\n"
		"ddb.adapterType = \"ide\"\n"
		"ddb.geometry.heads = \"%llu\"\n"
		"ddb.geometry.cylinders = \"%llu\"\n",
		(unsigned long long)sectors, (unsigned long long)heads,
		(unsigned long long)cylinders);

	if (uuid == NULL) {
		sprintf(desc + strlen(desc),
			"ddb.uuid.image=\"%08llx-%04llx-%04llx-%04llx-%012llx\"\n",
			uuid1 & 0xffffffffLL, uuid2 & 0xffffLL, uuid3 & 0xffffLL,
			uuid4 & 0xffffLL, uuid5 & 0xffffffffffffLL);
	} else
		sprintf(desc + strlen(desc), "ddb.uuid.image=\"%s\"\n", uuid);

	int fd = open(file, O_RDWR | O_CREAT, 0666);
	if (fd < 0) {
		fprintf(stderr, "Error: couldn't open file %s (%s)\n", file,
			strerror(errno));
		exit(EXIT_FAILURE);
	}
	if (write(fd, &header, sizeof(header)) != sizeof(header))
		goto write_err;

	if (write(fd, desc, sizeof(desc)) != sizeof(desc))
		goto write_err;

	if ((uint64_t)lseek(fd, headerSize - 1, SEEK_SET) != headerSize - 1)
		goto write_err;

	if (1 != write(fd, "", 1))
		goto write_err;

	if (!headerOnly) {
		if ((clearImage && ftruncate(fd, headerSize) != 0)
			|| ftruncate(fd, actualImageSize + headerSize) != 0) {
			fprintf(stderr, "Error: resizing file %s failed (%s)\n", file,
				strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	close(fd);
	return 0;

write_err:
	fprintf(stderr, "Error: writing file %s failed (%s)\n", file,
		strerror(errno));
	close(fd);
	exit(EXIT_FAILURE);
}
示例#15
0
文件: state.cpp 项目: CodEnFisH/mesos
Try<TaskState> TaskState::recover(
    const string& rootDir,
    const SlaveID& slaveId,
    const FrameworkID& frameworkId,
    const ExecutorID& executorId,
    const UUID& uuid,
    const TaskID& taskId,
    bool strict)
{
  TaskState state;
  state.id = taskId;
  string message;

  // Read the task info.
  string path = paths::getTaskInfoPath(
      rootDir, slaveId, frameworkId, executorId, uuid, taskId);
  if (!os::exists(path)) {
    // This could happen if the slave died after creating the task
    // directory but before it checkpointed the task info.
    LOG(WARNING) << "Failed to find task info file '" << path << "'";
    return state;
  }

  const Result<Task>& task = ::protobuf::read<Task>(path);

  if (!task.isSome()) {
    message = "Failed to read task info from '" + path +
              "': " + (task.isError() ? task.error() : " none");

    if (strict) {
      return Error(message);
    } else {
      LOG(WARNING) << message;
      state.errors++;
      return state;
    }
  }

  state.info = task.get();

  // Read the status updates.
  path = paths::getTaskUpdatesPath(
      rootDir, slaveId, frameworkId, executorId, uuid, taskId);
  if (!os::exists(path)) {
    // This could happen if the slave died before it checkpointed
    // any status updates for this task.
    LOG(WARNING) << "Failed to find status updates file '" << path << "'";
    return state;
  }

  // Open the status updates file for reading and writing (for truncating).
  const Try<int>& fd = os::open(path, O_RDWR);

  if (fd.isError()) {
    message = "Failed to open status updates file '" + path +
              "': " + fd.error();

    if (strict) {
      return Error(message);
    } else {
      LOG(WARNING) << message;
      state.errors++;
      return state;
    }
  }

  // Now, read the updates.
  Result<StatusUpdateRecord> record = None();
  while (true) {
    // Ignore errors due to partial protobuf read.
    record = ::protobuf::read<StatusUpdateRecord>(fd.get(), true);

    if (!record.isSome()) {
      break;
    }

    if (record.get().type() == StatusUpdateRecord::UPDATE) {
      state.updates.push_back(record.get().update());
    } else {
      state.acks.insert(UUID::fromBytes(record.get().uuid()));
    }
  }

  // Always truncate the file to contain only valid updates.
  // NOTE: This is safe even though we ignore partial protobuf
  // read errors above, because the 'fd' is properly set to the
  // end of the last valid update by 'protobuf::read()'.
  if (ftruncate(fd.get(), lseek(fd.get(), 0, SEEK_CUR)) != 0) {
    return ErrnoError(
        "Failed to truncate status updates file '" + path + "'");
  }

  // After reading a non-corrupted updates file, 'record' should be 'none'.
  if (record.isError()) {
    message = "Failed to read status updates file  '" + path +
              "': " + record.error();

    if (strict) {
      return Error(message);
    } else {
      LOG(WARNING) << message;
      state.errors++;
      return state;
    }
  }

  // Close the updates file.
  const Try<Nothing>& close = os::close(fd.get());

  if (close.isError()) {
    message = "Failed to close status updates file '" + path +
              "': " + close.error();

    if (strict) {
      return Error(message);
    } else {
      LOG(WARNING) << message;
      state.errors++;
      return state;
    }
  }

  return state;
}
示例#16
0
void Ftruncate(int fd, off_t length)
{
    if (ftruncate(fd, length) == -1)
		err_sys("ftruncate error");
}
示例#17
0
bool
HaikuAggGlue::prepDrawingArea(int width, int height, boost::uint32_t sdl_flags)
{
    (void) sdl_flags;

    assert(width > 0);
    assert(height > 0);
    if (_width == width && _height == height)
        QQ(1);
        //return true;

    int depth_bytes = _bpp / 8;  // TODO: <Udo> is this correct? Gives 1 for 15 bit modes!

    assert(_bpp % 8 == 0);

    boost::uint32_t rmask, gmask, bmask, amask;

    switch(_bpp) {
        case 32: // RGBA32
            // BGRA32?
            rmask = 0xFF;
            gmask = 0xFF << 8;
            bmask = 0xFF << 16;
            amask = 0xFF << 24;
            break;
        case 24: // RGB24
            rmask = 0xFF;
            gmask = 0xFF << 8;
            bmask = 0xFF << 16;
            amask = 0;
            break;
        case 16: // RGB565: 5 bits for red, 6 bits for green, and 5 bits for blue
            rmask = 0x1F << 11;
            gmask = 0x3F << 5;
            bmask = 0x1F;
            amask = 0;
            break;
        default:
            abort();
    }

#define CHUNK_SIZE (100 * 100 * depth_bytes)

    int bufsize = static_cast<int>(width * height * depth_bytes / CHUNK_SIZE + 1) * CHUNK_SIZE;

    if (_xid != 0)
    {
        int pagesize = getpagesize();
        bufsize = ((bufsize + pagesize - 1) / pagesize) * pagesize;
    }

    if (_bufsize != (unsigned)bufsize)
    {
        if (_xid != 0 && _bufsize != 0)
        {
            if (msync(_sharebuf, _bufsize, MS_INVALIDATE) != 0)
                perror("msync");
            if (munmap(_sharebuf, _bufsize) != 0)
                perror("munmap");
        }

        delete [] _offscreenbuf;
        _bufsize = bufsize;
        _offscreenbuf = new unsigned char[bufsize];
        //BlankScreen();

        if (_xid != 0)
        {
	        if (ftruncate(_sharefd, _bufsize) != 0)
		        perror("ftruncate");
            _sharebuf =
                static_cast<unsigned char*>(
                mmap(
                    (caddr_t)0,
                    _bufsize,
                    PROT_READ|PROT_WRITE,
                    MAP_SHARED,
                    _sharefd,
                    0
            ));
            if (_sharebuf == (void*) -1)
            {
                perror("mmap");
                exit(1);
            }
            memset(_sharebuf, 0xcc, _bufsize);
        }

        log_debug (_("SDL-AGG: %i byte offscreen buffer allocated"), bufsize);
    }
    _width = width;
    _height = height;


    // Only the AGG renderer has the function init_buffer, which is *not* part of
    // the renderer api. It allows us to change the renderers movie size (and buffer
    // address) during run-time.
    Renderer_agg_base * renderer =
        static_cast<Renderer_agg_base *>(_agg_renderer);
    renderer->init_buffer(_offscreenbuf, bufsize, width, height,
            width*((_bpp+7)/8));

    if (_view != NULL)
        ViewNeeded();

    _validbounds.setTo(0, 0, width-1, height-1);

    return true;
}
示例#18
0
int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
		const int fsync_after_written_bytes, const int timeout, \
		int64_t *true_file_bytes)
{
	int write_fd;
	char buff[FDFS_WRITE_BUFF_SIZE];
	int64_t remain_bytes;
	int recv_bytes;
	int written_bytes;
	int result;
	int flags;
	int count;
	tcprecvdata_exfunc recv_func;

	*true_file_bytes = 0;
	flags = fcntl(sock, F_GETFL, 0);
	if (flags < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	if (flags & O_NONBLOCK)
	{
		recv_func = tcprecvdata_nb_ex;
	}
	else
	{
		recv_func = tcprecvdata_ex;
	}

	write_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (write_fd < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	written_bytes = 0;
	remain_bytes = file_bytes;
	while (remain_bytes > 0)
	{
		if (remain_bytes > sizeof(buff))
		{
			recv_bytes = sizeof(buff);
		}
		else
		{
			recv_bytes = remain_bytes;
		}

		result = recv_func(sock, buff, recv_bytes, \
				timeout, &count);
		if (result != 0)
		{
			if (file_bytes != INFINITE_FILE_SIZE)
			{
				close(write_fd);
				unlink(filename);
				return result;
			}
		}

		if (count > 0 && write(write_fd, buff, count) != count)
		{
			result = errno != 0 ? errno: EIO;
			close(write_fd);
			unlink(filename);
			return result;
		}

		*true_file_bytes += count;
		if (fsync_after_written_bytes > 0)
		{
			written_bytes += count;
			if (written_bytes >= fsync_after_written_bytes)
			{
				written_bytes = 0;
				if (fsync(write_fd) != 0)
				{
					result = errno != 0 ? errno: EIO;
					close(write_fd);
					unlink(filename);
					return result;
				}
			}
		}

		if (result != 0)  //recv infinite file, does not delete the file
		{
			int read_fd;
			read_fd = -1;

			do
			{
				if (*true_file_bytes < 8)
				{
					break;
				}

				read_fd = open(filename, O_RDONLY);
				if (read_fd < 0)
				{
					return errno != 0 ? errno : EACCES;
				}

				if (lseek(read_fd, -8, SEEK_END) < 0)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				if (read(read_fd, buff, 8) != 8)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				*true_file_bytes -= 8;
				if (buff2long(buff) != *true_file_bytes)
				{
					result = EINVAL;
					break;
				}

				if (ftruncate(write_fd, *true_file_bytes) != 0)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				result = 0;
			} while (0);
		
			close(write_fd);
			if (read_fd >= 0)
			{
				close(read_fd);
			}

			if (result != 0)
			{
				unlink(filename);
			}

			return result;
		}

		remain_bytes -= count;
	}

	close(write_fd);
	return 0;
}
示例#19
0
文件: movemail.c 项目: exedre/emacs
int
main (int argc, char **argv)
{
  char *inname, *outname;
  int indesc, outdesc;
  ssize_t nread;
  int wait_status;
  int c, preserve_mail = 0;

#ifndef MAIL_USE_SYSTEM_LOCK
  struct stat st;
  int tem;
  char *lockname;
  char *tempname;
  size_t inname_len, inname_dirlen;
  int desc;
#endif /* not MAIL_USE_SYSTEM_LOCK */

#ifdef MAIL_USE_MAILLOCK
  char *spool_name;
#endif

#ifdef MAIL_USE_POP
  int pop_reverse_order = 0;
# define ARGSTR "pr"
#else /* ! MAIL_USE_POP */
# define ARGSTR "p"
#endif /* MAIL_USE_POP */

  uid_t real_gid = getgid ();
  uid_t priv_gid = getegid ();

#ifdef WINDOWSNT
  /* Ensure all file i/o is in binary mode. */
  _fmode = _O_BINARY;
#endif

  delete_lockname = 0;

  while ((c = getopt (argc, argv, ARGSTR)) != EOF)
    {
      switch (c) {
#ifdef MAIL_USE_POP
      case 'r':
	pop_reverse_order = 1;
	break;
#endif
      case 'p':
	preserve_mail++;
	break;
      default:
	exit (EXIT_FAILURE);
      }
    }

  if (
#ifdef MAIL_USE_POP
      (argc - optind < 2) || (argc - optind > 3)
#else
      (argc - optind != 2)
#endif
      )
    {
#ifdef MAIL_USE_POP
      fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n",
	       " [POP-password]");
#else
      fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", "");
#endif
      exit (EXIT_FAILURE);
    }

  inname = argv[optind];
  outname = argv[optind+1];

#ifdef MAIL_USE_MMDF
  mmdf_init (argv[0]);
#endif

  if (*outname == 0)
    fatal ("Destination file name is empty", 0, 0);

#ifdef MAIL_USE_POP
  if (!strncmp (inname, "po:", 3))
    {
      int status;

      status = popmail (inname + 3, outname, preserve_mail,
			(argc - optind == 3) ? argv[optind+2] : NULL,
			pop_reverse_order);
      exit (status);
    }

  if (setuid (getuid ()) < 0)
    fatal ("Failed to drop privileges", 0, 0);

#endif /* MAIL_USE_POP */

#ifndef DISABLE_DIRECT_ACCESS
#ifndef MAIL_USE_MMDF
#ifndef MAIL_USE_SYSTEM_LOCK
#ifdef MAIL_USE_MAILLOCK
  spool_name = mail_spool_name (inname);
  if (spool_name)
    {
#ifdef lint
      lockname = 0;
#endif
    }
  else
#endif
    {
      /* Use a lock file named after our first argument with .lock appended:
	 If it exists, the mail file is locked.  */
      /* Note: this locking mechanism is *required* by the mailer
	 (on systems which use it) to prevent loss of mail.

	 On systems that use a lock file, extracting the mail without locking
	 WILL occasionally cause loss of mail due to timing errors!

	 So, if creation of the lock file fails due to access
	 permission on the mail spool directory, you simply MUST
	 change the permission and/or make movemail a setgid program
	 so it can create lock files properly.

	 You might also wish to verify that your system is one which
	 uses lock files for this purpose.  Some systems use other methods.  */

      inname_len = strlen (inname);
      lockname = xmalloc (inname_len + sizeof ".lock");
      strcpy (lockname, inname);
      strcpy (lockname + inname_len, ".lock");
      for (inname_dirlen = inname_len;
	   inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
	   inname_dirlen--)
	continue;
      tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX");

      while (1)
	{
	  /* Create the lock file, but not under the lock file name.  */
	  /* Give up if cannot do that.  */

	  memcpy (tempname, inname, inname_dirlen);
	  strcpy (tempname + inname_dirlen, "EXXXXXX");
#ifdef HAVE_MKSTEMP
	  desc = mkstemp (tempname);
#else
	  mktemp (tempname);
	  if (!*tempname)
	    desc = -1;
	  else
	    {
	      unlink (tempname);
	      desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
	    }
#endif
	  if (desc < 0)
	    {
	      int mkstemp_errno = errno;
	      error ("error while creating what would become the lock file",
		     0, 0);
	      errno = mkstemp_errno;
	      pfatal_with_name (tempname);
	    }
	  close (desc);

	  tem = link (tempname, lockname);

	  if (tem < 0 && errno != EEXIST)
	    pfatal_with_name (lockname);

	  unlink (tempname);
	  if (tem >= 0)
	    break;
	  sleep (1);

	  /* If lock file is five minutes old, unlock it.
	     Five minutes should be good enough to cope with crashes
	     and wedgitude, and long enough to avoid being fooled
	     by time differences between machines.  */
	  if (stat (lockname, &st) >= 0)
	    {
	      time_t now = time (0);
	      if (st.st_ctime < now - 300)
		unlink (lockname);
	    }
	}

      delete_lockname = lockname;
    }
#endif /* not MAIL_USE_SYSTEM_LOCK */
#endif /* not MAIL_USE_MMDF */

  if (fork () == 0)
    {
      int lockcount = 0;
      int status = 0;
#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
      time_t touched_lock;
# ifdef lint
      touched_lock = 0;
# endif
#endif

      if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
	fatal ("Failed to drop privileges", 0, 0);

#ifndef MAIL_USE_MMDF
#ifdef MAIL_USE_SYSTEM_LOCK
      indesc = open (inname, O_RDWR);
#else  /* if not MAIL_USE_SYSTEM_LOCK */
      indesc = open (inname, O_RDONLY);
#endif /* not MAIL_USE_SYSTEM_LOCK */
#else  /* MAIL_USE_MMDF */
      indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
#endif /* MAIL_USE_MMDF */

      if (indesc < 0)
	pfatal_with_name (inname);

      /* Make sure the user can read the output file.  */
      umask (umask (0) & 0377);

      outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
      if (outdesc < 0)
	pfatal_with_name (outname);

      if (setregid (-1, priv_gid) < 0)
	fatal ("Failed to regain privileges", 0, 0);

      /* This label exists so we can retry locking
	 after a delay, if it got EAGAIN or EBUSY.  */
    retry_lock:

      /* Try to lock it.  */
#ifdef MAIL_USE_MAILLOCK
      if (spool_name)
	{
	  /* The "0 - " is to make it a negative number if maillock returns
	     non-zero. */
	  status = 0 - maillock (spool_name, 1);
#ifdef HAVE_TOUCHLOCK
	  touched_lock = time (0);
#endif
	  lockcount = 5;
	}
      else
#endif /* MAIL_USE_MAILLOCK */
	{
#ifdef MAIL_USE_SYSTEM_LOCK
#ifdef MAIL_USE_LOCKF
	  status = lockf (indesc, F_LOCK, 0);
#else /* not MAIL_USE_LOCKF */
#ifdef WINDOWSNT
	  status = locking (indesc, LK_RLCK, -1L);
#else
	  status = flock (indesc, LOCK_EX);
#endif
#endif /* not MAIL_USE_LOCKF */
#endif /* MAIL_USE_SYSTEM_LOCK */
	}

      /* If it fails, retry up to 5 times
	 for certain failure codes.  */
      if (status < 0)
	{
	  if (++lockcount <= 5 && (errno == EAGAIN || errno == EBUSY))
	    {
	      sleep (1);
	      goto retry_lock;
	    }

	  pfatal_with_name (inname);
	}

      {
	char buf[1024];

	while (1)
	  {
	    nread = read (indesc, buf, sizeof buf);
	    if (nread < 0)
	      pfatal_with_name (inname);
	    if (nread != write (outdesc, buf, nread))
	      {
		int saved_errno = errno;
		unlink (outname);
		errno = saved_errno;
		pfatal_with_name (outname);
	      }
	    if (nread < sizeof buf)
	      break;
#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
	    if (spool_name)
	      {
		time_t now = time (0);
		if (now - touched_lock > 60)
		  {
		    touchlock ();
		    touched_lock = now;
		  }
	      }
#endif /* MAIL_USE_MAILLOCK */
	  }
      }

      if (fsync (outdesc) != 0 && errno != EINVAL)
	pfatal_and_delete (outname);

      /* Prevent symlink attacks truncating other users' mailboxes */
      if (setregid (-1, real_gid) < 0)
	fatal ("Failed to drop privileges", 0, 0);

      /* Check to make sure no errors before we zap the inbox.  */
      if (close (outdesc) != 0)
	pfatal_and_delete (outname);

#ifdef MAIL_USE_SYSTEM_LOCK
      if (! preserve_mail)
	{
	  if (ftruncate (indesc, 0L) != 0)
	    pfatal_with_name (inname);
	}
#endif /* MAIL_USE_SYSTEM_LOCK */

#ifdef MAIL_USE_MMDF
      lk_close (indesc, 0, 0, 0);
#else
      close (indesc);
#endif

#ifndef MAIL_USE_SYSTEM_LOCK
      if (! preserve_mail)
	{
	  /* Delete the input file; if we can't, at least get rid of its
	     contents.  */
#ifdef MAIL_UNLINK_SPOOL
	  /* This is generally bad to do, because it destroys the permissions
	     that were set on the file.  Better to just empty the file.  */
	  if (unlink (inname) < 0 && errno != ENOENT)
#endif /* MAIL_UNLINK_SPOOL */
	    creat (inname, 0600);
	}
#endif /* not MAIL_USE_SYSTEM_LOCK */

      /* End of mailbox truncation */
      if (setregid (-1, priv_gid) < 0)
	fatal ("Failed to regain privileges", 0, 0);

#ifdef MAIL_USE_MAILLOCK
      /* This has to occur in the child, i.e., in the process that
         acquired the lock! */
      if (spool_name)
	mailunlock ();
#endif
      exit (EXIT_SUCCESS);
    }

  wait (&wait_status);
  if (!WIFEXITED (wait_status))
    exit (EXIT_FAILURE);
  else if (WEXITSTATUS (wait_status) != 0)
    exit (WEXITSTATUS (wait_status));

#if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
#ifdef MAIL_USE_MAILLOCK
  if (! spool_name)
#endif /* MAIL_USE_MAILLOCK */
    unlink (lockname);
#endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */

#endif /* ! DISABLE_DIRECT_ACCESS */

  return EXIT_SUCCESS;
}
示例#20
0
LIBSTDCL_API CONTEXT* 
clcontext_create( 
	const char* platform_name, 
	int devtyp, 
	size_t ndevmax,
	cl_context_properties* ctxprop_ext, 
	int lock_key
)
{

	int n;
	int err = 0;
	int i;
	size_t devlist_sz;
	CONTEXT* cp = 0;
	cl_platform_id* platforms = 0;
	cl_uint nplatforms;
	char info[1024];
	cl_platform_id platformid;
	int nctxprop = 0;
	cl_context_properties* ctxprop;
	size_t sz;
	cl_uint ndev = 0;
	cl_command_queue_properties prop = 0;

	DEBUG(__FILE__,__LINE__,"clcontext_create() called");


//	if (ndevmax) 
//		WARN(__FILE__,__LINE__,"__clcontext_create(): ndevmax argument ignored");


	/***
	 *** allocate CONTEXT struct
	 ***/

	DEBUG(__FILE__,__LINE__,
		"clcontext_create: sizeof CONTEXT %d",sizeof(CONTEXT));

//	cp = (CONTEXT*)malloc(sizeof(CONTEXT));
	assert(sizeof(CONTEXT)<getpagesize());
#ifdef _WIN64
	cp = (CONTEXT*)_aligned_malloc(sizeof(CONTEXT),getpagesize());
	if (!cp) {
		WARN(__FILE__,__LINE__,"memalign failed");
	}
#else
	if (posix_memalign((void**)&cp,getpagesize(),sizeof(CONTEXT))) {
		WARN(__FILE__,__LINE__,"posix_memalign failed");
	}
#endif

	DEBUG(__FILE__,__LINE__,"clcontext_create: context_ptr=%p",cp);
	
	if ((intptr_t)cp & (getpagesize()-1)) {
		ERROR(__FILE__,__LINE__,
			"clcontext_create: fatal error: unaligned context_ptr");
		exit(-1);
	}

	if (!cp) { errno=ENOMEM; return(0); }

	

   /***
    *** get platform id
    ***/



   clGetPlatformIDs(0,0,&nplatforms);

//	printf("XXX %d\n",nplatforms);

   if (nplatforms) {

      platforms = (cl_platform_id*)malloc(nplatforms*sizeof(cl_platform_id));
      clGetPlatformIDs(nplatforms,platforms,0);

      for(i=0;i<nplatforms;i++) { 

         char info[1024];

         DEBUG(__FILE__,__LINE__,"_libstdcl_init: available platform:");

         clGetPlatformInfo(platforms[i],CL_PLATFORM_PROFILE,1024,info,0);
         DEBUG(__FILE__,__LINE__,
            "_libstdcl_init: [%p]CL_PLATFORM_PROFILE=%s",platforms[i],info);

         clGetPlatformInfo(platforms[i],CL_PLATFORM_VERSION,1024,info,0);
         DEBUG(__FILE__,__LINE__,
            "_libstdcl_init: [%p]CL_PLATFORM_VERSION=%s",platforms[i],info);

         clGetPlatformInfo(platforms[i],CL_PLATFORM_NAME,1024,info,0);
         DEBUG(__FILE__,__LINE__,
            "_libstdcl_init: [%p]CL_PLATFORM_NAME=%s",platforms[i],info);

         clGetPlatformInfo(platforms[i],CL_PLATFORM_VENDOR,1024,info,0);
         DEBUG(__FILE__,__LINE__,
            "_libstdcl_init: [%p]CL_PLATFORM_VENDOR=%s",platforms[i],info);

         clGetPlatformInfo(platforms[i],CL_PLATFORM_EXTENSIONS,1024,info,0);
         DEBUG(__FILE__,__LINE__,
            "_libstdcl_init: [%p]CL_PLATFORM_EXTENSIONS=%s",platforms[i],info);

      }

   } else {

      WARN(__FILE__,__LINE__,
         "_libstdcl_init: no platforms found, continue and hope for the best");

   }

	platformid 
		= __get_platformid(nplatforms, platforms, platform_name);

	DEBUG(__FILE__,__LINE__,"clcontext_create: platformid=%p",platformid);

	

	/***
	 *** create context
	 ***/

	

	while (ctxprop_ext != 0 && ctxprop_ext[nctxprop] != 0) ++nctxprop;

//	cl_context_properties ctxprop[3] = {
//		(cl_context_properties)CL_CONTEXT_PLATFORM,
//		(cl_context_properties)platformid,
//		(cl_context_properties)0
//	};

	nctxprop += 3;

	ctxprop = (cl_context_properties*)malloc(nctxprop*sizeof(cl_context_properties));

	ctxprop[0] = (cl_context_properties)CL_CONTEXT_PLATFORM;
	ctxprop[1] = (cl_context_properties)platformid;

	for(i=0;i<nctxprop-3;i++) ctxprop[2+i] = ctxprop_ext[i];

	ctxprop[nctxprop-1] =  (cl_context_properties)0;

	

	clGetPlatformInfo(platformid,CL_PLATFORM_PROFILE,0,0,&sz);
	cp->platform_profile = (char*)malloc(sz);
	clGetPlatformInfo(platformid,CL_PLATFORM_PROFILE,sz,cp->platform_profile,0);

	clGetPlatformInfo(platformid,CL_PLATFORM_VERSION,0,0,&sz);
	cp->platform_version = (char*)malloc(sz);
	clGetPlatformInfo(platformid,CL_PLATFORM_VERSION,sz,cp->platform_version,0);

	clGetPlatformInfo(platformid,CL_PLATFORM_NAME,0,0,&sz);
	cp->platform_name = (char*)malloc(sz);
	clGetPlatformInfo(platformid,CL_PLATFORM_NAME,sz,cp->platform_name,0);

	clGetPlatformInfo(platformid,CL_PLATFORM_VENDOR,0,0,&sz);
	cp->platform_vendor = (char*)malloc(sz);
	clGetPlatformInfo(platformid,CL_PLATFORM_VENDOR,sz,cp->platform_vendor,0);

	clGetPlatformInfo(platformid,CL_PLATFORM_EXTENSIONS,0,0,&sz);
	cp->platform_extensions = (char*)malloc(sz);
	clGetPlatformInfo(platformid,CL_PLATFORM_EXTENSIONS,sz,
		cp->platform_extensions,0);


#ifdef _WIN64
	cp->ctx = clCreateContextFromType(ctxprop,devtyp,0,0,&err);
#else

	if (lock_key > 0) {

		if (ndevmax == 0) ndevmax = 1;

		cl_uint platform_ndev;
		err = clGetDeviceIDs(platformid,devtyp,0,0,&platform_ndev);
//		cl_uint platform_vndev = 2*platform_ndev;
		cl_uint platform_vndev = platform_ndev;

//DEBUG(__FILE__,__LINE__,"%d %d",platform_ndev,platform_vndev);
	
		cl_device_id* platform_dev 
			= (cl_device_id*)malloc(platform_ndev*sizeof(cl_device_id));

		err = clGetDeviceIDs(platformid,devtyp,platform_ndev,platform_dev,0);

		DEBUG(__FILE__,__LINE__,"clcontext_create: lock_key=%d",lock_key);

		pid_t pid = getpid();

		size_t sz_page = getpagesize();

//		system("ls /dev/shm");

		char shmobj[64];
		snprintf(shmobj,64,"/stdcl_ctx_lock%d.%d",devtyp,lock_key);

		DEBUG(__FILE__,__LINE__,
			"clcontext_create: attempt master shm_open %s from %d",shmobj,pid);

		int fd = shm_open(shmobj,O_RDWR|O_CREAT|O_EXCL,0);
		void* p0;

		struct timeval t0,t1;
		int timeout = 0;

		int noff = 0;

		if (fd < 0) {
			
			DEBUG(__FILE__,__LINE__,
				"clcontext_create: master shm_open failed from %d (%d)",pid,fd);

			DEBUG(__FILE__,__LINE__,
				"clcontext_create: attempt slave shm_open from %d",pid);

			timeout = 0;
			gettimeofday(&t0,0);
			t0.tv_sec += 10;

			do {
	
				fd = shm_open(shmobj,O_RDWR,0);
				gettimeofday(&t1,0);

				if (t1.tv_sec > t0.tv_sec && t1.tv_usec > t0.tv_usec) timeout = 1;

			} while (fd < 0 && !timeout);

			if (timeout) {

				ERROR(__FILE__,__LINE__,"clcontext_create: shm_open timeout");

			}

			ftruncate(fd,sz_page);

			p0 = mmap(0,sz_page,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

			if (!p0) return(0);

			__ctx_lock = (struct __ctx_lock_struct*)p0;

			pthread_mutex_lock(&__ctx_lock->mtx);
//			if (__ctx_lock->refc < platform_ndev) {
			if (__ctx_lock->refc < platform_vndev) {
				noff = __ctx_lock->refc;
//				ndev = min(ndevmax,platform_ndev-noff);
				ndev = min(ndevmax,platform_vndev-noff);
				__ctx_lock->refc += ndev;
			}
			pthread_mutex_unlock(&__ctx_lock->mtx);

			close(fd);

		} else {

			DEBUG(__FILE__,__LINE__,
				"clcontext_create: master shm_open succeeded from %d",pid);

			ftruncate(fd,sz_page);

			p0 = mmap(0,sz_page,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

			if (!p0) return(0);

			__ctx_lock = (struct __ctx_lock_struct*)p0;

			__ctx_lock->magic = 20110415;
			__ctx_lock->key = lock_key;
			pthread_mutex_init(&__ctx_lock->mtx,0);
//			ndev = min(ndevmax,platform_ndev);
			ndev = min(ndevmax,platform_vndev);
		DEBUG(__FILE__,__LINE__,"ndev=%d %d %d",ndev,ndevmax,platform_vndev);
			__ctx_lock->refc = ndev;

			fchmod(fd,S_IRUSR|S_IWUSR);

			close(fd);

		}

		DEBUG(__FILE__,__LINE__,"ndev=%d",ndev);

//		if (noff < platform_ndev) {
		if (noff < platform_vndev) {

//			cp->ctx = clCreateContext(ctxprop,ndev,platform_dev + noff,0,0,&err);
			cp->ctx = clCreateContext(ctxprop,ndev,platform_dev + noff%platform_ndev,0,0,&err);

			DEBUG(__FILE__,__LINE__,
				"clcontext_create: platform_ndev=%d ndev=%d noffset=%d",
				platform_ndev,ndev,noff);

			if (platform_dev) free(platform_dev);

		} else {

			cp->ctx = 0;

		}

	} else {

		cp->ctx = clCreateContextFromType(ctxprop,devtyp,0,0,&err);

	}
#endif

	if (cp->ctx) {

		cp->devtyp = devtyp;
		err = clGetContextInfo(cp->ctx,CL_CONTEXT_DEVICES,0,0,&devlist_sz);
		cp->ndev = devlist_sz/sizeof(cl_device_id);
		cp->dev = (cl_device_id*)malloc(10*devlist_sz);
		err=clGetContextInfo(cp->ctx,CL_CONTEXT_DEVICES,devlist_sz,cp->dev,0);

//		cp->devtyp = devtyp;
//		err = clGetDeviceIDs(platformid,devtyp,0,0,&(cp->ndev));
//		DEBUG(__FILE__,__LINE__,"xxx %d",err);
//		DEBUG(__FILE__,__LINE__,"number of devices %d",cp->ndev);
//		cp->dev = (cl_device_id*)malloc(cp->ndev * sizeof(cl_device_id) );
//		err = clGetDeviceIDs(platformid,devtyp,cp->ndev,cp->dev,&(cp->ndev));
//		DEBUG(__FILE__,__LINE__,"xxx %d",err);
//		DEBUG(__FILE__,__LINE__," %p device[0]",cp->dev[0]);

		
	} else {

		WARN(__FILE__,__LINE__,"clcontext_create: failed");

#ifndef _WIN64
		if (lock_key > 0 && ndev > 0) {
			pthread_mutex_lock(&__ctx_lock->mtx);
			__ctx_lock->refc -= ndev;
			pthread_mutex_unlock(&__ctx_lock->mtx);
		}

		free(cp);
#else
		_aligned_free(cp);
#endif

      return((CONTEXT*)0);

   }


	DEBUG(__FILE__,__LINE__,"number of devices %d",cp->ndev);

		
	/* XXX XXX TESTING ONLY!!!! */
	//_aligned_free(cp);
	//DEBUG(__FILE__,__LINE__,"MADE IT"); return (CONTEXT*)0;


	/***
	 *** create command queues
	 ***/

	cp->cmdq = (cl_command_queue*)malloc(sizeof(cl_command_queue)*cp->ndev);

	DEBUG(__FILE__,__LINE__,"will try to create cmdq");

	

// XXX something is broken in clCreateCommandQueue, using lazy creation
// XXX as a workaround -DAR
//	{
		//cl_command_queue_properties prop = 00;
		//prop |= CL_QUEUE_PROFILING_ENABLE; /* XXX this should be choice -DAR */
		for(i=0;i<cp->ndev;i++) {
			//DEBUG(__FILE__,__LINE__,"%d calling clCreateCommandQueue(%p,%p,%x,%p)",i,cp->ctx,cp->dev[i],prop,&err);
#ifdef _WIN64
			cp->cmdq[i] = 0; /* have to defer, dllmain limitations */
#else
			cp->cmdq[i] = clCreateCommandQueue(cp->ctx,cp->dev[i],prop,&err);
			//cp->cmdq[i] = clCreateCommandQueue(cp->ctx,cp->dev[i],0,&err);
			//cl_command_queue cmdq = clCreateCommandQueue(cp->ctx,cp->dev[0],0,&err);

			DEBUG(__FILE__,__LINE__,"clcontext_create: error from create cmdq %d (%p)\n",
				err,cp->cmdq[i]);
#endif
			//DEBUG(__FILE__,__LINE__,"MADE IT"); return (CONTEXT*)0;
		}
//	}
//	printf("WARNING CMDQs NOT CREATED\n");
//	for(i=0;i<cp->ndev;i++) cp->cmdq[i] = (cl_command_queue)0;



	/***
	 *** init context resources
	 ***/

	LIST_INIT(&cp->prgs_listhead);

	LIST_INIT(&cp->txt_listhead);

	LIST_INIT(&cp->memd_listhead);


//	struct _prgs_struct* prgs 
//		= (struct _prgs_struct*)malloc(sizeof(struct _prgs_struct));
//	prgs->len=-1;
//	LIST_INSERT_HEAD(&cp->prgs_listhead, prgs, prgs_list);
//
//	prgs = (struct _prgs_struct*)malloc(sizeof(struct _prgs_struct));
//	prgs->len=-2;
//	LIST_INSERT_HEAD(&cp->prgs_listhead, prgs, prgs_list);

/*
	printf("%p searching _proc_cl for prgs...\n",_proc_cl.clstrtab);
	printf("%s\n",&_proc_cl.clstrtab[1]);
	struct clprgs_entry* sp;
	for(n=0,sp=_proc_cl.clprgs;n<_proc_cl.clprgs_n;n++,sp++) {
		printf("found %s (%d bytes)\n",&_proc_cl.clstrtab[sp->e_name],sp->e_size);
		struct _prgs_struct* prgs = (struct _prgs_struct*)
			clload(cp,_proc_cl.cltexts+sp->e_offset,sp->e_size,0);
	}
*/


	/*** 
	 *** initialize event lists
	 ***/
	
//	cp->nkev = cp->kev_first = cp->kev_free = 0;
	cp->kev = (struct _event_list_struct*)
		malloc(cp->ndev*sizeof(struct _event_list_struct));

	for(i=0;i<cp->ndev;i++) {
		cp->kev[i].nev = cp->kev[i].ev_first = cp->kev[i].ev_free = 0;
	}

//	cp->nmev = cp->mev_first = cp->mev_free = 0;
	cp->mev = (struct _event_list_struct*)
		malloc(cp->ndev*sizeof(struct _event_list_struct));

	for(i=0;i<cp->ndev;i++) {
		cp->mev[i].nev = cp->mev[i].ev_first = cp->mev[i].ev_free = 0;
	}

//#ifdef ENABLE_CLEXPORT
//	cp->ndev_v = 0;
//	cp->extd = 0;
//	cp->imtd = 0;
//#endif


	if (platforms) free(platforms);

	return(cp);

}
示例#21
0
int main(int argc, char **argv) {
    char *filename;
    int fix = 0;
#ifdef _WIN32
    _fmode = _O_BINARY;
    setmode(_fileno(stdin), _O_BINARY);
    setmode(_fileno(stdout), _O_BINARY);
    setmode(_fileno(stderr), _O_BINARY);
#endif

    if (argc < 2) {
        printf("Usage: %s [--fix] <file.aof>\n", argv[0]);
        exit(1);
    } else if (argc == 2) {
        filename = argv[1];
    } else if (argc == 3) {
        if (strcmp(argv[1],"--fix") != 0) {
            printf("Invalid argument: %s\n", argv[1]);
            exit(1);
        }
        filename = argv[2];
        fix = 1;
    } else {
        printf("Invalid arguments\n");
        exit(1);
    }

    FILE *fp = fopen(filename,IF_WIN32("r+b","r+"));
    if (fp == NULL) {
        printf("Cannot open file: %s\n", filename);
        exit(1);
    }

    struct redis_stat sb;
    if (redis_fstat(fileno(fp),&sb) == -1) {
        printf("Cannot stat file: %s\n", filename);
        exit(1);
    }

    off_t size = sb.st_size;
    if (size == 0) {
        printf("Empty file: %s\n", filename);
        exit(1);
    }

    off_t pos = process(fp);
    off_t diff = size-pos;
    printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
        (PORT_LONGLONG) size, (PORT_LONGLONG) pos, (PORT_LONGLONG) diff);
    if (diff > 0) {
        if (fix) {
            char buf[2];
            printf("This will shrink the AOF from %lld bytes, with %lld bytes, to %lld bytes\n",(PORT_LONGLONG)size,(PORT_LONGLONG)diff,(PORT_LONGLONG)pos);
            printf("Continue? [y/N]: ");
            if (fgets(buf,sizeof(buf),stdin) == NULL ||
                strncasecmp(buf,"y",1) != 0) {
                    printf("Aborting...\n");
                    exit(1);
            }
            if (ftruncate(fileno(fp), pos) == -1) {
                printf("Failed to truncate AOF\n");
                exit(1);
            } else {
                printf("Successfully truncated AOF\n");
            }
        } else {
            printf("AOF is not valid\n");
            exit(1);
        }
    } else {
        printf("AOF is valid\n");
    }

    fclose(fp);
    return 0;
}
示例#22
0
static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
{
    IVShmemState *s = IVSHMEM(dev);
    uint8_t *pci_conf;
    uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
        PCI_BASE_ADDRESS_MEM_PREFETCH;

    if (!!s->server_chr + !!s->shmobj + !!s->hostmem != 1) {
        error_setg(errp,
                   "You must specify either 'shm', 'chardev' or 'x-memdev'");
        return;
    }

    if (s->hostmem) {
        MemoryRegion *mr;

        if (s->sizearg) {
            g_warning("size argument ignored with hostmem");
        }

        mr = host_memory_backend_get_memory(s->hostmem, errp);
        s->ivshmem_size = memory_region_size(mr);
    } else if (s->sizearg == NULL) {
        s->ivshmem_size = 4 << 20; /* 4 MB default */
    } else {
        char *end;
        int64_t size = qemu_strtosz(s->sizearg, &end);
        if (size < 0 || *end != '\0' || !is_power_of_2(size)) {
            error_setg(errp, "Invalid size %s", s->sizearg);
            return;
        }
        s->ivshmem_size = size;
    }

    fifo8_create(&s->incoming_fifo, sizeof(int64_t));

    /* IRQFD requires MSI */
    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
        !ivshmem_has_feature(s, IVSHMEM_MSI)) {
        error_setg(errp, "ioeventfd/irqfd requires MSI");
        return;
    }

    /* check that role is reasonable */
    if (s->role) {
        if (strncmp(s->role, "peer", 5) == 0) {
            s->role_val = IVSHMEM_PEER;
        } else if (strncmp(s->role, "master", 7) == 0) {
            s->role_val = IVSHMEM_MASTER;
        } else {
            error_setg(errp, "'role' must be 'peer' or 'master'");
            return;
        }
    } else {
        s->role_val = IVSHMEM_MASTER; /* default */
    }

    if (s->role_val == IVSHMEM_PEER) {
        error_setg(&s->migration_blocker,
                   "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
        migrate_add_blocker(s->migration_blocker);
    }

    pci_conf = dev->config;
    pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;

    pci_config_set_interrupt_pin(pci_conf, 1);

    memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
                          "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);

    /* region for registers*/
    pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
                     &s->ivshmem_mmio);

    memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
    if (s->ivshmem_64bit) {
        attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
    }

    if (s->hostmem != NULL) {
        MemoryRegion *mr;

        IVSHMEM_DPRINTF("using hostmem\n");

        mr = host_memory_backend_get_memory(MEMORY_BACKEND(s->hostmem), errp);
        vmstate_register_ram(mr, DEVICE(s));
        memory_region_add_subregion(&s->bar, 0, mr);
        pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
    } else if (s->server_chr != NULL) {
        /* FIXME do not rely on what chr drivers put into filename */
        if (strncmp(s->server_chr->filename, "unix:", 5)) {
            error_setg(errp, "chardev is not a unix client socket");
            return;
        }

        /* if we get a UNIX socket as the parameter we will talk
         * to the ivshmem server to receive the memory region */

        IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
                        s->server_chr->filename);

        if (ivshmem_setup_interrupts(s) < 0) {
            error_setg(errp, "failed to initialize interrupts");
            return;
        }

        /* we allocate enough space for 16 peers and grow as needed */
        resize_peers(s, 16);
        s->vm_id = -1;

        pci_register_bar(dev, 2, attr, &s->bar);

        s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));

        qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
                              ivshmem_check_version, ivshmem_event, s);
    } else {
        /* just map the file immediately, we're not using a server */
        int fd;

        IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);

        /* try opening with O_EXCL and if it succeeds zero the memory
         * by truncating to 0 */
        if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
                        S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
           /* truncate file to length PCI device's memory */
            if (ftruncate(fd, s->ivshmem_size) != 0) {
                error_report("could not truncate shared file");
            }

        } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
                        S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
            error_setg(errp, "could not open shared file");
            return;
        }

        if (check_shm_size(s, fd, errp) == -1) {
            return;
        }

        create_shared_memory_BAR(s, fd, attr, errp);
    }
}
示例#23
0
文件: mmap_sanity.c 项目: LLNL/lustre
/* client1 write to file_4a from mmap()ed file_4b;
 * client2 write to file_4b from mmap()ed file_4a. */
static int mmap_tst4(char *mnt)
{
        char *ptr, filea[256], fileb[256];
        int region, fdr, fdw, rc = 0;

        region = page_size * 100;
        sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
        sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");

        if (unlink(filea) && errno != ENOENT) {
                perror("unlink()");
                return -errno;
        }
        if (unlink(fileb) && errno != ENOENT) {
                perror("unlink()");
                return -errno;
        }

        fdr = fdw = -1;
        fdr = open(fileb, O_CREAT|O_RDWR, 0600);
        if (fdr < 0) {
                perror(fileb);
                return -errno;
        }
        if (ftruncate(fdr, region) < 0) {
                perror("ftruncate()");
                rc = -errno;
                goto out_close;
        }
        fdw = open(filea, O_CREAT|O_RDWR, 0600);
        if (fdw < 0) {
                perror(filea);
                rc = -errno;
                goto out_close;
        }
        if (ftruncate(fdw, region) < 0) {
                perror("ftruncate()");
                rc = -errno;
                goto out_close;
        }

        ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
        if (ptr == MAP_FAILED) {
                perror("mmap()");
                rc = -errno;
                goto out_close;
        }

        rc = mmap_run(4);
        if (rc)
                goto out_unmap;

        memset(ptr, '1', region);

        rc = write(fdw, ptr, region);
        if (rc <= 0) {
                perror("write()");
                rc = -errno;
        } else
                rc = 0;

        sleep(2);       /* wait for remote test finish */
out_unmap:
        munmap(ptr, region);
out_close:
        if (fdr >= 0)
                close(fdr);
        if (fdw >= 0)
                close(fdw);
        unlink(filea);
        unlink(fileb);
        return rc;
}
示例#24
0
xfWindow* xf_CreateDesktopWindow(xfContext* xfc, char* name, int width,
                                 int height)
{
	XEvent xevent;
	int input_mask;
	xfWindow* window;
	Window parentWindow;
	XClassHint* classHints;
	rdpSettings* settings;
	window = (xfWindow*) calloc(1, sizeof(xfWindow));

	if (!window)
		return NULL;

	settings = xfc->context.settings;
	parentWindow = (Window) xfc->context.settings->ParentWindowId;
	window->width = width;
	window->height = height;
	window->decorations = xfc->decorations;
	window->is_mapped = FALSE;
	window->is_transient = FALSE;
	window->handle = XCreateWindow(xfc->display, RootWindowOfScreen(xfc->screen),
	                               xfc->workArea.x, xfc->workArea.y, xfc->workArea.width, xfc->workArea.height,
	                               0, xfc->depth, InputOutput, xfc->visual,
	                               CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
	                               CWBorderPixel | CWWinGravity | CWBitGravity, &xfc->attribs);
	window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR),
	                         (S_IREAD | S_IWRITE));

	if (window->shmid < 0)
	{
		DEBUG_X11("xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
	}
	else
	{
		void* mem;
		ftruncate(window->shmid, sizeof(window->handle));
		mem = mmap(0, sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
		           window->shmid, 0);

		if (mem == MAP_FAILED)
		{
			DEBUG_X11("xf_CreateDesktopWindow: failed to assign pointer to the memory address - shmat()\n");
		}
		else
		{
			window->xfwin = mem;
			*window->xfwin = window->handle;
		}
	}

	classHints = XAllocClassHint();

	if (classHints)
	{
		classHints->res_name = "xfreerdp";

		if (xfc->context.settings->WmClass)
			classHints->res_class = xfc->context.settings->WmClass;
		else
			classHints->res_class = "xfreerdp";

		XSetClassHint(xfc->display, window->handle, classHints);
		XFree(classHints);
	}

	xf_ResizeDesktopWindow(xfc, window, width, height);
	xf_SetWindowDecorations(xfc, window->handle, window->decorations);
	xf_SetWindowPID(xfc, window->handle, 0);
	input_mask =
	    KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
	    VisibilityChangeMask | FocusChangeMask | StructureNotifyMask |
	    PointerMotionMask | ExposureMask | PropertyChangeMask;

	if (xfc->grab_keyboard)
		input_mask |= EnterWindowMask | LeaveWindowMask;

	XChangeProperty(xfc->display, window->handle, xfc->_NET_WM_ICON, XA_CARDINAL,
	                32,
	                PropModeReplace, (BYTE*) xf_icon_prop, ARRAYSIZE(xf_icon_prop));

	if (parentWindow)
		XReparentWindow(xfc->display, window->handle, parentWindow, 0, 0);

	XSelectInput(xfc->display, window->handle, input_mask);
	XClearWindow(xfc->display, window->handle);
	xf_SetWindowTitleText(xfc, window->handle, name);
	XMapWindow(xfc->display, window->handle);
	xf_input_init(xfc, window->handle);

	/*
	 * NOTE: This must be done here to handle reparenting the window,
	 * so that we don't miss the event and hang waiting for the next one
	 */
	do
	{
		XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
	}
	while (xevent.type != VisibilityNotify);

	/*
	 * The XCreateWindow call will start the window in the upper-left corner of our current
	 * monitor instead of the upper-left monitor for remote app mode (which uses all monitors).
	 * This extra call after the window is mapped will position the login window correctly
	 */
	if (xfc->context.settings->RemoteApplicationMode)
	{
		XMoveWindow(xfc->display, window->handle, 0, 0);
	}
	else if (settings->DesktopPosX != UINT32_MAX && settings->DesktopPosY != UINT32_MAX)
	{
		XMoveWindow(xfc->display, window->handle, settings->DesktopPosX,
		            settings->DesktopPosY);
	}

	window->floatbar = xf_floatbar_new(xfc, window->handle, name, settings->Floatbar);
	return window;
}
示例#25
0
文件: mmap_sanity.c 项目: LLNL/lustre
static int mmap_tst8(char *mnt)
{
        char  fname[256];
        char *buf = MAP_FAILED;
        int fd = -1;
        int rc = 0;
        pid_t pid;
        char xyz[page_size * 2];

        if (snprintf(fname, 256, "%s/mmap_tst8", mnt) >= 256) {
                fprintf(stderr, "dir name too long\n");
                rc = -ENAMETOOLONG;
                goto out;
        }
        fd = open(fname, O_RDWR | O_CREAT, 0644);
        if (fd == -1) {
                perror("open");
                rc = -errno;
                goto out;
        }
        if (ftruncate(fd, page_size) == -1) {
                perror("truncate");
                rc = -errno;
                goto out;
        }
        buf = mmap(NULL, page_size * 2,
                   PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (buf == MAP_FAILED) {
                perror("mmap");
                rc = -errno;
                goto out;
        }

        pid = fork();
        if (pid == 0) { /* child */
                memcpy(xyz, buf, page_size * 2);
                /* shouldn't reach here. */
                exit(0);
        } else if (pid > 0) { /* parent */
                int status = 0;
                pid = waitpid(pid, &status, 0);
                if (pid < 0) {
                        perror("wait");
                        rc = -errno;
                        goto out;
                }

                rc = -EFAULT;
                if (WIFSIGNALED(status) && SIGBUS == WTERMSIG(status))
                        rc = 0;
        } else {
                perror("fork");
                rc = -errno;
        }

out:
        if (buf != MAP_FAILED)
                munmap(buf, page_size);
        if (fd != -1)
                close(fd);
        return rc;
}
示例#26
0
static PyObject *
mmap_resize_method(mmap_object *self,
                   PyObject *args)
{
    Py_ssize_t new_size;
    CHECK_VALID(NULL);
    if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
        !is_resizeable(self)) {
        return NULL;
    }
    if (new_size < 0 || PY_SSIZE_T_MAX - new_size < self->offset) {
        PyErr_SetString(PyExc_ValueError, "new size out of range");
        return NULL;
    }

    {
#ifdef MS_WINDOWS
        DWORD dwErrCode = 0;
        DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
        /* First, unmap the file view */
        UnmapViewOfFile(self->data);
        self->data = NULL;
        /* Close the mapping object */
        CloseHandle(self->map_handle);
        self->map_handle = NULL;
        /* Move to the desired EOF position */
        newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
        newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
        off_hi = (DWORD)(self->offset >> 32);
        off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
        SetFilePointer(self->file_handle,
                       newSizeLow, &newSizeHigh, FILE_BEGIN);
        /* Change the size of the file */
        SetEndOfFile(self->file_handle);
        /* Create another mapping object and remap the file view */
        self->map_handle = CreateFileMapping(
            self->file_handle,
            NULL,
            PAGE_READWRITE,
            0,
            0,
            self->tagname);
        if (self->map_handle != NULL) {
            self->data = (char *) MapViewOfFile(self->map_handle,
                                                FILE_MAP_WRITE,
                                                off_hi,
                                                off_lo,
                                                new_size);
            if (self->data != NULL) {
                self->size = new_size;
                Py_INCREF(Py_None);
                return Py_None;
            } else {
                dwErrCode = GetLastError();
                CloseHandle(self->map_handle);
                self->map_handle = NULL;
            }
        } else {
            dwErrCode = GetLastError();
        }
        PyErr_SetFromWindowsErr(dwErrCode);
        return NULL;
#endif /* MS_WINDOWS */

#ifdef UNIX
#ifndef HAVE_MREMAP
        PyErr_SetString(PyExc_SystemError,
                        "mmap: resizing not available--no mremap()");
        return NULL;
#else
        void *newmap;

        if (self->fd != -1 && ftruncate(self->fd, self->offset + new_size) == -1) {
            PyErr_SetFromErrno(PyExc_OSError);
            return NULL;
        }

#ifdef MREMAP_MAYMOVE
        newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
#else
#if defined(__NetBSD__)
        newmap = mremap(self->data, self->size, self->data, new_size, 0);
#else
        newmap = mremap(self->data, self->size, new_size, 0);
#endif /* __NetBSD__ */
#endif
        if (newmap == (void *)-1)
        {
            PyErr_SetFromErrno(PyExc_OSError);
            return NULL;
        }
        self->data = newmap;
        self->size = new_size;
        Py_INCREF(Py_None);
        return Py_None;
#endif /* HAVE_MREMAP */
#endif /* UNIX */
    }
}
示例#27
0
static int au_trunc(struct cw_filestream *fs)
{
    if (ftruncate(fileno(fs->f), ftell(fs->f)))
        return -1;
    return update_header(fs->f);
}
示例#28
0
static int send_request(char *method, const char *path, FILE *fp, xmlParserCtxtPtr xmlctx, curl_slist *extra_headers)
{
  char url[MAX_URL_SIZE];
  char *slash;
  long response = -1;
  int tries = 0;

  if (!storage_url[0])
  {
    debugf("send_request with no storage_url?");
    abort();
  }

  while ((slash = strstr(path, "%2F")) || (slash = strstr(path, "%2f")))
  {
    *slash = '/';
    memmove(slash+1, slash+3, strlen(slash+3)+1);
  }
  while (*path == '/')
    path++;
  snprintf(url, sizeof(url), "%s/%s", storage_url, path);

  // retry on failures
  for (tries = 0; tries < REQUEST_RETRIES; tries++)
  {
    CURL *curl = get_connection(path);
    if (rhel5_mode)
      curl_easy_setopt(curl, CURLOPT_CAINFO, RHEL5_CERTIFICATE_FILE);
    curl_slist *headers = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_HEADER, 0);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify_ssl);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, debug);
    add_header(&headers, "X-Auth-Token", storage_token);
    if (!strcasecmp(method, "MKDIR"))
    {
      curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
      curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0);
      add_header(&headers, "Content-Type", "application/directory");
    }
    else if (!strcasecmp(method, "PUT") && fp)
    {
      rewind(fp);
      curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
      curl_easy_setopt(curl, CURLOPT_INFILESIZE, cloudfs_file_size(fileno(fp)));
      curl_easy_setopt(curl, CURLOPT_READDATA, fp);
    }
    else if (!strcasecmp(method, "GET"))
    {
      if (fp)
      {
        rewind(fp); // make sure the file is ready for a-writin'
        fflush(fp);
        if (ftruncate(fileno(fp), 0) < 0)
        {
          debugf("ftruncate failed.  I don't know what to do about that.");
          abort();
        }
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
      }
      else if (xmlctx)
      {
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, xmlctx);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &xml_dispatch);
      }
    }
    else
      curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
    /* add the headers from extra_headers if any */
    curl_slist *extra;
    for (extra = extra_headers; extra; extra = extra->next)
    {
      debugf("adding header: %s", extra->data);
      headers = curl_slist_append(headers, extra->data);
    }
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
    curl_slist_free_all(headers);
    curl_easy_reset(curl);
    return_connection(curl);
    if (response >= 200 && response < 400)
      return response;
    sleep(8 << tries); // backoff
    if (response == 401 && !cloudfs_connect()) // re-authenticate on 401s
      return response;
    if (xmlctx)
      xmlCtxtResetPush(xmlctx, NULL, 0, NULL, NULL);
  }
  return response;
}
示例#29
0
int main(void)
{
	char tmpfname[256];
	long page_size;

	char *pa, ch;
	ssize_t len;
	int fd;

	pid_t child;
	int i, exit_val, ret, size;

	page_size = sysconf(_SC_PAGE_SIZE);

	/* mmap will create a partial page */
	len = page_size / 2;

	snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_11_5_%ld", (long)getpid());
	child = fork();
	switch (child) {
	case 0:
		/* Create shared object */
		shm_unlink(tmpfname);
		fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL,
			      S_IRUSR | S_IWUSR);
		if (fd == -1) {
			printf("Error at shm_open(): %s\n", strerror(errno));
			return PTS_UNRESOLVED;
		}
		if (ftruncate(fd, len) == -1) {
			printf("Error at ftruncate(): %s\n", strerror(errno));
			return PTS_UNRESOLVED;
		}

		pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
		if (pa == MAP_FAILED) {
			printf("Error at mmap(): %s\n", strerror(errno));
			return PTS_FAIL;
		}

		/* Check the patial page is ZERO filled */
		for (i = len; i < page_size; i++)
			if (pa[i] != 0) {
				printf("Test FAILED: The partial page at the "
				       "end of an object is not zero-filled\n");
				return PTS_FAIL;
			}

		/* Write the partial page */
		pa[len + 1] = 'b';
		munmap(pa, len);
		close(fd);
		return PTS_PASS;
	case -1:
		printf("Error at fork(): %s\n", strerror(errno));
		return PTS_UNRESOLVED;
	default:
		break;
	}

	wait(&exit_val);
	if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
		unlink(tmpfname);

		if (WIFEXITED(exit_val))
			return WEXITSTATUS(exit_val);

		printf("Child exited abnormally\n");
		return PTS_UNRESOLVED;
	}

	fd = shm_open(tmpfname, O_RDWR, 0);
	shm_unlink(tmpfname);

	size = 0;

	while ((ret = read(fd, &ch, 1)) == 1) {

		if (ch != 0) {
			printf("File is not zeroed\n");
			return PTS_FAIL;
		}

		size += ret;
	}

	if (ret == -1) {
		printf("Error at read(): %s\n", strerror(errno));
		return PTS_UNRESOLVED;
	}

	if (size != len) {
		printf("File has wrong size\n");
		return PTS_UNRESOLVED;
	}

	close(fd);

	printf("Test PASSED\n");
	return PTS_PASS;
}
示例#30
0
文件: sim_imd.c 项目: ProtoSD/simh
/* Format an entire track.  The new track to be formatted must be after any existing tracks on
 * the disk.
 *
 * This routine should be enhanced to re-format an existing track to the same format (this
 * does not involve changing the disk image size.)
 *
 * Any existing data on the disk image will be destroyed when Track 0, Head 0 is formatted.
 * At that time, the IMD file is truncated.  So for the trackWrite to be used to sucessfully
 * format a disk image, then format program must format tracks starting with Cyl 0, Head 0,
 * and proceed sequentially through all tracks/heads on the disk.
 *
 * Format programs that are known to work include:
 * Cromemco CDOS "INIT.COM"
 * ADC Super-Six (CP/M-80) "FMT8.COM"
 * 86-DOS "INIT.COM"
 *
 */
t_stat trackWrite(DISK_INFO *myDisk,
               uint32 Cyl,
               uint32 Head,
               uint32 numSectors,
               uint32 sectorLen,
               uint8 *sectorMap,
               uint8 mode,
               uint8 fillbyte,
               uint32 *flags)
{
    FILE *fileref;
    IMD_HEADER track_header;
    uint8 *sectorData;
    unsigned long i;
    unsigned long dataLen;

    *flags = 0;

    /* Check parameters */
    if(myDisk == NULL) {
        *flags |= IMD_DISK_IO_ERROR_GENERAL;
        return(SCPE_IOERR);
    }

    if(myDisk->flags & FD_FLAG_WRITELOCK) {
        printf("Disk write-protected, cannot format tracks." NLP);
        *flags |= IMD_DISK_IO_ERROR_WPROT;
        return(SCPE_IOERR);
    }

    fileref = myDisk->file;

    DBG_PRINT(("Formatting C:%d/H:%d/N:%d, len=%d, Fill=0x%02x" NLP, Cyl, Head, numSectors, sectorLen, fillbyte));

    /* Truncate the IMD file when formatting Cyl 0, Head 0 */
    if((Cyl == 0) && (Head == 0))
    {
        /* Skip over IMD comment field. */
        commentParse(myDisk, NULL, 0);

        /* Truncate the IMD file after the comment field. */
#ifdef _WIN32 /* This might work under UNIX and/or VMS since this POSIX, but I haven't tried it. */
        _chsize(_fileno(fileref), ftell (fileref));
#else
        if (ftruncate(fileno(fileref), ftell (fileref)) == -1) {
            printf("Disk truncation failed." NLP);
            *flags |= IMD_DISK_IO_ERROR_GENERAL;
            return(SCPE_IOERR);
        }
#endif
        /* Flush and re-parse the IMD file. */
        fflush(fileref);
        diskParse(myDisk, 0);
    }

    /* Check to make sure the Cyl / Head is not already formatted. */
    if(sectSeek(myDisk, Cyl, Head) == 0) {
        printf("SIM_IMD: ERROR: Not Formatting C:%d/H:%d, track already exists." NLP, Cyl, Head);
        *flags |= IMD_DISK_IO_ERROR_GENERAL;
        return(SCPE_IOERR);
    }

    track_header.mode = mode;
    track_header.cyl = Cyl;
    track_header.head = Head;
    track_header.nsects = numSectors;
    track_header.sectsize = sectorLen;

    /* Forward to end of the file, write track header and sector map. */
    sim_fseek(myDisk->file, 0, SEEK_END);
    sim_fwrite(&track_header, 1, sizeof(IMD_HEADER), fileref);
    sim_fwrite(sectorMap, 1, numSectors, fileref);

    /* Compute data length, and fill a sector buffer with the
     * sector record type as the first byte, and fill the sector
     * data with the fillbyte.
     */
    dataLen = (128 << sectorLen)+1;
    sectorData = malloc(dataLen);
    memset(sectorData, fillbyte, dataLen);
    sectorData[0] = SECT_RECORD_NORM;

    /* For each sector on the track, write the record type and sector data. */
    for(i=0;i<numSectors;i++) {
        sim_fwrite(sectorData, 1, dataLen, fileref);
    }

    /* Flush the file, and free the sector buffer. */
    fflush(fileref);
    free(sectorData);

    /* Now that the disk track/sector layout has been modified, re-parse the disk image. */
    diskParse(myDisk, 0);

    return(SCPE_OK);
}