1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/threads/t_rcache.c */
4 * Copyright (C) 2006 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
40 unsigned long my_ctime;
41 unsigned int my_cusec;
46 #define DEFAULT_N_THREADS 2
47 #define DEFAULT_INTERVAL 20 /* 5 * 60 */
50 int n_threads = DEFAULT_N_THREADS;
51 int interval = DEFAULT_INTERVAL;
54 static void wait_for_tick ()
60 } while (now == next);
63 /* Encrypt data into out (preallocated by the caller) with a random key. */
64 static krb5_error_code encrypt_data (krb5_data *data, krb5_enc_data *out)
69 err = krb5_c_make_random_key(ctx, ENCTYPE_AES256_CTS_HMAC_SHA1_96,
73 err = krb5_c_encrypt(ctx, &kb, KRB5_KEYUSAGE_TGS_REQ_AUTH, NULL, data,
75 krb5_free_keyblock_contents(ctx, &kb);
79 static void try_one (struct tinfo *t)
82 char buf[256], buf2[512];
83 krb5_rcache my_rcache;
87 snprintf(buf, sizeof(buf), "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU",
90 /* k5_rc_store() requires a ciphertext. Create one by encrypting a dummy
91 * value in a random key. */
93 enc.ciphertext = make_data(buf2, sizeof(buf2));
94 err = encrypt_data(&d, &enc);
96 const char *msg = krb5_get_error_message(ctx, err);
97 fprintf(stderr, "%s: encrypting authenticator: %s\n", prog, msg);
98 krb5_free_error_message(ctx, msg);
102 if (t->now != t->my_ctime) {
103 if (t->my_ctime != 0) {
104 snprintf(buf2, sizeof(buf2), "%3d: %ld %5d\n", t->idx,
105 t->my_ctime, t->my_cusec);
108 t->my_ctime = t->now;
113 err = k5_rc_resolve(ctx, rcname, &my_rcache);
115 const char *msg = krb5_get_error_message(ctx, err);
116 fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
117 krb5_free_error_message(ctx, msg);
122 err = k5_rc_store(ctx, my_rcache, &enc);
124 com_err(prog, err, "storing in replay cache");
128 k5_rc_close(ctx, my_rcache);
131 static void *run_a_loop (void *x)
133 struct tinfo t = { 0 };
137 while (t.now != time(0))
140 while (t.now < end_time) {
149 static void usage(void)
151 fprintf (stderr, "usage: %s [ options ] rcname\n", prog);
152 fprintf (stderr, "options:\n");
153 fprintf (stderr, "\t-1\tcreate one rcache handle for process\n");
154 fprintf (stderr, "\t-t N\tnumber of threads to create (default: %d)\n",
157 "\t-i N\tinterval to run test over, in seconds (default: %d)\n",
162 static const char optstring[] = "1t:i:";
164 static void process_options (int argc, char *argv[])
169 while ((c = getopt(argc, argv, optstring)) != -1) {
179 n_threads = atoi (optarg);
180 if (n_threads < 1 || n_threads > 10000)
184 interval = atoi (optarg);
185 if (interval < 2 || n_threads > 100000)
198 int main (int argc, char *argv[])
204 process_options (argc, argv);
205 err = krb5_init_context(&ctx);
207 com_err(prog, err, "initializing context");
212 err = k5_rc_resolve(ctx, rcname, &rcache);
214 const char *msg = krb5_get_error_message(ctx, err);
215 fprintf(stderr, "%s: %s while initializing new replay cache\n",
217 krb5_free_error_message(ctx, msg);
222 ip = malloc(sizeof(int) * n_threads);
223 if (ip == 0 && n_threads > 0) {
227 for (i = 0; i < n_threads; i++)
231 end_time = time(0) + interval;
233 for (i = 0; i < n_threads; i++) {
234 pthread_t new_thread;
236 perr = pthread_create(&new_thread, 0, run_a_loop, &ip[i]);
239 perror("pthread_create");
243 while (time(0) < end_time + 1)
246 for (i = 0; i < n_threads; i++) {
248 printf("thread %d total %5d, about %.1f per second\n", i, ip[i],
249 ((double) ip[i])/interval);
251 printf("total %lu in %d seconds, avg ~%.1f/sec, ~%.1f/sec/thread\n",
253 ((double)sum)/interval, ((double)sum)/interval/n_threads);
257 k5_rc_close(ctx, rcache);
258 krb5_free_context(ctx);