VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/daemon/vboxadd_timesync.c@ 530

Last change on this file since 530 was 5, checked in by vboxsync, 18 years ago

Add C version of timesync daemon.

File size: 4.3 KB
Line 
1/** @file
2 *
3 * VirtualBox timesync daemon for Linux
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <getopt.h>
27#include <errno.h>
28#include <sys/fcntl.h>
29#include <sys/ioctl.h>
30#include <sys/time.h>
31#include <time.h>
32#include <VBox/VBoxGuest.h>
33#include <VBox/err.h>
34
35static void usage(void)
36{
37 printf("vboxadd-timesync [--interval <seconds>]\n"
38 " [--daemonize]"
39 "\n");
40}
41
42static void safe_sleep(time_t seconds)
43{
44 struct timespec requestedSleepTime;
45
46 requestedSleepTime.tv_sec = seconds;
47 requestedSleepTime.tv_nsec = 0;
48
49 for (;;)
50 {
51 int err;
52 struct timespec remainingSleepTime;
53
54 err = nanosleep(&requestedSleepTime, &remainingSleepTime);
55 if (err)
56 {
57 /* if the sleep was interrupted, remember remaining sleep time */
58 if (errno == EINTR)
59 {
60 requestedSleepTime = remainingSleepTime;
61 }
62 else
63 {
64 /* not good... */
65 }
66 }
67 else
68 {
69 /* nanosleep completed and took at least the requested time */
70 break;
71 }
72 }
73}
74
75
76int main(int argc, char *argv[])
77{
78 const struct option options[] =
79 {
80 { "interval", required_argument, NULL, 'i' },
81 { "daemonize", no_argument, NULL, 'd' },
82 { "help", no_argument, NULL, 'h' },
83 { NULL, 0, NULL, 0 }
84 };
85 int secInterval = 10;
86 int fDaemonize = 0;
87 int c;
88 int fd;
89 VMMDevReqHostTime req;
90
91 /* command line parsing */
92 for (;;)
93 {
94 c = getopt_long(argc, argv, "", options, NULL);
95 if (c == -1)
96 break;
97 switch (c)
98 {
99 case 'i':
100 {
101 secInterval = atoi(optarg);
102 break;
103 }
104
105 case 'd':
106 {
107 fDaemonize = 1;
108 break;
109 }
110
111 case 'h':
112 {
113 usage();
114 return 0;
115 }
116
117 case ':':
118 case '?':
119 {
120 /* unrecognized option (?) or parameter missing (:) */
121 return 1;
122 }
123
124 default:
125 break;
126 }
127 }
128
129 /* open the driver */
130 fd = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
131 if (fd < 0)
132 {
133 printf("Error opening kernel module! rc = %d\n", errno);
134 return 1;
135 }
136
137 /* prepare the request structure */
138 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_GetHostTime);
139
140 if (!fDaemonize)
141 printf("VirtualBox timesync daemon.\n"
142 "(C) 2005 InnoTek Systemberatung GmbH\n"
143 "\nSync interval: %d seconds.\n", secInterval);
144
145 if (fDaemonize)
146 daemon(1, 0);
147
148 do
149 {
150 /* perform VMM request */
151 if (ioctl(fd, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) >= 0)
152 {
153 if (VBOX_SUCCESS(req.header.rc))
154 {
155 /* set the system time */
156 struct timeval tv;
157 tv.tv_sec = req.time / (uint64_t)1000;
158 tv.tv_usec = (req.time % (uint64_t)1000) * 1000;
159 settimeofday(&tv, NULL);
160 }
161 else
162 {
163 printf("Error querying host time! header.rc = %d\n", req.header.rc);
164 }
165 }
166 else
167 {
168 printf("Error performing VMM request! errno = %d\n", errno);
169 }
170 /* wait for the next run */
171 safe_sleep(secInterval);
172
173 } while (1);
174
175 close(fd);
176
177 return 0;
178}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette