Skip to content

Instantly share code, notes, and snippets.

@pfactum
Created May 10, 2014 11:45
Show Gist options
  • Save pfactum/edf362be4ed8c8721350 to your computer and use it in GitHub Desktop.
Save pfactum/edf362be4ed8c8721350 to your computer and use it in GitHub Desktop.
/*
libpfrng — CPU jitter based random number generator
© 2013–2014, Oleksandr Natalenko aka post-factum <oleksandr@natalenko.name>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <getopt.h>
#include <pthread.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <time.h>
#include <zmq.h>
#include "../../libpfrng.h"
#include "pfrng-feeder.h"
int main(int argc, char** argv)
{
if (getuid() != 0)
{
fprintf(stderr, "You have to be root in order to use this tool!\n");
exit(EX_NOPERM);
}
int opts = 0;
struct option longopts[] =
{
{"ping", 0, NULL, 'p'},
{"exit", 0, NULL, 'e'},
{"help", 0, NULL, 'h'},
{0, 0, 0, 0}
};
void* mq_context = zmq_ctx_new();
void* mq_socket = zmq_socket(mq_context, ZMQ_REQ);
int mq_connect = zmq_connect(mq_socket, "ipc:///run/pfrng-feeder");
if (mq_connect != 0)
abort();
pfrng_msg_t mq_msg, mq_rsp;
// parse cmdline options
while (-1 != (opts = getopt_long(argc, argv, "peh", longopts, NULL)))
{
switch (opts)
{
case 'p':
mq_msg.key = PFMSG_PING;
struct timespec time_start, time_end;
clock_gettime(CLOCK_REALTIME, &time_start);
zmq_send(mq_socket, &mq_msg, sizeof(mq_msg), 0);
zmq_recv(mq_socket, &mq_rsp, sizeof(mq_rsp), 0);
clock_gettime(CLOCK_REALTIME, &time_end);
double ms = (time_end.tv_sec * 1000000000ULL + time_end.tv_nsec -
time_start.tv_sec * 1000000000ULL - time_start.tv_nsec) / 1000000.0;
if (mq_rsp.key == PFMSG_ECHO_REPLY)
printf("Got response from daemon in %1.3lf ms!\n", ms);
else
fprintf(stderr, "Got unknown response from daemon!\n");
break;
case 'e':
mq_msg.key = PFMSG_EXIT;
zmq_send(mq_socket, &mq_msg, sizeof(mq_msg), 0);
break;
case 'h':
printf(
"pfrng-feeder-ctl — small tool to control /dev/random feeder\n"
"Version %d.%d.%d, API version %d\n"
"© Oleksandr Natalenko aka post-factum <oleksandr@natalenko.name>, 2013–2014\n"
"Distributed under terms and conditions of LGPLv3. See COPYING file for details.\n"
"\n"
"Usage:\n"
"\tpfrng-feeder-ctl [--ping|--exit]\n"
"Switches:\n"
"\t--ping,\t-p\t— ping daemon\n"
"\t--exit,\t-e\t— tell daemon to exit\n"
"\t--help,\t-h\t— show this help\n",
LIBPFRNG_MAJOR, LIBPFRNG_MINOR, LIBPFRNG_RELEASE, LIBPFRNG_API
);
exit(EX_OK);
break;
default:
fprintf(stderr, "Unknown option: %c\n", opts);
exit(EX_USAGE);
break;
}
}
zmq_close(mq_socket);
zmq_ctx_term(mq_context);
exit(EX_OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment