Пример #1
0
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) {
    int core;
    int ret;
    cpu_set_t mask;
    CPU_ZERO(&mask);

    ret = old_pthread_create(thread, attr, start_routine, arg);

    if(!get_shm()->active)
        return ret;

    core = get_next_core();

    if(!get_shm()->per_node) {
        CPU_SET(core, &mask);
    } else {
        int i, node = numa_node_of_cpu(core);
        struct bitmask * bmp = numa_allocate_cpumask();
        numa_node_to_cpus(node, bmp);
        for(i = 0; i < numa_num_configured_cpus(); i++) {
            if(numa_bitmask_isbitset(bmp, i))
                CPU_SET(i, &mask);
        }
        numa_free_cpumask(bmp);
    }
    old_pthread_setaffinity_np(*thread, sizeof(mask), &mask);

    VERBOSE("-> Set affinity to %d\n", core);

    return ret;
}
Пример #2
0
pid_t fork(void) {
    pid_t ret;

    // When a new process is forked, the refcounter must be incremented
    pthread_mutex_lock(&get_shm()->pin_lock);
    get_shm()->refcount++;
    pthread_mutex_unlock(&get_shm()->pin_lock);

    ret = old_fork();
    if(ret > 0) {
        set_affinity(ret, get_next_core());
    }

    return ret;
}
Пример #3
0
int main(int argc, char const *argv[])
{
	printf("S4 started\n");
	get_shm();
	while(1)
	{
		char *buf=(char*)malloc(BUF_SIZE);
		int ret=read(0,buf,BUF_SIZE);
		if(ret==0)
		{
			cnt[3]++;
			break;
		}
		int i=0,j=strlen(buf)-2,flag=1;
		while(j>i)
		{
			if(buf[i]!=buf[j])
			{
				flag=0;
				break;
			}
			i++;
			j--;
		}
		if(flag==0)
			write(1,"It is not palindrome\n",strlen("It is not palindrome\n"));
		else
			write(1,"It is palindrome\n",strlen("It is palindrome\n"));
		sleep(1);
	}
	return 0;
}
Пример #4
0
pid_t fork(void) {
   pid_t ret;

   // Increment refcount on fork to avoid parent dying before child and destroying the shm
   __sync_fetch_and_add(&get_shm()->refcount, 1);

   ret = old_fork();
   if(ret > 0) {
      set_affinity(ret, get_next_core());
   } else if (ret < 0) {
       // fork failed, decrement
       __sync_fetch_and_sub(&get_shm()->refcount, 1);
   }       

   return ret;
}
Пример #5
0
int32_t main(int32_t argc, char *argv[]) {
    const int32_t SIZE = 128;

    int32_t fd = get_shm(MEMORY_NAME);
    if (fd < 0) {
        std::cout << "Error opening shared memory\n";
        exit(EXIT_FAILURE);
    }

    std::cout << "Truncating\n";
    ftruncate(fd, SIZE);

    std::cout << "Mapping to shared memory\n";
    uint8_t* shared_memory = (uint8_t*)mmap(NULL, MEMORY_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
    std::cout << "Shared address " << (uint64_t)shared_memory << "\n";

    std::cout << "Clearing memory\n";
    memset(shared_memory, 0, MEMORY_SIZE);
    std::cout << "About to write\n";
    uint8_t cnt = 0;
    while(true) {
        for(int32_t index = 0; index < MEMORY_SIZE; index++) {
            shared_memory[index] = cnt;
        }
        cnt++;
        usleep (250 * 1000); // 250 ms
    }

    munmap(shared_memory, SIZE);

    return 0;
}
Пример #6
0
/* 
 * do some tests on the shared memory from the Rtd image header and data
 * The arguments should be the shared memory Ids.
 */
static void test_shm(int data_id, int header_id)
{
    void* data = get_shm(data_id);
    char* header = (char*)get_shm(header_id);
    char buf[81];
    int i, j, n, w, h, bitpix;
    char* p;
    short* rawimage;
    
    if (header) {
	printf("got image header (%d):\n", header_id);
	p = header;
	for(i = 0; i < 10; i++) {
	    for (j=0; j<80; j++) {
		printf("%c", *p++);
	    }
	    printf("\n");
	}
	printf("...\n");
    }

    if (data) {
	printf("got image data (%d)\n", data_id);
	
	/* do something to the data... */
	w = atoi(send_rtd("width"));
	h = atoi(send_rtd("height"));
	bitpix = atoi(send_rtd("bitpix"));

	if (bitpix == 16) {
	    printf("modifying 'short' raw data (mult by 2):\n");
	    rawimage = (short*)data;
	    for(i = 0; i < w; i++) {
		for (j=0; j<h; j++) {
		    rawimage[j*h+i] *= 2;
		}
	    }
	    /* cause rtd to update the display */
	    send_rtd("shm update");
	}
    }
}
Пример #7
0
static void set_affinity(pid_t tid, int cpu_id) {
    if(!get_shm()->active)
        return;

    if(!get_shm()->per_node) {
        cpu_set_t mask;
        CPU_ZERO(&mask);
        CPU_SET(cpu_id, &mask);
        VERBOSE("--> Setting tid %d on core %d\n", tid, cpu_id);
        int r = old_sched_setaffinity(tid, sizeof(mask), &mask);
        if (r < 0) {
            fprintf(stderr, "couldn't set affinity on %d\n", cpu_id);
            exit(1);
        }
    } else {
        int r = numa_run_on_node(numa_node_of_cpu(cpu_id));
        if(r < 0) {
            fprintf(stderr, "couldn't set affinity on node of cpu %d\n", cpu_id);
            exit(1);
        }
    }
}
Пример #8
0
static int seize_index(int index, int size, char *proc)
{
	int mode = 0, key_base = 0;
	print(LOG_INFO, "Seizing index %d with %s\n", index, proc);
	proc_entry *entry = get_proc_at_index(index);
	/* setting up ctr area for my index */
	key_base = index * 4 + SEM_CTRL_KEY;
	entry->key_shm = key_base + 1;
	entry->key_rlock = key_base + 2;
	entry->key_wlock = key_base + 3;
	entry->key_active = key_base + 4;
	entry->size_shm = size;
	entry->sent = 0;
	entry->received = 0;
	my_index = index;

	/* map up the cache (mem_entry) */
	if ((mem_entry[index].active = create_lock(entry->key_active, 0)) == -1) {
		return -1;
	}
	/* signal active */
	if (set_active(mem_entry[index].active)) {
		return -1;
	}

	strncpy(entry->proc_name, proc, PROC_NAME_SIZE - 1);
	memcpy(mem_entry[index].proc_name, entry->proc_name, PROC_NAME_SIZE);

	if ((mem_entry[index].rlock = create_lock(entry->key_rlock, 0)) == -1) {
		return -1;
	}
	print(LOG_INFO, "Seize rlock for %s at index %d, key %d, sem %d\n",
	      entry->proc_name, index, entry->key_rlock,
	      mem_entry[index].rlock);

	if ((mem_entry[index].wlock = create_lock(entry->key_wlock, 1)) == -1) {
		return -1;
	}
	print(LOG_INFO, "Seize wlock for %s at index %d, key %d, sem %d\n",
	      entry->proc_name, index, entry->key_wlock,
	      mem_entry[index].wlock);

	if ((mem_entry[index].shm =
	     (char *)get_shm(entry->key_shm, entry->size_shm, &mode)) == 0) {
		return -1;
	}
	print(LOG_INFO, "Seize shm for %s at index %d, key %d, sem %p\n",
	      entry->proc_name, index, entry->key_shm, mem_entry[index].shm);

	return 0;
}
Пример #9
0
int decpry(char *ibuf, int len)
{
        struct sys_key *pskey = get_shm();
        struct sys_key skey;
        memcpy(&skey, pskey, sizeof(struct sys_key));
        AES_KEY dec_key;
        unsigned char dec_out[len];
        memset(dec_out, 0, len);

        AES_set_decrypt_key(skey.key, KEYLEN*8, &dec_key);
        AES_cbc_encrypt(ibuf, dec_out, len, &dec_key, skey.iv_dec,
                                                AES_DECRYPT);

        memcpy(ibuf, dec_out, len);
        return 0;
}
Пример #10
0
int main()
{
    int size = 1000;
    int shm_id = get_shm(size);
    sleep(5);
    char* mem = (char*)attach(shm_id);
    memset(mem,'\0',size);
    int index = 0;
    int count = 10;
    while(count--){
        mem[index++] = 'A';
        mem[index] = '\0';
        sleep(1);
    }
    detach(mem);
    return 0;
}
Пример #11
0
int main(int argc, char const *argv[])
{
	int i;
	int shm_id;
	pid_t pid;
	char *map_buf1;
	char *map_buf2;

	shm_id = get_shm();                      // 创建一个共享内存段,并返回一个id标识符
	pid = fork();

	if(pid < 0)
	{
		perror("Create New Process Error");
		exit(EXIT_FAILURE);
	}else
	if(pid == 0)
	{
		/*在子进程中*/
		map_buf1 = at_shm(shm_id);          // 将共享内存段连接到子进程中,并使map_buf1指向共享内存段
		i = 0;
		while(i < _SHM_SIZE_)
		{
			map_buf1[i] = '8';
			i++;
		}

		map_buf1[_SHM_SIZE_-1] = '\0';
		rm_shm(map_buf1);                  // 分离子进程与共享内存
	}else
	{
		/*在父进程中*/
		map_buf2 = at_shm(shm_id);         // 将共享内存连接到父进程中,并使map_buf2指向共享内存段
		sleep(5);
		printf("In Parent, read Share Memory: %s\n", map_buf2);
		rm_shm(map_buf2);                  // 分离父进程与共享内存

		waitpid(pid, NULL, 0);             // 等待子进程结束
		delete_shm(shm_id);                // 删除共享内存
	}

	return 0;
}
Пример #12
0
int encpry(char *ibuf, int len)
{
        struct sys_key *pskey = get_shm();
        struct sys_key skey;
        memcpy(&skey, pskey, sizeof(struct sys_key));
        AES_KEY enc_key;

        const size_t encslength = len%AES_BLOCK_SIZE?
                (len/AES_BLOCK_SIZE+1) * AES_BLOCK_SIZE: len;
        unsigned char enc_out[encslength+1];

        AES_set_encrypt_key(skey.key, KEYLEN*8, &enc_key);
        AES_cbc_encrypt(ibuf, enc_out, len, &enc_key, skey.iv_enc,
                        AES_ENCRYPT);

        memcpy(ibuf, enc_out, encslength);
        ibuf[encslength] = 0;
        return encslength;
}
Пример #13
0
static int		lemipc_bsc(int no_team, char *path)
{
  t_lemipc		lemipc;

  if (no_team < 1 || no_team > 255)
    return (lemipc_error(NO_TEAM_ERROR));
  if ((lemipc.key = ftok(path, 0)) == -1)
    return (lemipc_perror(FTOK_ERROR));
  lemipc.no_team = no_team;
  lemipc.pos = -1;
  lemipc.killed_by = 0;
  lemipc.is_alone = TRUE;
  lemipc.is_dead = FALSE;
  if (get_shm(&lemipc) == EXIT_FAILURE ||
      get_sem(&lemipc) == EXIT_FAILURE ||
      get_msgq(&lemipc) == EXIT_FAILURE ||
      launch_player(&lemipc) == EXIT_FAILURE)
    return (EXIT_FAILURE);
  return (EXIT_SUCCESS);
}
Пример #14
0
int main()
{
	int _size = 1000;
	int shm_id = get_shm(_size);
	printf("sleep(5)\n");
	sleep(5);
	char* mem = (char*)attach(shm_id);
	memset(mem,'\0',_size);
	int index = 0;
	//多进程往共享内存里面写数据,没有互斥访问共享内存,可以结合信号量机制互斥访问
	int count = 60;
	while(count--)
	{//可以再起一个client进行写,增加挂接共享内存的数目,命令:ipcs -m查看挂接数目
		mem[index] = 'X';
		mem[index+1] = '\0';
		index++;
		sleep(1);
	}
	detach(mem);
	printf("detach\n");
	return 0;
}
Пример #15
0
static int update_cache(int index, proc_entry * entry)
{
	int mode = 0;
	print(LOG_INFO, "Updating cache for %s at index %d\n", entry->proc_name,
	      index);
	if ((mem_entry[index].rlock = create_lock(entry->key_rlock, 0)) == -1) {
		print(LOG_ERR, "Unable to create rlock\n");
		return -1;
	}
	print(LOG_INFO, "Cache rlock for %s at index %d, key %d, sem %d\n",
	      entry->proc_name, index, entry->key_rlock,
	      mem_entry[index].rlock);

	if ((mem_entry[index].wlock = create_lock(entry->key_wlock, 0)) == -1) {
		print(LOG_ERR, "Unable to create wlock\n");
		return -1;
	}
	print(LOG_INFO, "Cache wlock for %s at index %d, key %d, sem %d\n",
	      entry->proc_name, index, entry->key_wlock,
	      mem_entry[index].wlock);

	if ((mem_entry[index].shm =
	     (char *)get_shm(entry->key_shm, entry->size_shm, &mode)) == 0) {
		print(LOG_ERR, "Unable to map shmc\n");
		return -1;
	}
	print(LOG_INFO, "Cache shm for %s at index %d, key %d, sem &p\n",
	      entry->proc_name, index, entry->key_shm, mem_entry[index].shm);

	if ((mem_entry[index].active = create_lock(entry->key_active, 0)) == -1) {
		print(LOG_ERR, "Unable to create  active lock\n");
		return -1;
	}
	memcpy(mem_entry[index].proc_name, entry->proc_name, PROC_NAME_SIZE);
	return 0;
}
Пример #16
0
main(int argc, char *argv[]) {
    char appname[128]="";

    strcpy(fsthaprogname,argv[0]);
    mypid=getpid();
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);
    setsid();
    umask(077);

    if ( argc != 2 || atoi(argv[1]) < 0 || atoi(argv[1]) > 1 ) {
        sprintf(msg,"Usage:  %s  <lh#app#>\n", argv[0]);
        die(msg);
    }

    strcpy(appname,argv[1]);

    if (get_shm(0) != 0)
        die("Failed to Attach");


    if ( strcmp(appname,"lh0app0") == 0 )  {
        sprintf(msg,"fstastlh0app0_pid %d",mypid);
        fsthahere(msg);
        fstast(0,0,&(shm->lh0node),&(shm->lh0app0stat),shm->lh0app0remtest,&(shm->lh0app0remnode),shm->lh0app0test,&(shm->lh0app0testres), &(shm->lh0app0crit),shm->lh0app0name,&(shm->lh0app0testtimeo),shm->lh0app0secs,shm->lh0app0pause);
    }
    else if ( strcmp(appname,"lh0app1") == 0 ) {
        sprintf(msg,"fstastlh0app1_pid %d",mypid);
        fsthahere(msg);
        fstast(0,1,&(shm->lh0node),&(shm->lh0app1stat),shm->lh0app1remtest,&(shm->lh0app1remnode),shm->lh0app1test,&(shm->lh0app1testres), &(shm->lh0app1crit),shm->lh0app1name,&(shm->lh0app1testtimeo),shm->lh0app1secs,shm->lh0app1pause);
    }
    else if ( strcmp(appname,"lh0app2") == 0 ) {
        sprintf(msg,"fstastlh0app2_pid %d",mypid);
        fsthahere(msg);
        fstast(0,2,&(shm->lh0node),&(shm->lh0app2stat),shm->lh0app2remtest,&(shm->lh0app2remnode),shm->lh0app2test,&(shm->lh0app2testres), &(shm->lh0app2crit),shm->lh0app2name,&(shm->lh0app2testtimeo),shm->lh0app2secs,shm->lh0app2pause);
    }
    else if ( strcmp(appname,"lh0app3") == 0 ) {
        sprintf(msg,"fstastlh0app3_pid %d",mypid);
        fsthahere(msg);
        fstast(0,3,&(shm->lh0node),&(shm->lh0app3stat),shm->lh0app0remtest,&(shm->lh0app3remnode),shm->lh0app3test,&(shm->lh0app3testres), &(shm->lh0app3crit),shm->lh0app3name,&(shm->lh0app3testtimeo),shm->lh0app3secs,shm->lh0app3pause);
    }


    else if ( strcmp(appname,"lh1app0") == 0 )  {
        sprintf(msg,"fstastlh1app0_pid %d",mypid);
        fsthahere(msg);
        fstast(1,0,&(shm->lh1node),&(shm->lh1app0stat),shm->lh1app0remtest,&(shm->lh1app0remnode),shm->lh1app0test,&(shm->lh1app0testres), &(shm->lh1app0crit),shm->lh1app0name,&(shm->lh1app0testtimeo),shm->lh1app0secs,shm->lh1app0pause);
    }
    else if ( strcmp(appname,"lh1app1") == 0 ) {
        sprintf(msg,"fstastlh1app1_pid %d",mypid);
        fsthahere(msg);
        fstast(1,1,&(shm->lh1node),&(shm->lh1app1stat),shm->lh1app1remtest,&(shm->lh1app1remnode),shm->lh1app1test,&(shm->lh1app1testres), &(shm->lh1app1crit),shm->lh1app1name,&(shm->lh1app1testtimeo),shm->lh1app1secs,shm->lh1app1pause);
    }
    else if ( strcmp(appname,"lh1app2") == 0 ) {
        sprintf(msg,"fstastlh1app2_pid %d",mypid);
        fsthahere(msg);
        fstast(1,2,&(shm->lh1node),&(shm->lh1app2stat),shm->lh1app2remtest,&(shm->lh1app2remnode),shm->lh1app2test,&(shm->lh1app2testres), &(shm->lh1app2crit),shm->lh1app2name,&(shm->lh1app2testtimeo),shm->lh1app2secs,shm->lh1app2pause);
    }
    else if ( strcmp(appname,"lh1app3") == 0 ) {
        sprintf(msg,"fstastlh1app3_pid %d",mypid);
        fsthahere(msg);
        fstast(1,3,&(shm->lh1node),&(shm->lh1app3stat),shm->lh1app3remtest,&(shm->lh1app3remnode),shm->lh1app3test,&(shm->lh1app3testres), &(shm->lh1app3crit),shm->lh1app3name,&(shm->lh1app3testtimeo),shm->lh1app3secs,shm->lh1app3pause);
    }


    else if ( strcmp(appname,"lh2app0") == 0 )  {
        sprintf(msg,"fstastlh2app0_pid %d",mypid);
        fsthahere(msg);
        fstast(2,0,&(shm->lh2node),&(shm->lh2app0stat),shm->lh2app0remtest,&(shm->lh2app0remnode),shm->lh2app0test,&(shm->lh2app0testres), &(shm->lh2app0crit),shm->lh2app0name,&(shm->lh2app0testtimeo),shm->lh2app0secs,shm->lh2app0pause);
    }
    else if ( strcmp(appname,"lh2app1") == 0 ) {
        sprintf(msg,"fstastlh2app1_pid %d",mypid);
        fsthahere(msg);
        fstast(2,1,&(shm->lh2node),&(shm->lh2app1stat),shm->lh2app1remtest,&(shm->lh2app1remnode),shm->lh2app1test,&(shm->lh2app1testres), &(shm->lh2app1crit),shm->lh2app1name,&(shm->lh2app1testtimeo),shm->lh2app1secs,shm->lh2app1pause);
    }
    else if ( strcmp(appname,"lh2app2") == 0 ) {
        sprintf(msg,"fstastlh2app2_pid %d",mypid);
        fsthahere(msg);
        fstast(2,2,&(shm->lh2node),&(shm->lh2app2stat),shm->lh2app2remtest,&(shm->lh2app2remnode),shm->lh2app2test,&(shm->lh2app2testres), &(shm->lh2app2crit),shm->lh2app2name,&(shm->lh2app2testtimeo),shm->lh2app2secs,shm->lh2app2pause);
    }
    else if ( strcmp(appname,"lh2app3") == 0 ) {
        sprintf(msg,"fstastlh2app3_pid %d",mypid);
        fsthahere(msg);
        fstast(2,3,&(shm->lh2node),&(shm->lh2app3stat),shm->lh2app3remtest,&(shm->lh2app3remnode),shm->lh2app3test,&(shm->lh2app3testres), &(shm->lh2app3crit),shm->lh2app3name,&(shm->lh2app3testtimeo),shm->lh2app3secs,shm->lh2app3pause);
    }


    else if ( strcmp(appname,"lh3app0") == 0 )  {
        sprintf(msg,"fstastlh3app0_pid %d",mypid);
        fsthahere(msg);
        fstast(3,0,&(shm->lh3node),&(shm->lh3app0stat),shm->lh3app0remtest,&(shm->lh3app0remnode),shm->lh3app0test,&(shm->lh3app0testres), &(shm->lh3app0crit),shm->lh3app0name,&(shm->lh3app0testtimeo),shm->lh3app0secs,shm->lh3app0pause);
    }
    else if ( strcmp(appname,"lh3app1") == 0 ) {
        sprintf(msg,"fstastlh3app1_pid %d",mypid);
        fsthahere(msg);
        fstast(3,1,&(shm->lh3node),&(shm->lh3app1stat),shm->lh3app1remtest,&(shm->lh3app1remnode),shm->lh3app1test,&(shm->lh3app1testres), &(shm->lh3app1crit),shm->lh3app1name,&(shm->lh3app1testtimeo),shm->lh3app1secs,shm->lh3app1pause);
    }
    else if ( strcmp(appname,"lh3app2") == 0 ) {
        sprintf(msg,"fstastlh3app2_pid %d",mypid);
        fsthahere(msg);
        fstast(3,2,&(shm->lh3node),&(shm->lh3app2stat),shm->lh3app2remtest,&(shm->lh3app2remnode),shm->lh3app2test,&(shm->lh3app2testres), &(shm->lh3app2crit),shm->lh3app2name,&(shm->lh3app2testtimeo),shm->lh3app2secs,shm->lh3app2pause);
    }
    else if ( strcmp(appname,"lh3app3") == 0 ) {
        sprintf(msg,"fstastlh3app3_pid %d",mypid);
        fsthahere(msg);
        fstast(3,3,&(shm->lh3node),&(shm->lh3app3stat),shm->lh3app3remtest,&(shm->lh3app3remnode),shm->lh3app3test,&(shm->lh3app3testres), &(shm->lh3app3crit),shm->lh3app3name,&(shm->lh3app3testtimeo),shm->lh3app3secs,shm->lh3app3pause);
    }

    sprintf(msg,"%s %s is Down.",basename(fsthaprogname),argv[1]);
    logit(msg);
}
Пример #17
0
int
main(int argc, char *argv[])
{
	struct cgi_applet *a = NULL;
	struct rlimit rl;
	int i;
	seteuid(BBSUID);
	setuid(BBSUID);
	setgid(BBSGID);
	cgi_time(NULL);
	rl.rlim_cur = 20 * 1024 * 1024;
	rl.rlim_max = 40 * 1024 * 1024;
	setrlimit(RLIMIT_CORE, &rl);
	thispid = getpid();
	now_t = time(NULL);
	srand(now_t * 2 + thispid);
	wwwcache = get_shm(WWWCACHE_SHMKEY, sizeof (struct WWWCACHE));
	if (NULL == wwwcache)
		exit(0);
	thisversion = file_time(argv[0]);
	if (thisversion > wwwcache->www_version)
		wwwcache->www_version = thisversion;
	html_header(0);
	if (geteuid() != BBSUID)
		http_fatal("uid error.");
	chdir(BBSHOME);
	shm_init();
	if (ummap())
		http_fatal("mmap error.");
	signal(SIGTERM, wantquit);
	if (access("NOLOGIN", F_OK))
		nologin = 0;
	get_att_server();
	while (FCGI_Accept() >= 0) {
//              start_outcache();
		cginame = NULL;
		incgiloop = 1;
		if (setjmp(cgi_start)) {
//                      end_outcache();
			cgi_time(a);
			if (!incgiloop || wwwcache->www_version > thisversion
			    || rt++ > 40000) {
				logtimeused();
				exit(2);
			}
			incgiloop = 0;
			continue;
		}
		html_header(0);
		now_t = time(NULL);
		via_proxy = 0;
		strsncpy(fromhost, getsenv("REMOTE_ADDR"), 46); //ipv6 by leoncom
		inet_pton(PF_INET6,fromhost,&from_addr);
		//inet_aton(fromhost, &from_addr);
		/*  ipv6 by leoncom 无视validproxy
		for (i = 0; wwwcache->validproxy[i] && i < MAX_PROXY_NUM; i++) {
			if (from_addr.s_addr == wwwcache->validproxy[i]) {
				via_proxy = 1;
				break;
			}
		}
		if (via_proxy) {
			char *ptr, *p;
			int IPLEN = 255;
			ptr = getenv("HTTP_X_FORWARDED_FOR");
			if (!ptr)
				ptr = getsenv("REMOTE_ADDR");
			p = strrchr(ptr, ',');
			if (p != NULL) {
				while (!isdigit(*p) && *p)
					p++;
				if (*p)
					strncpy(fromhost, p, IPLEN);
				else
					strncpy(fromhost, ptr, IPLEN);
			} else
				strncpy(fromhost, ptr, IPLEN);
			fromhost[IPLEN] = 0;
			inet_aton(fromhost, &from_addr);
		}
		*/
		if (url_parse())
			http_fatal("%s 没有实现的功能!", getsenv("SCRIPT_URL"));
		http_parm_init();
		a = get_cgi_applet(needcgi);
		if (a != NULL) {
			cginame = a->name[0];
			//access(getsenv("QUERY_STRING"), F_OK);
			wwwcache->www_visit++;
			(*(a->main)) ();
//                      end_outcache();
			cgi_time(a);
			if (!incgiloop || wwwcache->www_version > thisversion) {
				logtimeused();
				exit(4);
			}
			incgiloop = 0;
			continue;
		}
		http_fatal("%s 没有实现的功能!", getsenv("SCRIPT_URL"));
//              end_outcache();
		incgiloop = 0;
	}
	munmap(ummap_ptr, ummap_size);
	exit(5);


}
Пример #18
0
int main(){

  int shmd, semd, fd, SHM_KEY, SEM_KEY;

  SHM_KEY = ftok("makefile", 1);
  SEM_KEY = ftok("makefile", 2);

  shmd = shmget(SHM_KEY, sizeof(int), 0644);
  semd = semget(SEM_KEY, 1, 0644);

  //frankly, I don't know if the semaphore is serving / could potentially serve any purpose based on the way I wrote this
  //really should get around to testing that
  if(!down(semd)){

    //get size of last line
    int size = get_shm(shmd);
    char buffer[size+1];

    printf("==================\n\n");//
    
    printf("size of buffer: %lu bytes\n\n",sizeof(buffer));

    printf("buffer at creation: ");//
    print(buffer,size);//

    buffer[size] = '\0';

    printf("buffer after terminating null added: ");//
    print(buffer,size);//

    //display the last line added to the file
    int i;
    fd = open("story.txt", O_RDWR);
    i = fd;//
    printf("open returns %d\n",i);//
    i = lseek(fd, (-1)*size, SEEK_END);//
    printf("lseek returns %d\n",i);//
    i = read(fd, buffer, size);//keep this one
    printf("read returns %d\n",i);//

    printf("buffer after attempting to read: ");//
    print(buffer,size);//

    if(i == -1){

      printf("read error: %s\n", strerror(errno));

    }else{

      printf("==================\n");//
      printf("The story so far:\n");
      printf("%s ...\n\n", buffer);
      //printf("Cannot figure out why the above won't print out as a char array.  In any case, running \"./control --remove\" will correctly print out all the contents of the file.  But this is kind of driving me crazy.\n\n");

      //prompt the user to add another line (max 256 characters)
      printf("Add a new line (max 256 characters): ");
      char new_line[256];
      fgets(new_line, sizeof(new_line), stdin);
      new_line[strlen(new_line) - 1] = ' ';
      new_line[strlen(new_line)] = '\0';
      size = strlen(new_line);
      /* printf("new size (including space character): %d\n", size); */

      //append the new line to the file
      lseek(fd, 0, SEEK_END); //shouldn't be necessary, but read() isn't working up above so this is necessary
      write(fd, new_line, size);

    }
    close(fd);

    //update shared memory
    set_shm(shmd, size);
    /* print_shm(shmd); */

    //release the semaphore
    up(semd);

    return 0;

  }else{
    printf("semaphore error: %s (make sure you've already run \"./control --create\")\n", strerror(errno));
    return -1;
  }
  
}
Пример #19
0
int main(int argc, char const *argv[])
{
	init();
	get_shm();
	int ret,i;
	for(i=0;i<No_of_Ports;i++)
	{
		sfd[i]=socket(AF_INET,SOCK_STREAM,0);
		if (sfd[i]==-1)
		{
			printf("Socket Error : %d %d\n",i, errno);
			exit(1);
		}

		struct sockaddr_in saddr;
		memset(&saddr, 0, sizeof(struct sockaddr_in));
		saddr.sin_family=AF_INET;
		saddr.sin_port=htons(S[i].port);
		saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
		int addr_len=sizeof(struct sockaddr_in);

		ret=bind(sfd[i],(struct sockaddr*)&saddr,sizeof(saddr));	
		if (ret==-1)
		{
			printf("Bind Error : %d %d\n", i,errno);
			exit(1);
		}

		ret=listen(sfd[i],S[i].listen);
		if (ret==-1)
		{
			printf("Listen Error : %d %d\n",i, errno);
			exit(1);
		}
	}
	int caddr_len;
	i=0;
	printf("Port : 8889 Service :Capital to Small.\n");
	printf("Port : 8888 Service :Small to Capital.\n");
	printf("Port : 8887 Service :Reverse the String.\n");
	printf("Port : 8886 Service :Check For palindrome.\n");
	while(1)
	{
		if(i==0)
			print_status();
		struct timeval tv;
	    tv.tv_sec = 1;
	    tv.tv_usec = 0;
	    fd_set tr;
	    FD_ZERO(&tr);
	    FD_SET(sfd[i],&tr);
	    int res=select(sfd[i]+1,&tr,NULL,NULL,&tv);
	    if(res>0&&FD_ISSET(sfd[i],&tr))
	    {
			struct sockaddr_in caddr;
			memset(&caddr, 0, sizeof(struct sockaddr_in));
			int nsfd=accept(sfd[i],(struct sockaddr*)&caddr,&caddr_len);
			if(cnt[i]==0)
			{
				send(nsfd,"-1",strlen("-1"),0);
				close(nsfd);
			}
			else
			{
				send(nsfd,"1",strlen("1"),0);
				cnt[i]--;
				if(nsfd==-1)
				{
					printf("Accept Error : %d\n", errno);
					exit(1);
				}	
				printf("Connection Accepted one more Clinet On Port Number : %d\n", S[i].port);
				int c;
				c=fork();
				if(c==0)
				{
					dup2(nsfd,0);
					dup2(nsfd,1);
					execl(S[i].service,S[i].service,NULL);
				}
				else
				{
					//close(nsfd);
				}
			}
		}
		i=(i+1)%No_of_Ports;
	}
	return 0;
}
Пример #20
0
int init_memshare(char *proc_name, int size, int qsize)
{
	int ctrl_mode = 1;
	int retvalue = 0, index;
	print(LOG_INFO, "Init_memshare start for %s with size %d\n", proc_name,
	      size);

	if (initialized)
		return 1;
	/* a source proc is a must */
	if (proc_name == NULL)
		return 2;

	memset(my_proc, 0, PROC_NAME_SIZE);
	strncpy(my_proc, proc_name, PROC_NAME_SIZE - 1);

	/* If I don't set a qsize I'm considered to be a send proc only */
	if (size)
		send_only = 0;

	if (!send_only) {
		init_queues();
		seize_queue(&queue_index, "memshare", qsize);
	}

	/* clear the cache */
	init_mem_proc();

	/* start off by locking the ctrl lock */
	if ((lock_ctrl_sem = create_lock(SEM_CTRL_KEY, 1)) == -1) {
		print(LOG_ERR, "Unable to create ctrl lock\n");
		return 3;
	}

	while (lock(lock_ctrl_sem) < 0) ;
	print(LOG_DEBUG, "Ctrl locked (init) by %s, %d\n\n", proc_name,
	      lock_ctrl_sem);
	/*print(LOG_ERR, "%d trylock (init) key=%d, sem=%d\n",
	   try_lock1(lock_ctrl_sem), SEM_CTRL_KEY, lock_ctrl_sem); */

	/* map up the ctrl area */
	if ((shm_ctrl_ptr = get_shm(SHM_CTRL_KEY, CTRL_SIZE, &ctrl_mode)) == 0) {
		print(LOG_ERR, "Unable to alloc shared mem\n");
		while (unlock(lock_ctrl_sem) < 0) ;
		return 6;
	}

	if (get_index_for_proc(my_proc) != -1) {
		print(LOG_ERR, "Procname %s already exists\n", my_proc);
		while (unlock(lock_ctrl_sem) < 0) ;
		return 4;
	}

	if (!send_only) {

		if ((index = get_first_free()) < 0) {
			while (unlock(lock_ctrl_sem) < 0) ;
			print(LOG_ERR, "Max num of processes registered\n");
			return 4;
		}

		print(LOG_DEBUG, "Next free index is %d\n", index);

		retvalue = seize_index(index, size, my_proc);

		if (retvalue == -1) {
			while (unlock(lock_ctrl_sem) < 0) ;
			return 6;
		}
	} else {
		print(LOG_INFO, "%s is a send only proc\n", my_proc);
	}

	print(LOG_DEBUG, "Ctrl unlocked by %s\n\n", proc_name);
	while (unlock(lock_ctrl_sem) < 0) ;

	if (!send_only)
		start_listen_thread();
	print(LOG_DEBUG, "Init_memshare done for %s\n", my_proc);
	initialized = 1;
	return 0;
}