コード例 #1
0
ファイル: stats.c プロジェクト: FeepingCreature/swig
/* zero all the stats structures */
void stats_zero(void)
{
	int dir, fd;
	unsigned i;
	char *fname;
	unsigned counters[STATS_END];

	x_asprintf(&fname, "%s/stats", cache_dir);
	unlink(fname);
	free(fname);

	for (dir=0;dir<=0xF;dir++) {
		x_asprintf(&fname, "%s/%1x/stats", cache_dir, dir);
		fd = safe_open(fname);
		if (fd == -1) {
			free(fname);
			continue;
		}
		memset(counters, 0, sizeof(counters));
		lock_fd(fd);
		stats_read_fd(fd, counters);
		for (i=0;stats_info[i].message;i++) {
			if (!(stats_info[i].flags & FLAG_NOZERO)) {
				counters[stats_info[i].stat] = 0;
			}
		}
		write_stats(fd, counters);
		close(fd);
		free(fname);
	}
}
コード例 #2
0
static mp_net_stream_packet_t* send_net_stream_cmd(stream_t *s,uint16_t cmd,char* data,int len) {
  mp_net_stream_packet_t* pack;

  // Cache is enabled : lock
  if(s->cache_data && !lock_fd(s->fd))
    return NULL;
  // Send a command
  if(!write_packet(s->fd,cmd,data,len)) {
    if(s->cache_data) unlock_fd(s->fd);
    return 0;
  }
  // Read the response
  pack = read_packet(s->fd);
  // Now we can unlock
  if(s->cache_data) unlock_fd(s->fd);

  if(!pack)
    return NULL;

  switch(pack->cmd) {
  case NET_STREAM_OK:
    return pack;
  case NET_STREAM_ERROR:
    if(pack->len > sizeof(mp_net_stream_packet_t))
      mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed: %s\n",pack->data);
    else
      mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed\n");
    free(pack);
    return NULL;
  }

  mp_msg(MSGT_STREAM,MSGL_ERR, "Unknown response to %d: %d\n",cmd,pack->cmd);
  free(pack);
  return NULL;
}
コード例 #3
0
ファイル: stats.c プロジェクト: FeepingCreature/swig
/* update the stats counter for this compile */
static void stats_update_size(enum stats stat, size_t size, size_t numfiles)
{
	int fd;
	unsigned counters[STATS_END];
	int need_cleanup = 0;

	if (getenv("CCACHE_NOSTATS")) return;

	if (!stats_file) {
		if (!cache_dir) return;
		x_asprintf(&stats_file, "%s/stats", cache_dir);
	}

	/* open safely to try to prevent symlink races */
	fd = safe_open(stats_file);

	/* still can't get it? don't bother ... */
	if (fd == -1) return;

	memset(counters, 0, sizeof(counters));

	if (lock_fd(fd) != 0) {
		close(fd);
		return;
	}

	/* read in the old stats */
	stats_read_fd(fd, counters);

	/* update them */
	counters[stat]++;

	/* on a cache miss we up the file count and size */
	if (stat == STATS_TOCACHE) {
		counters[STATS_NUMFILES] += numfiles;
		counters[STATS_TOTALSIZE] += size;
	}

	/* and write them out */
	write_stats(fd, counters);
	close(fd);

	/* we might need to cleanup if the cache has now got too big */
	if (counters[STATS_MAXFILES] != 0 &&
	    counters[STATS_NUMFILES] > counters[STATS_MAXFILES]) {
		need_cleanup = 1;
	}
	if (counters[STATS_MAXSIZE] != 0 &&
	    counters[STATS_TOTALSIZE] > counters[STATS_MAXSIZE]) {
		need_cleanup = 1;
	}

	if (need_cleanup) {
		char *p = dirname(stats_file);
		cleanup_dir(p, counters[STATS_MAXFILES], counters[STATS_MAXSIZE],
			    numfiles);
		free(p);
	}
}
コード例 #4
0
ファイル: lockfile.c プロジェクト: dieterdeyke/WAMPES
int lock_file(const char *filename, int exclusive, int dont_block)
{
  int fd;

  if ((fd = open(filename, O_RDWR | O_CREAT, 0644)) < 0)
    return -1;
  if (!lock_fd(fd, exclusive, dont_block))
    return fd;
  close(fd);
  return -1;
}
コード例 #5
0
ファイル: topten.c プロジェクト: clockfort/bingehack4
static void
update_log(const struct toptenentry *newtt)
{
    /* used for debugging (who dies of what, where) */
    int fd = open_datafile(LOGFILE, O_CREAT | O_APPEND | O_WRONLY, SCOREPREFIX);

    if (lock_fd(fd, 10)) {
        writeentry(fd, newtt);
        unlock_fd(fd);
        close(fd);
    }
}
コード例 #6
0
ファイル: stats.c プロジェクト: FeepingCreature/swig
/* read in the stats from one dir and add to the counters */
void stats_read(const char *stats_file, unsigned counters[STATS_END])
{
	int fd;

	fd = open(stats_file, O_RDONLY|O_BINARY);
	if (fd == -1) {
		stats_default(counters);
		return;
	}
	lock_fd(fd);
	stats_read_fd(fd, counters);
	close(fd);
}
コード例 #7
0
ファイル: topten.c プロジェクト: clockfort/bingehack4
static void
update_xlog(const struct toptenentry *newtt)
{
    /* used for statistical purposes and tournament scoring */
    int fd =
        open_datafile(XLOGFILE, O_CREAT | O_APPEND | O_WRONLY, SCOREPREFIX);
    if (lock_fd(fd, 10)) {
        FILE *xlfile = fdopen(fd, "a");

        write_xlentry(xlfile, newtt);
        unlock_fd(fd);
        fclose(xlfile); /* also closes fd */
    }
}
コード例 #8
0
ファイル: stream_sagetv.c プロジェクト: BOTCrusher/sagetv
int ReOpenConnection(stream_t* s)
{
//printf("Reopening connection\n");fflush(stdout);
    struct stream_priv_s* p = (struct stream_priv_s*)s->priv;
    if (!p->url) return 0;
	unlock_fd(p);
	close(s->fd);
	s->fd = 0;
	if (OpenConnection(s))
	{
		lock_fd(p);
		return 1;
	}
	return 0;
}
コード例 #9
0
ファイル: stats.c プロジェクト: FeepingCreature/swig
/* set the per directory limits */
int stats_set_limits(long maxfiles, long maxsize)
{
	int dir;
	unsigned counters[STATS_END];

	if (maxfiles != -1) {
		maxfiles /= 16;
	}
	if (maxsize != -1) {
		maxsize /= 16;
	}

	if (create_dir(cache_dir) != 0) {
		return 1;
	}

	/* set the limits in each directory */
	for (dir=0;dir<=0xF;dir++) {
		char *fname, *cdir;
		int fd;

		x_asprintf(&cdir, "%s/%1x", cache_dir, dir);
		if (create_dir(cdir) != 0) {
			return 1;
		}
		x_asprintf(&fname, "%s/stats", cdir);
		free(cdir);

		memset(counters, 0, sizeof(counters));
		fd = safe_open(fname);
		if (fd != -1) {
			lock_fd(fd);
			stats_read_fd(fd, counters);
			if (maxfiles != -1) {
				counters[STATS_MAXFILES] = maxfiles;
			}
			if (maxsize != -1) {
				counters[STATS_MAXSIZE] = maxsize;
			}
			write_stats(fd, counters);
			close(fd);
		}
		free(fname);
	}

	return 0;
}
コード例 #10
0
ファイル: stats.c プロジェクト: FeepingCreature/swig
/* set the per directory sizes */
void stats_set_sizes(const char *dir, size_t num_files, size_t total_size)
{
	int fd;
	unsigned counters[STATS_END];
	char *stats_file;

	create_dir(dir);
	x_asprintf(&stats_file, "%s/stats", dir);

	memset(counters, 0, sizeof(counters));

	fd = safe_open(stats_file);
	if (fd != -1) {
		lock_fd(fd);
		stats_read_fd(fd, counters);
		counters[STATS_NUMFILES] = num_files;
		counters[STATS_TOTALSIZE] = total_size;
		write_stats(fd, counters);
		close(fd);
	}

	free(stats_file);
}
コード例 #11
0
ファイル: topten.c プロジェクト: clockfort/bingehack4
/*
 * Add the result of the current game to the score list
 */
void
update_topten(int how)
{
    struct toptenentry *toptenlist, newtt;
    boolean need_rewrite;
    int fd;

    if (program_state.panicking)
        return;

    end_how = how;      /* save how for nh_get_topten */

    fill_topten_entry(&newtt, how);
    update_log(&newtt);
    update_xlog(&newtt);

    /* nothing more to do for non-scoring games */
    if (wizard || discover)
        return;

    fd = open_datafile(RECORD, O_RDWR | O_CREAT, SCOREPREFIX);
    if (!lock_fd(fd, 30)) {
        close(fd);
        return;
    }

    toptenlist = read_topten(fd, TTLISTLEN);

    /* possibly rearrange the score list to include the new entry */
    need_rewrite = toptenlist_insert(toptenlist, &newtt);
    if (need_rewrite)
        write_topten(fd, toptenlist);

    unlock_fd(fd);
    close(fd);
    free(toptenlist);
}
コード例 #12
0
ファイル: stream_sagetv.c プロジェクト: BOTCrusher/sagetv
static off_t size(struct stream_st *s, off_t *availSize)
{
    struct stream_priv_s* p = (struct stream_priv_s*)s->priv;
	char data[512];
	off_t otherAvail;
	if (!availSize)
		availSize = &otherAvail;
	sprintf(data, "SIZE\r\n");
	int dataSize = strlen(data);
	if (!lock_fd(p)) return 0;
//printf("Sending2 cmd to SageTV Server:%s\n", data);fflush(stdout);
	if (send(s->fd, data, dataSize, 0) < dataSize)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
//		printf("socket write failed, reopening...\n");
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}

	int nbytes = sockReadLine(s->fd, data, sizeof(data));
	if (nbytes < 0)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		sprintf(data, "SIZE\r\n");
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
		nbytes = sockReadLine(s->fd, data, sizeof(data));
		if (nbytes < 0)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}
//	printf("Read back %s\n", data);fflush(stdout);
	unlock_fd(p);
	char* spacePtr = strchr(data, ' ');
	if (!spacePtr)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		return 0;
	}
	*spacePtr = '\0';
	if (availSize)
		*availSize = strtoll(data, NULL, 10);
	off_t totalSize = strtoll(spacePtr + 1, NULL, 10);
//printf("avail=%I64d total=%I64d\n", *availSize, totalSize);fflush(stdout);
	if (*availSize != totalSize)
	{
		// Be sure the active file flag is set!
		if (!s->activeFileFlag)
		{
			if (s->cache_data)
			{
				cache_vars_t* sc = s->cache_data;
				sc->streamOriginal->activeFileFlag = 1;
			}
			s->activeFileFlag = 1;
		}
		active_file = 1;
	}
	else
	{
		// If we're going to turn it on we better turn it off as well!
		if (s->activeFileFlag)
		{
			if (s->cache_data)
			{
				cache_vars_t* sc = s->cache_data;
				sc->streamOriginal->activeFileFlag = 0;
			}
			s->activeFileFlag = 0;
		}
		active_file	= 0;
	}
	return totalSize;
}
コード例 #13
0
ファイル: lockfd.c プロジェクト: beyerservice/zmap
int lock_file(FILE *f)
{
	assert(f);
	return lock_fd(fileno(f));
}
コード例 #14
0
int main(int argc, char **argv) {

#line 3047 "ifupdown.nw"
    int (*cmds)(interface_defn *) = NULL;
#line 3141 "ifupdown.nw"
    struct option long_opts[] = {
        {"help",        no_argument,       NULL, 'h'},
        {"version",     no_argument,       NULL, 'V'},
        {"verbose",     no_argument,       NULL, 'v'},
        {"all",         no_argument,       NULL, 'a'},
        {"allow",	required_argument, NULL,  3 },
        {"interfaces",  required_argument, NULL, 'i'},
        {"exclude",     required_argument, NULL, 'e'},
        {"no-act",      no_argument,       NULL, 'n'},
        {"no-mappings", no_argument,       NULL,  1 },
        {"force",       no_argument,       NULL,  2 },
        {0,0,0,0}
    };
#line 3171 "ifupdown.nw"
    int do_all = 0;
    int run_mappings = 1;
    int force = 0;
    char *allow_class = NULL;
    char *interfaces = "/etc/network/interfaces";
    char *statefile = "/etc/network/run/ifstate";
    char *excludeint = NULL ;
#line 3338 "ifupdown.nw"
    interfaces_file *defn;
#line 3497 "ifupdown.nw"
    int n_target_ifaces;
    char **target_iface;
#line 3554 "ifupdown.nw"
    char **state = NULL; /* list of iface=liface */
    int n_state = 0;
    int max_state = 0;
#line 3628 "ifupdown.nw"
    static FILE *state_fp = NULL;

#line 2992 "ifupdown.nw"

#line 3016 "ifupdown.nw"
    {
        int i;
        for (i = 0; i <= 2; i++) {
            if (fcntl(i, F_GETFD) == -1) {
                if (errno == EBADF && open("/dev/null", 0) == -1) {
                    fprintf(stderr,
                            "%s: fd %d not available; aborting\n",
                            argv[0], i);
                    exit(2);
                } else if (errno == EBADF) {
                    errno = 0; /* no more problems */
                } else {
                    /* some other problem -- eeek */
                    perror(argv[0]);
                    exit(2);
                }
            }
        }
    }

#line 2994 "ifupdown.nw"

#line 3053 "ifupdown.nw"
    {
        char *command;


#line 3064 "ifupdown.nw"
        if ((command = strrchr(argv[0],'/'))) {
            command++; /* first char after / */
        } else {
            command = argv[0]; /* no /'s in argv[0] */
        }
#line 3057 "ifupdown.nw"

#line 3072 "ifupdown.nw"
        if (strcmp(command, "ifup")==0) {
            cmds = iface_up;
        } else if (strcmp(command, "ifdown")==0) {
            cmds = iface_down;
        } else {
            fprintf(stderr,"This command should be called as ifup or ifdown\n");
            exit(1);
        }
#line 3058 "ifupdown.nw"
    }
#line 2995 "ifupdown.nw"

#line 3234 "ifupdown.nw"
    for(;;) {
        int c;
        c = getopt_long(argc, argv, "e:s:i:hVvna", long_opts, NULL);
        if (c == EOF) break;

        switch(c) {

#line 3252 "ifupdown.nw"
        case 'i':
            interfaces = strdup(optarg);
            break;
#line 3257 "ifupdown.nw"
        case 'v':
            verbose = 1;
            break;
#line 3262 "ifupdown.nw"
        case 'a':
            do_all = 1;
            break;
#line 3267 "ifupdown.nw"
        case 3:
            allow_class = strdup(optarg);
            break;
#line 3272 "ifupdown.nw"
        case 'n':
            no_act = 1;
            break;
#line 3277 "ifupdown.nw"
        case 1:
            run_mappings = 0;
            break;
#line 3282 "ifupdown.nw"
        case 2:
            force = 1;
            break;
#line 3287 "ifupdown.nw"
        case 'e':
            excludeint = strdup(optarg);
            break;
#line 3295 "ifupdown.nw"
        case 'h':
            help(argv[0]);
            break;
#line 3300 "ifupdown.nw"
        case 'V':
            version(argv[0]);
            break;
#line 3309 "ifupdown.nw"
        default:
            usage(argv[0]);
            break;
#line 3241 "ifupdown.nw"
        }
    }

#line 3321 "ifupdown.nw"
    if (argc - optind > 0 && do_all) {
        usage(argv[0]);
    }
#line 3327 "ifupdown.nw"
    if (argc - optind == 0 && !do_all) {
        usage(argv[0]);
    }

#line 2997 "ifupdown.nw"

#line 3342 "ifupdown.nw"
    defn = read_interfaces(interfaces);
    if ( !defn ) {
        fprintf(stderr, "%s: couldn't read interfaces file \"%s\"\n",
                argv[0], interfaces);
        exit(1);
    }

#line 2999 "ifupdown.nw"

#line 3632 "ifupdown.nw"
    {
        state_fp = fopen(statefile, no_act ? "r" : "a+");
        if (state_fp == NULL && !no_act) {
            fprintf(stderr,
                    "%s: failed to open statefile %s: %s\n",
                    argv[0], statefile, strerror(errno));
            exit (1);
        }

        if (state_fp != NULL) {
            char buf[80];
            char *p;

            if (!no_act) {
                int flags;

                if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
                        || fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
                    fprintf(stderr,
                            "%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
                            argv[0], statefile, strerror(errno));
                    exit(1);
                }

                if (lock_fd (fileno(state_fp)) < 0) {
                    fprintf(stderr,
                            "%s: failed to lock statefile %s: %s\n",
                            argv[0], statefile, strerror(errno));
                    exit(1);
                }

            }

            rewind (state_fp);
            while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
                char *pch;

                pch = buf + strlen(buf) - 1;
                while(pch > buf && isspace(*pch)) pch--;
                *(pch+1) = '\0';

                pch = buf;
                while(isspace(*pch)) pch++;

                add_to_state(&state, &n_state, &max_state, strdup(pch));
            }
        }
    }
#line 3508 "ifupdown.nw"
    if (do_all) {
        if (
#line 3087 "ifupdown.nw"
            (cmds == iface_up)
#line 3509 "ifupdown.nw"
        ) {
            allowup_defn *autos = find_allowup(defn, "auto");
            target_iface = autos ? autos->interfaces : NULL;
            n_target_ifaces = autos ? autos->n_interfaces : 0;
        } else if (
#line 3091 "ifupdown.nw"
            (cmds == iface_down)
#line 3513 "ifupdown.nw"
        ) {
            target_iface = state;
            n_target_ifaces = n_state;
        } else {
            assert(0);
        }
    } else {
        target_iface = argv + optind;
        n_target_ifaces = argc - optind;
    }
#line 3357 "ifupdown.nw"
    {
        int i;
        for (
#line 3502 "ifupdown.nw"
            i = 0; i < n_target_ifaces; i++
#line 3359 "ifupdown.nw"
        ) {
            char iface[80], liface[80];


#line 3526 "ifupdown.nw"
            strncpy(iface, target_iface[i], sizeof(iface));
            iface[sizeof(iface)-1] = '\0';

            {
                char *pch;
                if ((pch = strchr(iface, '='))) {
                    *pch = '\0';
                    strncpy(liface, pch+1, sizeof(liface));
                    liface[sizeof(liface)-1] = '\0';
                } else {
                    strncpy(liface, iface, sizeof(liface));
                    liface[sizeof(liface)-1] = '\0';
                }
            }
#line 3363 "ifupdown.nw"
            if (!force) {

#line 3715 "ifupdown.nw"
                {
                    int already_up = lookfor_iface(state, n_state, iface);;

                    if (
#line 3087 "ifupdown.nw"
                        (cmds == iface_up)
#line 3718 "ifupdown.nw"
                    ) {
                        if (already_up != -1) {
                            fprintf(stderr,
                                    "%s: interface %s already configured\n",
                                    argv[0], iface);
                            continue;
                        }
                    } else if (
#line 3091 "ifupdown.nw"
                        (cmds == iface_down)
#line 3725 "ifupdown.nw"
                    ) {
                        if (already_up == -1) {
                            fprintf(stderr, "%s: interface %s not configured\n",
                                    argv[0], iface);
                            continue;
                        }
                        strncpy(liface, strchr(state[already_up], '=') + 1, 80);
                        liface[79] = 0;
                    } else {
                        assert(0);
                    }
                }
#line 3365 "ifupdown.nw"
            }

            if (
#line 3100 "ifupdown.nw"
                (allow_class != NULL)

#line 3367 "ifupdown.nw"
            ) {

#line 3103 "ifupdown.nw"
                {
                    int i;
                    allowup_defn *allowup = find_allowup(defn, allow_class);
                    if (allowup == NULL)
                        continue;

                    for (i = 0; i < allowup->n_interfaces; i++) {
                        if (strcmp(allowup->interfaces[i], iface) == 0)
                            break;
                    }
                    if (i >= allowup->n_interfaces)
                        continue;
                }
#line 3369 "ifupdown.nw"
            }

            if (
#line 3125 "ifupdown.nw"
                (excludeint != NULL && strstr(iface,excludeint) != NULL)
#line 3371 "ifupdown.nw"
            )
                continue;

            if (
#line 3087 "ifupdown.nw"
                (cmds == iface_up)
#line 3374 "ifupdown.nw"
                && run_mappings) {

#line 3392 "ifupdown.nw"
                {
                    mapping_defn *currmap;
                    for (currmap = defn->mappings; currmap; currmap = currmap->next) {
                        int i;
                        for (i = 0; i < currmap->n_matches; i++) {

#line 3413 "ifupdown.nw"
                            if (fnmatch(currmap->match[i], liface, 0) != 0)
                                continue;
#line 3398 "ifupdown.nw"

#line 3421 "ifupdown.nw"
                            if (verbose) {
                                fprintf(stderr, "Running mapping script %s on %s\n",
                                        currmap->script, liface);
                            }
                            run_mapping(iface, liface, sizeof(liface), currmap);
#line 3399 "ifupdown.nw"
                            break;
                        }
                    }
                }
#line 3376 "ifupdown.nw"
            }


#line 3431 "ifupdown.nw"
            {
                interface_defn *currif;
                int okay = 0;
                int failed = 0;
                for (currif = defn->ifaces; currif; currif = currif->next) {
                    if (strcmp(liface, currif->logical_iface) == 0) {
                        okay = 1;


#line 3457 "ifupdown.nw"
                        {
                            currif->real_iface = iface;

                            if (verbose) {
                                fprintf(stderr, "Configuring interface %s=%s (%s)\n",
                                        iface, liface, currif->address_family->name);
                            }

                            switch(cmds(currif)) {
                            case -1:
                                printf("Don't seem to be have all the variables for %s/%s.\n",
                                       liface, currif->address_family->name);
                                failed = 1;
                                break;
                            case 0:
                                failed = 1;
                                break;
                            /* not entirely successful */
                            case 1:
                                failed = 0;
                                break;
                            /* successful */
                            default:
                                printf("Internal error while configuring interface %s/%s (assuming it failed)\n",
                                       liface, currif->address_family->name);
                                failed = 1;
                                /* what happened here? */
                            }
                            currif->real_iface = NULL;
                        }

#line 3441 "ifupdown.nw"
                        if (failed) break;
                        /* Otherwise keep going: this interface may have
                         * match with other address families */
                    }
                }

                if (!okay && !force) {
                    fprintf(stderr, "Ignoring unknown interface %s=%s.\n",
                            iface, liface);
                } else {

#line 3743 "ifupdown.nw"
                    {
                        int already_up = lookfor_iface(state, n_state, iface);

                        if (
#line 3087 "ifupdown.nw"
                            (cmds == iface_up)
#line 3746 "ifupdown.nw"
                        ) {
                            char *newiface =
                                malloc(strlen(iface) + 1 + strlen(liface) + 1);
                            sprintf(newiface, "%s=%s", iface, liface);

                            if (already_up == -1) {
                                if (failed == 1) {
                                    printf("Failed to bring up %s.\n", liface);
                                } else {
                                    add_to_state(&state, &n_state, &max_state, newiface);
                                }
                            } else {
                                free(state[already_up]);
                                state[already_up] = newiface;
                            }
                        } else if (
#line 3091 "ifupdown.nw"
                            (cmds == iface_down)
#line 3761 "ifupdown.nw"
                        ) {
                            if (already_up != -1) {
                                state[already_up] = state[--n_state];
                            }
                        } else {
                            assert(0);
                        }
                    }
#line 3452 "ifupdown.nw"
                }
            }
#line 3379 "ifupdown.nw"

#line 3691 "ifupdown.nw"
            if (state_fp != NULL && !no_act) {
                int i;

                if (ftruncate(fileno(state_fp), 0) < 0)
                {
                    fprintf(stderr,
                            "%s: failed to truncate statefile %s: %s\n",
                            argv[0], statefile, strerror(errno));
                    exit(1);
                }

                rewind(state_fp);
                for (i = 0; i < n_state; i++) {
                    fprintf(state_fp, "%s\n", state[i]);
                }
                fflush(state_fp);
            }
#line 3380 "ifupdown.nw"
        }
    }
#line 3683 "ifupdown.nw"
    if (state_fp != NULL) {
        fclose(state_fp);
        state_fp = NULL;
    }

#line 3001 "ifupdown.nw"
    return 0;
}
コード例 #15
0
ファイル: pam_pwdfile.c プロジェクト: Kafay/libpam-pwdfile
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
				   int argc, const char **argv) {
    int i;
    const char *name;
    char const * password;
    char const * pwdfilename = NULL;
    char const * stored_crypted_password = NULL;
    char const * crypted_password;
    FILE *pwdfile;
    int use_flock = 0;
    int use_delay = 1;
    int legacy_crypt = 0;
    int debug = 0;
    char * linebuf = NULL;
    size_t linebuflen;
#ifdef USE_CRYPT_R
    struct crypt_data crypt_buf;
#endif
    
    /* we require the pwdfile switch and argument to be present, else we don't work */
    for (i = 0; i < argc; ++i) {
	if (!strcmp(argv[i], "pwdfile") && i + 1 < argc)
	    pwdfilename = argv[++i];
	else if (!strncmp(argv[i], "pwdfile=", strlen("pwdfile=")))
	    pwdfilename = argv[i] + strlen("pwdfile=");
	else if (!strcmp(argv[i], "flock"))
	    use_flock = 1;
	else if (!strcmp(argv[i], "noflock"))
	    use_flock = 0;
	else if (!strcmp(argv[i], "nodelay"))
	    use_delay = 0;
	else if (!strcmp(argv[i], "debug"))
	    debug = 1;
	else if (!strcmp(argv[i], "legacy_crypt"))
	    legacy_crypt = 1;
    }
    
#ifdef HAVE_PAM_FAIL_DELAY
    if (use_delay) {
	if (debug) pam_syslog(pamh, LOG_DEBUG, "setting fail delay");
	(void) pam_fail_delay(pamh, 2000000);   /* 2 sec */
    }
#endif
    
    if (!pwdfilename) {
	pam_syslog(pamh, LOG_ERR, "password file name not specified");
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    if (pam_get_user(pamh, &name, NULL) != PAM_SUCCESS) {
	pam_syslog(pamh, LOG_ERR, "couldn't get username from PAM stack");
	return PAM_AUTH_ERR;
    }
    if (debug) pam_syslog(pamh, LOG_DEBUG, "username is %s", name);
    
    if (!(pwdfile = fopen(pwdfilename, "r"))) {
	pam_syslog(pamh, LOG_ALERT, "couldn't open password file %s", pwdfilename);
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    if (use_flock && lock_fd(fileno(pwdfile)) == -1) {
	pam_syslog(pamh, LOG_ALERT, "couldn't lock password file %s", pwdfilename);
	fclose(pwdfile);
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    /* get the crypted password corresponding to this user out of pwdfile */
    while (getline(&linebuf, &linebuflen, pwdfile) > 0) {
	/* strsep changes its argument, make a copy */
	char * nexttok = linebuf;
	
	/* first field: username */
	char * curtok = strsep(&nexttok, ":");
	
	/* skip non-matching usernames */
	if (strcmp(curtok, name))
	    continue;
	
	/* second field: password (until next colon or newline) */
	if ((curtok = strsep(&nexttok, ":\n"))) {
	    stored_crypted_password = curtok;
	    break;
	}
    }
    fclose(pwdfile);
    /* we keep linebuf (allocated by getline), stored_crypted_password is pointing into it */

    if (!stored_crypted_password)
	if (debug) pam_syslog(pamh, LOG_ERR, "user not found in password database");
    
    if (stored_crypted_password && !strlen(stored_crypted_password)) {
	if (debug) pam_syslog(pamh, LOG_DEBUG, "user has empty password field");
	free(linebuf);
	return flags & PAM_DISALLOW_NULL_AUTHTOK ? PAM_AUTH_ERR : PAM_SUCCESS;
    }
    
    if (pam_get_authtok(pamh, PAM_AUTHTOK, &password, NULL) != PAM_SUCCESS) {
	pam_syslog(pamh, LOG_ERR, "couldn't get password from PAM stack");
	free(linebuf);
	return PAM_AUTH_ERR;
    }
    
    if (!stored_crypted_password) {
	free(linebuf);
	return PAM_USER_UNKNOWN;
    }
    
    if (debug) pam_syslog(pamh, LOG_DEBUG, "got crypted password == '%s'", stored_crypted_password);
    
#ifdef USE_CRYPT_R
    crypt_buf.initialized = 0;
    if (!(crypted_password = crypt_r(password, stored_crypted_password, &crypt_buf)))
#else
    if (!(crypted_password = crypt(password, stored_crypted_password)))
#endif
    {
	pam_syslog(pamh, LOG_ERR, "crypt() failed");
	free(linebuf);
	return PAM_AUTH_ERR;
    }
    
    if (legacy_crypt && strcmp(crypted_password, stored_crypted_password)) {
	if (!strncmp(stored_crypted_password, "$1$", 3))
	    crypted_password = Brokencrypt_md5(password, stored_crypted_password);
	else
	    crypted_password = bigcrypt(password, stored_crypted_password);
    }

    if (strcmp(crypted_password, stored_crypted_password)) {
	pam_syslog(pamh, LOG_NOTICE, "wrong password for user %s", name);
	free(linebuf);
	return PAM_AUTH_ERR;
    }
    
    if (debug) pam_syslog(pamh, LOG_DEBUG, "passwords match");
    free(linebuf);
    return PAM_SUCCESS;
}
コード例 #16
0
ファイル: stream_sagetv.c プロジェクト: BOTCrusher/sagetv
static int fill_buffer(stream_t *s, char* pbBuffer, int max_len)
{
    struct stream_priv_s* p = (struct stream_priv_s*)s->priv;

	// Check on EOS condition
	if (s->activeFileFlag)
		p->actualSize = 0;
	if (!p->actualSize && !s->activeFileFlag)
	{
		// Get the actual size and store it
		p->actualSize = size(s, NULL);
	}
	if (p->actualSize && !s->activeFileFlag)
	{
		if (max_len + s->pos > p->actualSize)
		{
			max_len = p->actualSize - s->pos;
			if (max_len < 0) max_len = 0;
		}
	}
	
	if (max_len <= 0) return 0;
	char data[512];
	lock_fd(p);
#ifndef WIN32
	sprintf(data, "READ %lld %d\r\n", s->pos, max_len);
#else
	sprintf(data, "READ %I64d %d\r\n", s->pos, max_len);
#endif
	int dataSize = strlen(data);
//printf("Sending cmd to SageTV Server:%s\n", data);fflush(stdout);
	if (send(s->fd, data, dataSize, 0) < dataSize)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		// Try to do it again...
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}

	int nbytes;
	char* pOriginalBuffer = pbBuffer;
	int originaldwBytesToRead = max_len;
	int bytesRead = 0;
	nbytes = recv(s->fd, (char*)pbBuffer, max_len, 0);
	while (nbytes >= 0 && max_len > 0)
	{
		max_len -= nbytes;
		pbBuffer += nbytes;
		bytesRead += nbytes;
		if (max_len > 0)
			nbytes = recv(s->fd, (char*)pbBuffer, max_len, 0);
	}
	if (nbytes < 0)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
		bytesRead = 0;
		pbBuffer = pOriginalBuffer;
		max_len = originaldwBytesToRead;
		nbytes = recv(s->fd, (char*)pbBuffer, max_len, 0);
		while (nbytes >= 0 && max_len > 0)
		{
			max_len -= nbytes;
			pbBuffer += nbytes;
			bytesRead += nbytes;
			if (max_len > 0)
				nbytes = recv(s->fd, (char*)pbBuffer, max_len, 0);
		}
		if (nbytes < 0)
		{
//printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}
	unlock_fd(p);
//printf("Read %d bytes from network\n", bytesRead);fflush(stdout);
	//s->pos += bytesRead; // for mplayer this is handled externally to us
	// Check in case the server's flipped the active file flag and we're not an integrated player
	if (s->activeFileFlag)
	{
		p->actualSize = size(s, NULL);
		if (s->activeFileFlag)
			p->actualSize = 0;
		else
		{
//printf("Active file flag flipped!!!\n");fflush(stdout);
			// Flag got flipped, make sure we don't have overage!
			bytesRead = min(bytesRead, p->actualSize - s->pos);
		}
	}
	return bytesRead;
}
コード例 #17
0
ファイル: stream_sagetv.c プロジェクト: BOTCrusher/sagetv
static int seek(stream_t *s,off_t newpos)
{
  struct stream_priv_s* p = (struct stream_priv_s*)s->priv;
	if (newpos >= 0 && s->activeFileFlag)
	{
		s->pos = newpos;
		return 1;
	}
	if (!lock_fd(p))
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		return 0;
	}
	char data[512];
	sprintf(data, "SIZE\r\n");
	int dataSize = strlen(data);
//printf("Sending cmd to SageTV Server:%s", data);fflush(stdout);
	if (send(s->fd, data, dataSize, 0) < dataSize)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}

	int nbytes = sockReadLine(s->fd, data, sizeof(data));
	if (nbytes < 0)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		if (!ReOpenConnection(s))
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			return 0;
		}
		sprintf(data, "SIZE\r\n");
		if (send(s->fd, data, dataSize, 0) < dataSize)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
		nbytes = sockReadLine(s->fd, data, sizeof(data));
		if (nbytes < 0)
		{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
			unlock_fd(p);
			return 0;
		}
	}
	unlock_fd(p);
	char* spacePtr = strchr(data, ' ');
	if (!spacePtr)
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		return 0;
	}
	*spacePtr = '\0';
	off_t availSize = strtoll(data, NULL, 10);
	if (newpos >= 0 && (newpos < availSize || s->activeFileFlag))
	{
		s->pos = newpos;
		return 1;
	}
	else
	{
printf("FAILURE %d\n", __LINE__);fflush(stdout);
		return 0;

	}
}
コード例 #18
0
/* expected hook for auth service */
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
				   int argc, const char **argv) {
    int retval, pcnt, pwdfilename_found;
    const char *name;
    char *password;
    char pwdfilename[PWDFN_LEN];
    char salt[12], stored_crypted_password[CRYPTED_BCPWD_LEN+1];
    char *crypted_password;
    FILE *pwdfile;
    int use_flock = 0;
    int use_delay = 1;
    int temp_result = 0;
    
    /* we require the pwdfile switch and argument to be present, else we don't work */
    /* pcnt is the parameter counter variable for iterating through argv */
    pcnt = pwdfilename_found = 0;
    do {
	/* see if the current parameter looks like "pwdfile" */
	if (strcmp(argv[pcnt],PWDF_PARAM)==0) {
	    /* if argv is long enough, grab the subsequent parameter */
	    if (pcnt+1 < argc) {
		/* make sure we can't overflow */
		strncpy(pwdfilename,argv[++pcnt],PWDFN_LEN);
		/* indicate that we've found it */
		pwdfilename_found = 1;
	    }
	    /* also check for "pwdfile=blah" */
	} else if (strncmp(argv[pcnt],PWDF_PARAM "=",sizeof(PWDF_PARAM "=")-1)==0) {
	    /* make sure we can't overflow */
	    strncpy(pwdfilename,argv[pcnt]+sizeof(PWDF_PARAM),PWDFN_LEN);
	    /* indicate that we've found it */
	    pwdfilename_found = 1;
	} else if (strcmp(argv[pcnt],FLOCK_PARAM)==0) {
	    /* we have a "flock" parameter */
	    use_flock = 1;
	} else if (strcmp(argv[pcnt],"no" FLOCK_PARAM)==0) {
	    /* or a "noflock" parameter */
	    use_flock = 0;
	} else if (strcmp(argv[pcnt],NODELAY_PARAM)==0) {
	    /* no delay on authentication failure */
	    use_delay = 0;
	}
	
    } while (++pcnt < argc);
    
#ifdef HAVE_PAM_FAIL_DELAY
    if (use_delay) {
	D(("setting delay"));
	(void) pam_fail_delay(pamh, 2000000);   /* 2 sec delay for on failure */
    }
#endif
    
    /* for some or other reason, the password file wasn't specified */
    if (!pwdfilename_found) {
	_pam_log(LOG_ERR,"password file name not specified");
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    /* DEBUG */
    D(_pam_log(LOG_ERR, "password filename extracted"));
    
    /* now try to open the password file */
    if ((pwdfile=fopen(pwdfilename,"r"))==NULL) {
	_pam_log(LOG_ERR,"couldn't open password file %s",pwdfilename);
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    /* set a lock on the password file */
    if (use_flock && lock_fd(fileno(pwdfile)) == -1) {
	_pam_log(LOG_ERR,"couldn't lock password file %s",pwdfilename);
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    /* get user name */
    if ((retval = pam_get_user(pamh,&name,"login: "******"username not found");
	fclose(pwdfile);
	return retval;
    }
    
    
    /* DEBUG */
    D(_pam_log(LOG_ERR,"username is %s", name));
    
    /* get password - code from pam_unix_auth.c */
    pam_get_item(pamh, PAM_AUTHTOK, (void *)&password);
    if (!password) {
	retval = _set_auth_tok(pamh, flags, argc, argv);
	if (retval!=PAM_SUCCESS) {
	    fclose(pwdfile);
	    return retval;
	}
    }
    pam_get_item(pamh, PAM_AUTHTOK, (void *)&password);
    
    if ((retval = pam_get_item(pamh, PAM_AUTHTOK, (void *)&password)) != PAM_SUCCESS) {
	_pam_log(LOG_ERR, "auth token not found");
	fclose(pwdfile);
	return retval;
    }
    
    /* DEBUG */
    D(_pam_log(LOG_ERR,"got password from user", password));
    
    /* now crypt password and compare to the user entry in the password file */
    /* first make sure password is long enough -- may I do this? */
    if (strlen(password)<2 || password==NULL) {
	_pam_log(LOG_ERR,"password too short or NULL");
	fclose(pwdfile);
	return PAM_AUTH_ERR;
    }
    
    /* get the crypted password corresponding to this user */
    if (!fgetpwnam(pwdfile, name, stored_crypted_password)) {
	_pam_log(LOG_ERR,"user not found in password database");
	fclose(pwdfile);
	return PAM_AUTHINFO_UNAVAIL;
    }
    
    /* DEBUG */
    D(_pam_log(LOG_ERR,"got crypted password == '%s'", stored_crypted_password));
    
    
    temp_result = 0;
    
    /* Extract the salt and set the passwd length, depending on MD5 or DES */
    if (strncmp(stored_crypted_password, "$1$", 3) == 0) {
	D(_pam_log(LOG_ERR,"password hash type is 'md5'"));
	/* get out the salt into "salt" */
	strncpy(salt, stored_crypted_password, 11);
	salt[11] = '\0';
	stored_crypted_password[CRYPTED_MD5PWD_LEN] = '\0';
	/* try both md5 crypts */
	crypted_password = Goodcrypt_md5(password, salt);
	if (strcmp(crypted_password, stored_crypted_password) == 0)
	{
	    temp_result = 1;
	}
	else
	{
	    crypted_password = Brokencrypt_md5(password, salt);
	    if (strcmp(crypted_password, stored_crypted_password) == 0)
	    {
		temp_result = 1;
	    }
	}
    } else {
	/* get the salt out into "salt" */
	strncpy(salt, stored_crypted_password, 2);
	salt[2] = '\0';
	stored_crypted_password[CRYPTED_BCPWD_LEN] = '\0';

	if (strlen(stored_crypted_password) <= CRYPTED_DESPWD_LEN) {
	    D(_pam_log(LOG_ERR,"password hash type is 'crypt'"));
	    crypted_password = crypt(password, salt);
	} else {
	    D(_pam_log(LOG_ERR,"password hash type is 'bigcrypt'"));
	    crypted_password = bigcrypt(password, salt);
	}

	if (strcmp(crypted_password, stored_crypted_password) == 0)
	{
	    temp_result = 1;
	}
    }
    
    /* DEBUG */
    D(_pam_log(LOG_ERR,"user password crypted is '%s'", crypted_password));
    
    /* if things don't match up, complain */
    if (!temp_result) 
    {
	_pam_log(LOG_ERR,"wrong password for user %s",name);
	fclose(pwdfile);
	return PAM_AUTH_ERR;
    }
    
    /* DEBUG */
    D(_pam_log(LOG_ERR,"passwords match"));
    
    /* we've gotten here, i.e. authentication was sucessful! */
    fclose(pwdfile);
    return PAM_SUCCESS;
}
コード例 #19
0
ファイル: lock.c プロジェクト: oldprogs/fidogate
/*
 * lock_file() --- lock file using FILE *
 */
int lock_file(FILE *fp)
{
    return lock_fd( fileno(fp) );
}