Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove/replace time calls #3328

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 12 additions & 56 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2045,18 +2045,6 @@ g_obj_wait(tintptr *read_objs, int rcount, tintptr *write_objs, int wcount,
void
g_random(char *data, int len)
{
#if defined(_WIN32)
int index;

srand(g_time1());

for (index = 0; index < len; index++)
{
data[index] = (char)rand(); /* rand returns a number between 0 and
RAND_MAX */
}

#else
int fd;

memset(data, 0x44, len);
Expand All @@ -2075,8 +2063,6 @@ g_random(char *data, int len)

close(fd);
}

#endif
}

/*****************************************************************************/
Expand Down Expand Up @@ -3778,51 +3764,21 @@ g_check_user_in_group(const char *username, int gid, int *ok)
#endif // HAVE_GETGROUPLIST

/*****************************************************************************/
/* returns the time since the Epoch (00:00:00 UTC, January 1, 1970),
measured in seconds.
for windows, returns the number of seconds since the machine was
started. */
int
g_time1(void)
{
#if defined(_WIN32)
return GetTickCount() / 1000;
#else
return time(0);
#endif
}

/*****************************************************************************/
/* returns the number of milliseconds since the machine was
started. */
int
g_time2(void)
unsigned int
g_get_elapsed_ms(void)
{
#if defined(_WIN32)
return (int)GetTickCount();
#else
struct tms tm;
clock_t num_ticks = 0;
g_memset(&tm, 0, sizeof(struct tms));
num_ticks = times(&tm);
return (int)(num_ticks * 10);
#endif
}
unsigned int result = 0;
struct timespec tp;

/*****************************************************************************/
/* returns time in milliseconds, uses gettimeofday
does not work in win32 */
int
g_time3(void)
{
#if defined(_WIN32)
return 0;
#else
struct timeval tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
{
result = (unsigned int)tp.tv_sec * 1000;
// POSIX 1003.1-2004 specifies that tv_nsec is a long (i.e. a
// signed type), but can only contain [0..999,999,999]
result += tp.tv_nsec / 1000000;
}

gettimeofday(&tp, 0);
return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
#endif
return result;
}

/******************************************************************************/
Expand Down
23 changes: 20 additions & 3 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,26 @@ int g_getgroup_info(const char *groupname, int *gid);
* Primary group of username is also checked
*/
int g_check_user_in_group(const char *username, int gid, int *ok);
int g_time1(void);
int g_time2(void);
int g_time3(void);

/**
* Gets elapsed milliseconds since some arbitrary point in the past
*
* The returned value is unaffected by leap-seconds or time zone changes.
*
* @return elaped ms since some arbitrary point
*
* Calculate the duration of a task by calling this routine before and
* after the task, and subtracting the two values.
*
* The value wraps every so often (every 49.7 days on a 32-bit system),
* but as we are using unsigned arithmetic, the difference of any of these
* two values can be used to calculate elapsed time, whether-or-not a wrap
* occurs during the interval - provided of course the time being measured
* is less than the total wrap-around interval.
*/
unsigned int
g_get_elapsed_ms(void);

int g_save_to_bmp(const char *filename, char *data, int stride_bytes,
int width, int height, int depth, int bits_per_pixel);
void *g_shmat(int shmid);
Expand Down
12 changes: 6 additions & 6 deletions common/trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,18 +696,18 @@ local_connect_shim(int fd, const char *server, const char *port)
/**************************************************************************//**
* Waits for an asynchronous connect to complete.
* @param self - Transport object
* @param start_time Start time of connect (from g_time3())
* @param start_time Start time of connect (from g_get_elapsed_ms())
* @param timeout Total wait timeout
* @return 0 - connect succeeded, 1 - Connect failed
*
* If the transport is set up for checking a termination object, this
* on a regular basis.
*/
static int
poll_for_async_connect(struct trans *self, int start_time, int timeout)
poll_for_async_connect(struct trans *self, unsigned int start_time, int timeout)
{
int rv = 1;
int ms_remaining = timeout - (g_time3() - start_time);
int ms_remaining = timeout - (int)(g_get_elapsed_ms() - start_time);

while (ms_remaining > 0)
{
Expand Down Expand Up @@ -736,7 +736,7 @@ poll_for_async_connect(struct trans *self, int start_time, int timeout)
break;
}

ms_remaining = timeout - (g_time3() - start_time);
ms_remaining = timeout - (int)(g_get_elapsed_ms() - start_time);
}
return rv;
}
Expand All @@ -747,7 +747,7 @@ int
trans_connect(struct trans *self, const char *server, const char *port,
int timeout)
{
int start_time = g_time3();
unsigned int start_time = g_get_elapsed_ms();
int error;
int ms_before_next_connect;

Expand Down Expand Up @@ -826,7 +826,7 @@ trans_connect(struct trans *self, const char *server, const char *port,
}

/* Have we reached the total timeout yet? */
int ms_left = timeout - (g_time3() - start_time);
int ms_left = timeout - (int)(g_get_elapsed_ms() - start_time);
if (ms_left <= 0)
{
error = 1;
Expand Down
6 changes: 3 additions & 3 deletions sesman/chansrv/chansrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ add_timeout(int msoffset, void (*callback)(void *data), void *data)
tui32 now;

LOG_DEVEL(LOG_LEVEL_DEBUG, "add_timeout:");
now = g_time3();
now = g_get_elapsed_ms();
tobj = g_new0(struct timeout_obj, 1);
tobj->mstime = now + msoffset;
tobj->callback = callback;
Expand Down Expand Up @@ -167,7 +167,7 @@ get_timeout(int *timeout)
tobj = g_timeout_head;
if (tobj != 0)
{
now = g_time3();
now = g_get_elapsed_ms();
while (tobj != 0)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " now %u tobj->mstime %u", now, tobj->mstime);
Expand Down Expand Up @@ -215,7 +215,7 @@ check_timeout(void)
while (tobj != 0)
{
count++;
now = g_time3();
now = g_get_elapsed_ms();
if (now >= tobj->mstime)
{
tobj->callback(tobj->data);
Expand Down
2 changes: 1 addition & 1 deletion sesman/chansrv/clipboard_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ clipboard_get_file(const char *file, int bytes)
list_add_item(g_files_list, (tintptr)cfi);
cfi->size = g_file_get_size(full_fn);
cfi->flags = CB_FILE_ATTRIBUTE_ARCHIVE;
cfi->time = (g_time1() + CB_EPOCH_DIFF) * 10000000LL;
cfi->time = (time(NULL) + CB_EPOCH_DIFF) * 10000000LL;
LOG_DEVEL(LOG_LEVEL_DEBUG, "ok filename [%s] pathname [%s] size [%d]",
cfi->filename, cfi->pathname, cfi->size);
result = 0;
Expand Down
24 changes: 12 additions & 12 deletions sesman/chansrv/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static struct trans *g_audio_c_trans_out = 0; /* connection */
static struct trans *g_audio_l_trans_in = 0; /* listener */
static struct trans *g_audio_c_trans_in = 0; /* connection */

static int g_training_sent_time = 0;
static unsigned int g_training_sent_time = 0;
static int g_cBlockNo = 0;
static int g_bytes_in_stream = 0;
struct fifo *g_in_fifo;
Expand All @@ -86,7 +86,7 @@ static struct stream *g_stream_incoming_packet = NULL;
#define MAX_BBUF_SIZE (1024 * 16)
static char g_buffer[MAX_BBUF_SIZE];
static int g_buf_index = 0;
static int g_sent_time[256];
static unsigned int g_sent_time[256];

static int g_bbuf_size = 1024 * 8; /* may change later */

Expand Down Expand Up @@ -341,15 +341,15 @@ sound_send_training(void)
{
struct stream *s;
int bytes;
int time;
unsigned int time;
char *size_ptr;

make_stream(s);
init_stream(s, 8182);
out_uint16_le(s, SNDC_TRAINING);
size_ptr = s->p;
out_uint16_le(s, 0); /* size, set later */
time = g_time3();
time = g_get_elapsed_ms();
g_training_sent_time = time;
out_uint16_le(s, time);
out_uint16_le(s, 1024);
Expand Down Expand Up @@ -885,7 +885,7 @@ sound_send_wave_data_chunk(char *data, int data_bytes)
{
struct stream *s;
int bytes;
int time;
unsigned int time;
int format_index;
char *size_ptr;

Expand All @@ -912,7 +912,7 @@ sound_send_wave_data_chunk(char *data, int data_bytes)
out_uint16_le(s, SNDC_WAVE);
size_ptr = s->p;
out_uint16_le(s, 0); /* size, set later */
time = g_time3();
time = g_get_elapsed_ms();
out_uint16_le(s, time);
out_uint16_le(s, format_index); /* wFormatNo */
g_cBlockNo++;
Expand Down Expand Up @@ -1040,7 +1040,7 @@ sound_process_training(struct stream *s, int size)
{
int time_diff;

time_diff = g_time3() - g_training_sent_time;
time_diff = g_get_elapsed_ms() - g_training_sent_time;
LOG(LOG_LEVEL_INFO, "sound_process_training: round trip time %u", time_diff);
return 0;
}
Expand All @@ -1052,12 +1052,12 @@ sound_process_wave_confirm(struct stream *s, int size)
{
int wTimeStamp;
int cConfirmedBlockNo;
int time;
unsigned int time;
int time_diff;
int index;
int acc;

time = g_time3();
time = g_get_elapsed_ms();
in_uint16_le(s, wTimeStamp);
in_uint8(s, cConfirmedBlockNo);
time_diff = time - g_sent_time[cConfirmedBlockNo & 0xff];
Expand Down Expand Up @@ -1095,13 +1095,13 @@ static int
process_pcm_message(int id, int size, struct stream *s)
{
static int sending_silence = 0;
static int silence_start_time = 0;
static unsigned int silence_start_time = 0;
switch (id)
{
case 0:
if ((g_client_does_fdk_aac || g_client_does_mp3lame) && sending_silence)
{
if ((g_time3() - silence_start_time) < (int)g_cfg->msec_do_not_send)
if ((g_get_elapsed_ms() - silence_start_time) < g_cfg->msec_do_not_send)
{
/* do not send data within msec_do_not_send msec after SNDC_CLOSE is sent, to avoid stutter. setting from sesman.ini */
return 0;
Expand All @@ -1119,7 +1119,7 @@ process_pcm_message(int id, int size, struct stream *s)
if (buf != NULL)
{
int i;
silence_start_time = g_time3();
silence_start_time = g_get_elapsed_ms();
sending_silence = 1;
for (i = 0; i < send_silence_times; i++)
{
Expand Down
11 changes: 0 additions & 11 deletions sesman/chansrv/xcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ xcommon_fatal_handler(Display *dis)
return 0;
}

/*****************************************************************************/
/* returns time in milliseconds
this is like g_time2 in os_calls, but not milliseconds since machine was
up, something else
this is a time value similar to what the xserver uses */
int
xcommon_get_local_time(void)
{
return g_time3();
}

/******************************************************************************/
/* this should be called first */
int
Expand Down
2 changes: 0 additions & 2 deletions sesman/chansrv/xcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

typedef void (*x_server_fatal_cb_type)(void);

int
xcommon_get_local_time(void);
int
xcommon_init(void);
int
Expand Down
6 changes: 3 additions & 3 deletions sesman/libsesman/verify_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ auth_check_pwd_chg(const char *user)
}

/* check if we need a pwd change */
now = g_time1();
now = time(NULL);
today = now / SECS_PER_DAY;

if (stp->sp_expire == -1)
Expand Down Expand Up @@ -306,7 +306,7 @@ auth_change_pwd(const char *user, const char *newpwd)
}

stp->sp_pwdp = g_strdup(hash);
today = g_time1() / SECS_PER_DAY;
today = time(NULL) / SECS_PER_DAY;
stp->sp_lstchg = today;
stp->sp_expire = today + stp->sp_max + stp->sp_inact;
fd = fopen("/etc/shadow", "rw");
Expand Down Expand Up @@ -377,7 +377,7 @@ auth_account_disabled(struct spwd *stp)
return 1;
}

today = g_time1() / SECS_PER_DAY;
today = time(NULL) / SECS_PER_DAY;

LOG_DEVEL(LOG_LEVEL_DEBUG, "last %ld", stp->sp_lstchg);
LOG_DEVEL(LOG_LEVEL_DEBUG, "min %ld", stp->sp_min);
Expand Down
4 changes: 2 additions & 2 deletions sesman/sesexec/login_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ log_authfail_message(const char *username, const char *ip_addr)
{
ip_addr = "unknown";
}
LOG(LOG_LEVEL_INFO, "AUTHFAIL: user=%s ip=%s time=%d",
username, ip_addr, g_time1());
LOG(LOG_LEVEL_INFO, "AUTHFAIL: user=%s ip=%s time=%ld",
username, ip_addr, (long)time(NULL));
}

/******************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions sesman/sesexec/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ session_start_wrapped(struct login_info *login_info,
sd->win_mgr = window_manager_pid;
sd->x_server = display_pid;
sd->chansrv = chansrv_pid;
sd->start_time = g_time1();
sd->start_time = time(NULL);
status = E_SCP_SCREATE_OK;
}
}
Expand Down Expand Up @@ -860,7 +860,7 @@ session_process_child_exit(struct session_data *sd,
}
else if (pid == sd->win_mgr)
{
int wm_wait_time = g_time1() - sd->start_time;
int wm_wait_time = time(NULL) - sd->start_time;

if (e->reason == E_PXR_STATUS_CODE && e->val == 0)
{
Expand Down
Loading
Loading