Exemple #1
0
int main(int argc, char **argv)
{
    redisContext *c = redisConnect("localhost", 6379);
    redisReply *reply;
    redisLock lock;
    char *keyname = "lock";

    if (argc > 1)
        keyname = argv[1];


    if (c == NULL || c->err) {
        if (c) {
            printf("Connection error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
        exit(1);
    }


    if (redis_lock_init_context(c, &reply)) {
        fprintf(stderr, "redis_locker_init() failed: error=%s\n",
                reply? reply->str: "unknown");
        if (reply)
            freeReplyObject(reply);
        redisFree(c);
        return -1;
    }


    if (redis_lock_init(&lock, keyname, -1)) {
        printf("redis_lock_init(): failed\n");
        exit(1);
    }

    reply = redisCommand(c, "DEL counter");
    check_reply(reply);
    freeReplyObject(reply);

    redis_lock_set_data(&lock, "counter", -1);

    while (1) {
        double t0, t1;
        int i;

        t0 = gettime();
        for (i = 0; i < 10000; i++) {
            redisReply *data_reply;
            int ret;
            long counter = 0;
            char buf[100];
            int len;

            while (1) {
                redis_lock_set_time(&lock, gettime(), .1);
                ret = redis_lock_acquire_data(&lock, c, &data_reply);

                if (ret > 0)
                    break;
                if (ret != REDIS_LOCK_STATE_BUSY) {
                    fprintf(stderr, "oops\n");
                    exit(1);
                }
                usleep(100);
            }

            if (data_reply->str)
                counter = atoi(data_reply->str);
            freeReplyObject(data_reply);

            len = snprintf(buf, sizeof(buf), "%ld", counter + 1);

            ret = redis_lock_release_data(&lock, c, buf, len);
            if (ret != 1)
                fprintf(stderr, "unlock script returned %d\n", ret);
        }
        t1 = gettime();

        printf("%s: %.3lf locks/sec\n", argv[0], i / (t1 - t0));
    }

    return 0;
}
Exemple #2
0
int main(int argc, char **argv) {
	memset(broadcast_addr.ether_addr_octet, -1, ETH_ALEN);

	struct device iface;
	struct in_addr ipaddr;
	struct ether_addr macaddr = broadcast_addr;
	struct timeval timeout = { 1, 0 };

	int ch;
	while ((ch = getopt(argc, argv, "w:m:")) != -1) {
		switch (ch) {
		case 'w':
			errno = 0;
			char *remains;
			double dbl = strtod(optarg, &remains);

			if (errno || *remains != 0 || dbl < 0) {
				fprintf(stderr, "Invalid timeout '%s'\n", optarg);
				return ERR_ARGS;
			}

			double intpart, fractpart;
			fractpart = modf(dbl, &intpart);
			timeout.tv_sec = (long)intpart;
			timeout.tv_usec = (long)(fractpart * 1000000);
			break;
		case 'm':
			;
			struct ether_addr *macaddr_p = ether_aton(optarg);
			if (macaddr_p == NULL) {
				fprintf(stderr, "Invalid MAC address '%s'\n", optarg);
				return ERR_ARGS;
			}
			macaddr = *macaddr_p;
			break;
		default:
			usage(argv[0]);
			return ERR_ARGS;
		}
	}

	switch (argc-optind) {
	case 2:
		if (inet_aton(argv[optind+1], &ipaddr) != 1) {
			fprintf(stderr, "Invalid IP address %s\n", argv[optind+1]);
			usage(argv[0]);
			return ERR_ARGS;
		}
		iface.name = argv[optind];
		break;
	default:
		usage(argv[0]);
		return ERR_ARGS;
	}

	if (find_device(&iface) != 0) {
		fprintf(stderr, "Invalid interface %s\n", iface.name);
		return ERR_SYS;
	}

	/* prepare socket */
	int sock = socket(AF_PACKET, SOCK_DGRAM, 0);
	if (sock == -1) {
		perror("socket");
		return ERR_SYS;
	}

	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
		perror("setsockopt");
		return ERR_SYS;
	}

	struct sockaddr_ll addr = {0};
	addr.sll_family = AF_PACKET;
	addr.sll_ifindex = iface.ifindex;
	addr.sll_protocol = htons(ETH_P_ARP);

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		perror("bind");
		return ERR_SYS;
	}

	struct sockaddr_ll me = addr;
	socklen_t me_len = sizeof(me);
	if (getsockname(sock, (struct sockaddr*)&me, &me_len) == -1) {
		perror("getsockname");
		return ERR_SYS;
	}
	if (me.sll_halen == 0) {
		fprintf(stderr, "Interface %s has no ll address\n", iface.name);
		return ERR_SYS;
	}

	addr.sll_halen = ETHER_ADDR_LEN;
	memcpy(addr.sll_addr, macaddr.ether_addr_octet, ETH_ALEN);

	/* construct arp request */
	struct ether_arp req;
	req.arp_hrd = htons(ARPHRD_ETHER);
	req.arp_pro = htons(ETH_P_IP);
	req.arp_hln = ETHER_ADDR_LEN;
	req.arp_pln = sizeof(in_addr_t);
	req.arp_op = htons(ARPOP_REQUEST);

	memcpy(req.arp_sha, me.sll_addr, sizeof(req.arp_sha));
	memset(req.arp_spa, 0, sizeof(req.arp_spa));
	memcpy(req.arp_tha, macaddr.ether_addr_octet, sizeof(req.arp_tha));
	memcpy(req.arp_tpa, &ipaddr.s_addr, sizeof(req.arp_tpa));

	/* send and receive */
	if (sendto(sock, &req, sizeof(req), 0, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		perror("sendto");
		return ERR_SYS;
	}

	struct ether_arp response;
	int ret = ERR_SUCCESS;

	do {
		int recv_ret = recvfrom(sock, &response, sizeof(response), 0, NULL, NULL);
		if (recv_ret <= 0) {
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				/* timeout, no such host */
				ret = ERR_FAIL;
				break;
			} else {
				perror("recvfrom");
				ret = ERR_SYS;
				break;
			}
		}
	} while (check_reply(&req, &response));

	if (ret == ERR_SUCCESS) {
		printf("%02X:%02X:%02X:%02X:%02X:%02X\n",
			response.arp_sha[0],
			response.arp_sha[1],
			response.arp_sha[2],
			response.arp_sha[3],
			response.arp_sha[4],
			response.arp_sha[5]);
	}

	close(sock);

	return ret;
}
Exemple #3
0
/* ARGSUSED */
static int
checkfilesys(char *filesys)
{
    ufs2_daddr_t n_ffree, n_bfree;
    struct dups *dp;
    int cylno;
    intmax_t blks, files;

    check_filename = filesys;
    if (check_debug && check_clean)
        check_warn("starting\n");

    check_sblock_init();
    if (check_clean && check_skipclean) {
        /*
         * If file system is gjournaled, check it here.
         */
        if ((check_fsreadfd = open(filesys, O_RDONLY)) < 0 || check_readsb(0) == 0)
            exit(3);    /* Cannot read superblock */
        close(check_fsreadfd);
        if ((check_sblk.b_un.b_fs->fs_flags & FS_GJOURNAL) != 0) {
            //printf("GJournaled file system detected on %s.\n",
            //    filesys);
            if (check_sblk.b_un.b_fs->fs_clean == 1) {
                check_warn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
                exit(0);
            }
            if ((check_sblk.b_un.b_fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
                check_gjournal(filesys);
                exit(0);
            } else {
                check_fatal(
                "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
            }
        }
    }

    switch (check_setup(filesys, 0)) {
    case 0:
        if (check_preen)
            check_fatal("CAN'T CHECK FILE SYSTEM.");
        return (0);
    case -1:
        check_warn("clean, %ld free ", (long)(check_sblk.b_un.b_fs->fs_cstotal.cs_nffree +
            check_sblk.b_un.b_fs->fs_frag * check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree));
        printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
            (intmax_t)check_sblk.b_un.b_fs->fs_cstotal.cs_nffree,
            (intmax_t)check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree,
            check_sblk.b_un.b_fs->fs_cstotal.cs_nffree * 100.0 / check_sblk.b_un.b_fs->fs_dsize);
        return (0);
    }
    /*
     * Determine if we can and should do journal recovery.
     */
    if ((check_sblk.b_un.b_fs->fs_flags & FS_SUJ) == FS_SUJ) {
        if ((check_sblk.b_un.b_fs->fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && check_skipclean) {
            if (check_preen || check_reply("USE JOURNAL")) {
                if (check_suj(filesys) == 0) {
                    printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
                    exit(0);
                }
            }
            printf("** Skipping journal, falling through to full fsck\n\n");
        }
        /*
         * Write the superblock so we don't try to recover the
         * journal on another pass.
         */
        check_sblk.b_un.b_fs->fs_mtime = time(NULL);
        dirty(&check_sblk);
    }

    /*
     * Cleared if any questions answered no. Used to decide if
     * the superblock should be marked clean.
     */
    check_resolved = 1;
    /*
     * 1: scan inodes tallying blocks used
     */
    if (check_preen == 0) {
        printf("** Last Mounted on %s\n", check_sblk.b_un.b_fs->fs_fsmnt);
        printf("** Phase 1 - Check Blocks and Sizes\n");
    }
    check_pass1();

    /*
     * 1b: locate first references to duplicates, if any
     */
    if (check_duplist) {
        if (check_preen || check_usedsoftdep)
            check_fatal("INTERNAL ERROR: dups with %s%s%s",
                check_preen ? "-p" : "",
                (check_preen && check_usedsoftdep) ? " and " : "",
                check_usedsoftdep ? "softupdates" : "");
        printf("** Phase 1b - Rescan For More DUPS\n");
        check_pass1b();
    }

    /*
     * 2: traverse directories from root to mark all connected directories
     */
    if (check_preen == 0)
        printf("** Phase 2 - Check Pathnames\n");
    check_pass2();

    /*
     * 3: scan inodes looking for disconnected directories
     */
    if (check_preen == 0)
        printf("** Phase 3 - Check Connectivity\n");
    check_pass3();

    /*
     * 4: scan inodes looking for disconnected files; check reference counts
     */
    if (check_preen == 0)
        printf("** Phase 4 - Check Reference Counts\n");
    check_pass4();

    /*
     * 5: check and repair resource counts in cylinder groups
     */
    if (check_preen == 0)
        printf("** Phase 5 - Check Cyl groups\n");
    check_pass5();

    /*
     * print out summary statistics
     */
    n_ffree = check_sblk.b_un.b_fs->fs_cstotal.cs_nffree;
    n_bfree = check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree;
    files = check_maxino - ROOTINO - check_sblk.b_un.b_fs->fs_cstotal.cs_nifree - check_n_files;
    blks = check_n_blks +
        check_sblk.b_un.b_fs->fs_ncg * (cgdmin(check_sblk.b_un.b_fs, 0) - cgsblock(check_sblk.b_un.b_fs, 0));
    blks += cgsblock(check_sblk.b_un.b_fs, 0) - cgbase(check_sblk.b_un.b_fs, 0);
    blks += howmany(check_sblk.b_un.b_fs->fs_cssize, check_sblk.b_un.b_fs->fs_fsize);
    blks = check_maxfsblock - (n_ffree + check_sblk.b_un.b_fs->fs_frag * n_bfree) - blks;
    check_warn("%ld files, %jd used, %ju free ",
        (long)check_n_files, (intmax_t)check_n_blks,
        (uintmax_t)(n_ffree + check_sblk.b_un.b_fs->fs_frag * n_bfree));
    printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
        (uintmax_t)n_ffree, (uintmax_t)n_bfree,
        n_ffree * 100.0 / check_sblk.b_un.b_fs->fs_dsize);
    if (check_debug) {
        if (files < 0)
            printf("%jd inodes missing\n", -files);
        if (blks < 0)
            printf("%jd blocks missing\n", -blks);
        if (check_duplist != NULL) {
            printf("The following duplicate blocks remain:");
            for (dp = check_duplist; dp; dp = dp->next)
                printf(" %jd,", (intmax_t)dp->dup);
            printf("\n");
        }
    }
    check_duplist = (struct dups *)0;
    check_muldup = (struct dups *)0;
    check_inocleanup();
    if (check_fsmodified) {
        check_sblk.b_un.b_fs->fs_time = time(NULL);
        dirty(&check_sblk);
    }
    if (check_cvtlevel && check_sblk.b_dirty) {
        /*
         * Write out the duplicate super blocks
         */
        for (cylno = 0; cylno < check_sblk.b_un.b_fs->fs_ncg; cylno++)
            check_blwrite(check_fswritefd, (char *)check_sblk.b_un.b_fs,
                fsbtodb(check_sblk.b_un.b_fs, cgsblock(check_sblk.b_un.b_fs, cylno)),
                SBLOCKSIZE);
    }
    if (check_rerun)
        check_resolved = 0;

    /*
     * Check to see if the file system is mounted read-write.
     */
    check_finish(check_resolved);

    for (cylno = 0; cylno < check_sblk.b_un.b_fs->fs_ncg; cylno++)
        if (check_inostathead[cylno].il_stat != NULL)
            free((char *)check_inostathead[cylno].il_stat);
    free((char *)check_inostathead);
    check_inostathead = NULL;
    if (check_fsmodified && !check_preen)
        printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    if (check_rerun)
        printf("\n***** PLEASE RERUN FSCK *****\n");
    return (0);
}