feisty meow concerns codebase  2.140
time_set_effective_id.cpp
Go to the documentation of this file.
1 /*
2  * Name : time_set_effective_id
3  * Author : Chris Koeritz
4  * Purpose:
5  *
6  * A simple test of how long it takes to call set effective id on Unix.
7  * For this to really work as designed, it should be run as root.
8  *
9  * Copyright (c) 2003-$now By Author. This program is free software; you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation; either version 2 of
12  * the License or (at your option) any later version. This is online at:
13  * http://www.fsf.org/copyleft/gpl.html
14  * Please send any updates to: fred@gruntose.com
15  */
16 
17 #include <basis/functions.h>
18 #include <basis/astring.h>
19 #include <structures/set.h>
20 #include <timely/time_stamp.h>
22 #include <loggers/console_logger.h>
23 #include <loggers/file_logger.h>
25 
26 using namespace application;
27 using namespace basis;
28 using namespace loggers;
29 using namespace filesystem;
30 using namespace textual;
31 using namespace timely;
32 
33 #undef BASE_LOG
34 #define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
35 #undef LOG
36 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
37 
38 class time_set_effective_id : public application_shell
39 {
40 public:
41  time_set_effective_id() : application_shell() {}
42  DEFINE_CLASS_NAME("time_set_effective_id");
43  int execute();
44 };
45 
46 int time_set_effective_id::execute()
47 {
48  FUNCDEF("execute");
50 
51 #ifndef __WIN32__
52  int test_runs = 1000000;
53 
54  time_stamp start; // start of test.
55  for (int i = 0; i < test_runs; i++) {
56  // set effective id to fred.
57  int ret = seteuid(1008);
58  if (ret != 0) {
59  LOG("failure to change effective user id to normal user.");
60  exit(1);
61  }
62  // set effective id to root.
63  ret = seteuid(0);
64  if (ret != 0) {
65  LOG("failure to change effective user id to root.");
66  exit(1);
67  }
68  }
69  time_stamp completion_time; // end of test.
70 
71  double durat = completion_time.value() - start.value();
72  double secs = durat / 1000.0; // convert to seconds.
73  LOG(a_sprintf("test run took %0.2f milliseconds or %0.2f seconds or %0.2f minutes.", durat, secs, secs / 60.0));
74  // divide by two because we're doing two calls above.
75  LOG(a_sprintf("individual call takes %0.0f milliseconds.", durat / double(test_runs) / 2.0));
76 #endif
77 
78  return 0;
79 }
80 
81 HOOPLE_MAIN(time_set_effective_id, )
82 
83 #ifdef __BUILD_STATIC_APPLICATION__
84  // static dependencies found by buildor_gen_deps.sh:
85 #endif // __BUILD_STATIC_APPLICATION__
86 
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38
time_representation value() const
returns the time_stamp in terms of the lower level type.
Definition: time_stamp.h:61
#define SETUP_COMBO_LOGGER
a macro that retasks the program-wide logger as a combo_logger.
Definition: combo_logger.h:49
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
#include <time.h>
Definition: earth_time.cpp:37
#define LOG(to_print)