~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/util/support/threads.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2009-05-07 16:16:34 UTC
  • mfrom: (13.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090507161634-xqyk0s9na0le4flj
Tags: 1.7dfsg~beta1-4
When  decrypting the TGS response fails with the subkey, try with the
session key to work around Heimdal bug, Closes: #527353 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * util/support/threads.c
3
3
 *
4
 
 * Copyright 2004,2005,2006 by the Massachusetts Institute of Technology.
 
4
 * Copyright 2004,2005,2006,2007,2008 by the Massachusetts Institute of Technology.
5
5
 * All Rights Reserved.
6
6
 *
7
7
 * Export of this software from the United States of America may
30
30
#include <assert.h>
31
31
#include <stdlib.h>
32
32
#include <errno.h>
 
33
#define THREAD_SUPPORT_IMPL
33
34
#include "k5-thread.h"
34
35
#include "k5-platform.h"
35
36
#include "supp-int.h"
170
171
static struct tsd_block tsd_if_single;
171
172
# define GET_NO_PTHREAD_TSD()   (&tsd_if_single)
172
173
#else
 
174
int krb5int_pthread_loaded (void)
 
175
{
 
176
    return 1;
 
177
}
173
178
# define GET_NO_PTHREAD_TSD()   (abort(),(struct tsd_block *)0)
174
179
#endif
175
180
 
270
275
        int i;
271
276
        t = malloc(sizeof(*t));
272
277
        if (t == NULL)
273
 
            return errno;
 
278
            return ENOMEM;
274
279
        for (i = 0; i < K5_KEY_MAX; i++)
275
280
            t->values[i] = 0;
276
281
        /* add to global linked list */
290
295
            int i;
291
296
            t = malloc(sizeof(*t));
292
297
            if (t == NULL)
293
 
                return errno;
 
298
                return ENOMEM;
294
299
            for (i = 0; i < K5_KEY_MAX; i++)
295
300
                t->values[i] = 0;
296
301
            /* add to global linked list */
407
412
 
408
413
#include "cache-addrinfo.h"
409
414
 
410
 
#ifdef DEBUG_THREADS_STATS
411
 
#include <stdio.h>
412
 
static FILE *stats_logfile;
413
 
#endif
414
 
 
415
415
int krb5int_thread_support_init (void)
416
416
{
417
417
    int err;
420
420
    printf("krb5int_thread_support_init\n");
421
421
#endif
422
422
 
423
 
#ifdef DEBUG_THREADS_STATS
424
 
    /*    stats_logfile = stderr; */
425
 
    stats_logfile = fopen("/dev/tty", "w+");
426
 
    if (stats_logfile == NULL)
427
 
      stats_logfile = stderr;
428
 
#endif
429
 
 
430
423
#ifndef ENABLE_THREADS
431
424
 
432
425
    /* Nothing to do for TLS initialization.  */
491
484
 
492
485
#endif
493
486
 
494
 
#ifdef DEBUG_THREADS_STATS
495
 
    fflush(stats_logfile);
496
 
    /* XXX Should close if not stderr, in case unloading library but
497
 
       not exiting.  */
498
 
#endif
499
 
 
500
487
    krb5int_fini_fac();
501
488
}
502
489
 
503
 
#ifdef DEBUG_THREADS_STATS
504
 
void KRB5_CALLCONV
505
 
k5_mutex_lock_update_stats(k5_debug_mutex_stats *m,
506
 
                           k5_mutex_stats_tmp startwait)
507
 
{
508
 
  k5_debug_time_t now;
509
 
  k5_debug_timediff_t tdiff, tdiff2;
510
 
 
511
 
  now = get_current_time();
512
 
  (void) krb5int_call_thread_support_init();
513
 
  m->count++;
514
 
  m->time_acquired = now;
515
 
  tdiff = timediff(now, startwait);
516
 
  tdiff2 = tdiff * tdiff;
517
 
  if (m->count == 1 || m->lockwait.valmin > tdiff)
518
 
    m->lockwait.valmin = tdiff;
519
 
  if (m->count == 1 || m->lockwait.valmax < tdiff)
520
 
    m->lockwait.valmax = tdiff;
521
 
  m->lockwait.valsum += tdiff;
522
 
  m->lockwait.valsqsum += tdiff2;
523
 
}
524
 
 
525
 
void KRB5_CALLCONV
526
 
krb5int_mutex_unlock_update_stats(k5_debug_mutex_stats *m)
527
 
{
528
 
  k5_debug_time_t now = get_current_time();
529
 
  k5_debug_timediff_t tdiff, tdiff2;
530
 
  tdiff = timediff(now, m->time_acquired);
531
 
  tdiff2 = tdiff * tdiff;
532
 
  if (m->count == 1 || m->lockheld.valmin > tdiff)
533
 
    m->lockheld.valmin = tdiff;
534
 
  if (m->count == 1 || m->lockheld.valmax < tdiff)
535
 
    m->lockheld.valmax = tdiff;
536
 
  m->lockheld.valsum += tdiff;
537
 
  m->lockheld.valsqsum += tdiff2;
538
 
}
539
 
 
540
 
#include <math.h>
541
 
static inline double
542
 
get_stddev(struct k5_timediff_stats sp, int count)
543
 
{
544
 
  long double mu, mu_squared, rho_squared;
545
 
  mu = (long double) sp.valsum / count;
546
 
  mu_squared = mu * mu;
547
 
  /* SUM((x_i - mu)^2)
548
 
     = SUM(x_i^2 - 2*mu*x_i + mu^2)
549
 
     = SUM(x_i^2) - 2*mu*SUM(x_i) + N*mu^2
550
 
 
551
 
     Standard deviation rho^2 = SUM(...) / N.  */
552
 
  rho_squared = (sp.valsqsum - 2 * mu * sp.valsum + count * mu_squared) / count;
553
 
  return sqrt(rho_squared);
554
 
}
555
 
 
556
 
void KRB5_CALLCONV
557
 
krb5int_mutex_report_stats(k5_mutex_t *m)
558
 
{
559
 
  char *p;
560
 
 
561
 
  /* Tweak this to only record data on "interesting" locks.  */
562
 
  if (m->stats.count < 10)
563
 
    return;
564
 
  if (m->stats.lockwait.valsum < 10 * m->stats.count)
565
 
    return;
566
 
 
567
 
  p = strrchr(m->loc_created.filename, '/');
568
 
  if (p == NULL)
569
 
    p = m->loc_created.filename;
570
 
  else
571
 
    p++;
572
 
  fprintf(stats_logfile, "mutex @%p: created at line %d of %s\n",
573
 
          (void *) m, m->loc_created.lineno, p);
574
 
  if (m->stats.count == 0)
575
 
    fprintf(stats_logfile, "\tnever locked\n");
576
 
  else {
577
 
    double sd_wait, sd_hold;
578
 
    sd_wait = get_stddev(m->stats.lockwait, m->stats.count);
579
 
    sd_hold = get_stddev(m->stats.lockheld, m->stats.count);
580
 
    fprintf(stats_logfile,
581
 
            "\tlocked %d time%s; wait %lu/%f/%lu/%fus, hold %lu/%f/%lu/%fus\n",
582
 
            m->stats.count, m->stats.count == 1 ? "" : "s",
583
 
            (unsigned long) m->stats.lockwait.valmin,
584
 
            (double) m->stats.lockwait.valsum / m->stats.count,
585
 
            (unsigned long) m->stats.lockwait.valmax,
586
 
            sd_wait,
587
 
            (unsigned long) m->stats.lockheld.valmin,
588
 
            (double) m->stats.lockheld.valsum / m->stats.count,
589
 
            (unsigned long) m->stats.lockheld.valmax,
590
 
            sd_hold);
591
 
  }
592
 
}
593
 
#else
594
 
/* On Windows, everything defined in the export list must be defined.
595
 
   The UNIX systems where we're using the export list don't seem to
596
 
   care.  */
597
 
#undef krb5int_mutex_lock_update_stats
598
 
void KRB5_CALLCONV
599
 
krb5int_mutex_lock_update_stats(k5_debug_mutex_stats *m,
600
 
                                k5_mutex_stats_tmp startwait)
601
 
{
602
 
}
603
 
#undef krb5int_mutex_unlock_update_stats
604
 
void KRB5_CALLCONV
605
 
krb5int_mutex_unlock_update_stats(k5_debug_mutex_stats *m)
606
 
{
607
 
}
608
 
#undef krb5int_mutex_report_stats
609
 
void KRB5_CALLCONV
610
 
krb5int_mutex_report_stats(k5_mutex_t *m)
611
 
{
612
 
}
613
 
#endif
614
 
 
615
490
/* Mutex allocation functions, for use in plugins that may not know
616
491
   what options a given set of libraries was compiled with.  */
617
492
int KRB5_CALLCONV
622
497
 
623
498
    ptr = malloc (sizeof (k5_mutex_t));
624
499
    if (ptr == NULL)
625
 
        return errno;
 
500
        return ENOMEM;
626
501
    err = k5_mutex_init (ptr);
627
502
    if (err) {
628
503
        free (ptr);