示例#1
0
/**
 * finds the (k + 1)th perm and writes it in tmp
 */
void permut(int k) {
    for(a = 0; a < len; a++) {
        tmp = fctrl(len - 1 - a);
        indx = find(k / tmp);
        perm[a] = availableNums[indx];
        availableNums[indx] = -1;
        k %= tmp;
    }
}
示例#2
0
int main(int argc, char **argv) {

    printf("This program demonstrates two algorithms: the kth permutation and the next permutation.\n");
    printf("Enter the length (2 - 9): ");
    scanf("%d", &len);

    if ((len > 9) || (len < 2)) {
        fprintf(stderr, "length should be between 2 to 9\n");
        exit(1);
    }

    int load = fctrl(len);

    printf("Enter the value of k (1 - %d): ", load);
    scanf("%d", &myK);

    if ((myK < 1) || (myK > load)) {
        fprintf(stderr, "k should be between 1 to %d\n", load);
        exit(1);
    }

    availableNums = (int *)calloc(len, sizeof(int));
    perm = (int *)calloc(len, sizeof(int));

    for (a = 0; a < len; a++)
        availableNums[a] = a + 1;

    printf("The %dth permutation is: ", myK);
    permut(myK - 1);
    process();
    printf("\n");

    printf("The next permutation is: ");

    if (myK == load)   // some cheat
		printf("na");	
    
    else {
        nextPermut();
	process();
    }

    printf("\n");

    free(availableNums);
    free(perm);
    scanf("\n");
    return 0;
}
/* checkTag() also tries to lookup the socket address in the kernel and
 * return it when *addr  == NULL.
 * This allows for better look ups when another process is also setting the same
 * tag + uid. But it is not fool proof.
 * Without the kernel reporting more info on who setup the socket tag, it is
 * not easily verifiable from user-space.
 * Returns: true if tag found.
 */
bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
    int res;
    uint64_t k_tag;
    uint32_t k_uid;
    long dummy_count;
    pid_t dummy_pid;

    std::ifstream fctrl("/proc/net/xt_qtaguid/ctrl", std::fstream::in);
    if(!fctrl.is_open()) {
        testPrintI("qtaguid ctrl open failed!");
    }

    uint64_t full_tag = acct_tag | uid;
    std::string buff = android::base::StringPrintf(" tag=0x%" PRIx64 " (uid=%u)", full_tag, uid);
    if (addr) {
          buff = android::base::StringPrintf("sock=%" PRIxPTR, (uintptr_t)addr) + buff;
    }

    testPrintI("looking for '%s'", buff.c_str());
    std::string ctrl_data;
    std::size_t pos = std::string::npos;
    while(std::getline(fctrl, ctrl_data)) {
        testPrintI("<ctrl_raw_data> : %s", ctrl_data.c_str());
        pos = ctrl_data.find(buff);
        if (pos != std::string::npos) {
            if(!addr) {
                testPrintI("matched data : %s", ctrl_data.c_str());
                assert(sizeof(void*) == sizeof(long int));
                res = sscanf(ctrl_data.c_str(),
                            "sock=%" SCNxPTR " tag=0x%" SCNx64 " (uid=%" SCNu32 ") pid=%u f_count=%lu",
                            (uintptr_t *)&addr, &k_tag, &k_uid, &dummy_pid, &dummy_count );
                if (!(res == 5 && k_tag == full_tag && k_uid == uid)) {
                    testPrintE("Unable to read sock addr res=%d", res);
                    addr = 0;
                } else {
                    testPrintI("Got sock_addr %lx", addr);
                }
            }
            break;
        }
    }
    return pos != std::string::npos;
}