2000-02-18 03:38:33 +08:00
|
|
|
/* times.cc
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2007-07-04 18:43:29 +08:00
|
|
|
#define __timezonefunc__
|
2009-01-03 13:12:22 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/timeb.h>
|
2018-02-07 20:07:44 +08:00
|
|
|
#include <sys/param.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <utime.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-18 01:08:10 +08:00
|
|
|
#include <unistd.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2005-02-20 19:44:32 +08:00
|
|
|
#include "dtable.h"
|
2005-10-20 00:50:43 +08:00
|
|
|
#include "cygheap.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "pinfo.h"
|
2011-05-18 01:08:10 +08:00
|
|
|
#include "thread.h"
|
2003-12-24 00:26:31 +08:00
|
|
|
#include "cygtls.h"
|
2007-08-14 22:48:52 +08:00
|
|
|
#include "ntdll.h"
|
2018-11-27 00:47:53 +08:00
|
|
|
#include "spinlock.h"
|
2000-03-19 12:14:07 +08:00
|
|
|
|
2013-06-14 23:41:17 +08:00
|
|
|
static inline void __attribute__ ((always_inline))
|
|
|
|
get_system_time (PLARGE_INTEGER systime)
|
|
|
|
{
|
|
|
|
wincap.has_precise_system_time ()
|
2020-03-11 20:23:55 +08:00
|
|
|
? GetSystemTimePreciseAsFileTime ((LPFILETIME) systime)
|
2013-06-14 23:41:17 +08:00
|
|
|
: GetSystemTimeAsFileTime ((LPFILETIME) systime);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Cygwin internal */
|
2022-06-07 00:00:45 +08:00
|
|
|
static uint64_t
|
2013-06-14 17:09:41 +08:00
|
|
|
__to_clock_t (PLARGE_INTEGER src, int flag)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-06-14 17:09:41 +08:00
|
|
|
uint64_t total = src->QuadPart;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Convert into clock ticks - the total is in 10ths of a usec. */
|
|
|
|
if (flag)
|
|
|
|
total -= FACTOR;
|
|
|
|
|
2018-02-07 20:07:44 +08:00
|
|
|
total /= NS100PERSEC / CLOCKS_PER_SEC;
|
2000-02-18 03:38:33 +08:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* times: POSIX 4.5.2.1 */
|
|
|
|
extern "C" clock_t
|
2002-10-21 09:00:58 +08:00
|
|
|
times (struct tms *buf)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-06-14 17:09:41 +08:00
|
|
|
static SYSTEM_TIMEOFDAY_INFORMATION stodi;
|
|
|
|
KERNEL_USER_TIMES kut;
|
|
|
|
LARGE_INTEGER ticks;
|
2014-08-22 17:21:33 +08:00
|
|
|
clock_t tc = (clock_t) -1;
|
2007-02-23 00:22:38 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
/* Fetch boot time if we haven't already. */
|
|
|
|
if (!stodi.BootTime.QuadPart)
|
|
|
|
NtQuerySystemInformation (SystemTimeOfDayInformation,
|
|
|
|
&stodi, sizeof stodi, NULL);
|
|
|
|
|
|
|
|
NtQueryInformationProcess (NtCurrentProcess (), ProcessTimes,
|
|
|
|
&kut, sizeof kut, NULL);
|
|
|
|
get_system_time (&ticks);
|
|
|
|
|
|
|
|
/* uptime */
|
|
|
|
ticks.QuadPart -= stodi.BootTime.QuadPart;
|
|
|
|
/* ticks is in in 100ns, convert to clock ticks. */
|
2018-02-07 20:07:44 +08:00
|
|
|
tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NS100PERSEC);
|
2014-08-22 17:21:33 +08:00
|
|
|
|
2019-09-16 00:28:21 +08:00
|
|
|
/* Linux allows a NULL buf and just returns tc in that case, so
|
|
|
|
mimic that */
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
buf->tms_stime = __to_clock_t (&kut.KernelTime, 0);
|
|
|
|
buf->tms_utime = __to_clock_t (&kut.UserTime, 0);
|
|
|
|
timeval_to_filetime (&myself->rusage_children.ru_stime, &kut.KernelTime);
|
|
|
|
buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1);
|
|
|
|
timeval_to_filetime (&myself->rusage_children.ru_utime, &kut.UserTime);
|
|
|
|
buf->tms_cutime = __to_clock_t (&kut.UserTime, 1);
|
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
}
|
|
|
|
__except (EFAULT)
|
|
|
|
{
|
|
|
|
tc = (clock_t) -1;
|
|
|
|
}
|
|
|
|
__endtry
|
2013-06-17 16:33:41 +08:00
|
|
|
syscall_printf ("%D = times(%p)", tc, buf);
|
2007-02-23 00:22:38 +08:00
|
|
|
return tc;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2004-09-10 16:30:51 +08:00
|
|
|
EXPORT_ALIAS (times, _times)
|
2002-10-21 09:00:58 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* settimeofday: BSD */
|
|
|
|
extern "C" int
|
2001-02-27 06:36:09 +08:00
|
|
|
settimeofday (const struct timeval *tv, const struct timezone *tz)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-02-27 06:36:09 +08:00
|
|
|
SYSTEMTIME st;
|
|
|
|
struct tm *ptm;
|
2014-08-22 17:21:33 +08:00
|
|
|
int res = -1;
|
2011-05-09 16:57:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2011-05-09 16:57:46 +08:00
|
|
|
{
|
2018-02-07 20:07:44 +08:00
|
|
|
if (tv->tv_usec < 0 || tv->tv_usec >= USPERSEC)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptm = gmtime (&tv->tv_sec);
|
|
|
|
st.wYear = ptm->tm_year + 1900;
|
|
|
|
st.wMonth = ptm->tm_mon + 1;
|
|
|
|
st.wDayOfWeek = ptm->tm_wday;
|
|
|
|
st.wDay = ptm->tm_mday;
|
|
|
|
st.wHour = ptm->tm_hour;
|
|
|
|
st.wMinute = ptm->tm_min;
|
|
|
|
st.wSecond = ptm->tm_sec;
|
2018-02-13 05:08:42 +08:00
|
|
|
st.wMilliseconds = tv->tv_usec / (USPERSEC / MSPERSEC);
|
2014-08-22 17:21:33 +08:00
|
|
|
|
|
|
|
res = -!SetSystemTime (&st);
|
|
|
|
if (res)
|
|
|
|
set_errno (EPERM);
|
2011-05-09 16:57:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT)
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
__endtry
|
2013-04-23 17:44:36 +08:00
|
|
|
syscall_printf ("%R = settimeofday(%p, %p)", res, tv, tz);
|
2001-02-27 06:36:09 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 03:03:02 +08:00
|
|
|
/* stime: SVr4 */
|
|
|
|
extern "C" int
|
|
|
|
stime (const time_t *t)
|
|
|
|
{
|
|
|
|
struct timeval tv = { *t, 0 };
|
|
|
|
return settimeofday(&tv, NULL);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* timezone: standards? */
|
|
|
|
extern "C" char *
|
2005-11-18 23:57:16 +08:00
|
|
|
timezone (void)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-12-24 00:26:31 +08:00
|
|
|
char *b = _my_tls.locals.timezone_buf;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
tzset ();
|
|
|
|
__small_sprintf (b,"GMT%+d:%02d", (int) (-_timezone / 3600), (int) (abs (_timezone / 60) % 60));
|
2000-02-18 03:38:33 +08:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2013-06-14 17:09:41 +08:00
|
|
|
totimeval (struct timeval *dst, PLARGE_INTEGER src, int sub, int flag)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-06-14 17:09:41 +08:00
|
|
|
int64_t x = __to_clock_t (src, flag);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2018-02-07 20:07:44 +08:00
|
|
|
x *= (int64_t) USPERSEC / CLOCKS_PER_SEC; /* Turn x into usecs */
|
|
|
|
x -= (int64_t) sub * USPERSEC;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2018-02-07 20:07:44 +08:00
|
|
|
dst->tv_usec = x % USPERSEC; /* And split */
|
|
|
|
dst->tv_sec = x / USPERSEC;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-02-15 15:05:05 +08:00
|
|
|
/* FIXME: Make thread safe */
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" int
|
2013-11-25 19:38:08 +08:00
|
|
|
gettimeofday (struct timeval *__restrict tv, void *__restrict tzvp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2007-07-07 04:09:23 +08:00
|
|
|
struct timezone *tz = (struct timezone *) tzvp;
|
2002-03-28 13:20:53 +08:00
|
|
|
static bool tzflag;
|
2018-11-27 20:47:02 +08:00
|
|
|
LONGLONG now = get_clock (CLOCK_REALTIME)->usecs ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2018-02-07 20:07:44 +08:00
|
|
|
tv->tv_sec = now / USPERSEC;
|
|
|
|
tv->tv_usec = now % USPERSEC;
|
2002-03-28 12:56:06 +08:00
|
|
|
|
|
|
|
if (tz != NULL)
|
|
|
|
{
|
2002-05-12 09:50:38 +08:00
|
|
|
if (!tzflag)
|
|
|
|
{
|
2002-09-22 11:38:57 +08:00
|
|
|
tzset ();
|
2002-05-12 09:50:38 +08:00
|
|
|
tzflag = true;
|
|
|
|
}
|
2002-03-28 12:56:06 +08:00
|
|
|
tz->tz_minuteswest = _timezone / 60;
|
|
|
|
tz->tz_dsttime = _daylight;
|
|
|
|
}
|
|
|
|
|
2002-02-15 15:05:05 +08:00
|
|
|
return 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2004-09-10 16:30:51 +08:00
|
|
|
EXPORT_ALIAS (gettimeofday, _gettimeofday)
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
/* Cygwin internal */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2013-06-14 17:09:41 +08:00
|
|
|
timespec_to_filetime (const struct timespec *time_in, PLARGE_INTEGER out)
|
2008-04-24 17:59:54 +08:00
|
|
|
{
|
|
|
|
if (time_in->tv_nsec == UTIME_OMIT)
|
2013-06-14 17:09:41 +08:00
|
|
|
out->QuadPart = 0;
|
2008-04-24 17:59:54 +08:00
|
|
|
else
|
2018-02-07 20:07:44 +08:00
|
|
|
out->QuadPart = time_in->tv_sec * NS100PERSEC
|
|
|
|
+ time_in->tv_nsec / (NSPERSEC/NS100PERSEC) + FACTOR;
|
2008-04-24 17:59:54 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Cygwin internal */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2013-06-14 17:09:41 +08:00
|
|
|
timeval_to_filetime (const struct timeval *time_in, PLARGE_INTEGER out)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2018-02-07 20:07:44 +08:00
|
|
|
out->QuadPart = time_in->tv_sec * NS100PERSEC
|
|
|
|
+ time_in->tv_usec * (NS100PERSEC/USPERSEC) + FACTOR;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-07 23:16:51 +08:00
|
|
|
/* Cygwin internal */
|
|
|
|
bool
|
|
|
|
timeval_to_ms (const struct timeval *time_in, DWORD &ms)
|
|
|
|
{
|
|
|
|
if (time_in->tv_sec < 0 || time_in->tv_usec < 0
|
|
|
|
|| time_in->tv_usec >= USPERSEC)
|
|
|
|
return false;
|
|
|
|
if ((time_in->tv_sec == 0 && time_in->tv_usec == 0)
|
2018-02-13 05:08:42 +08:00
|
|
|
|| time_in->tv_sec >= (time_t) (INFINITE / MSPERSEC))
|
2018-02-07 23:16:51 +08:00
|
|
|
ms = INFINITE;
|
|
|
|
else
|
2018-02-13 05:08:42 +08:00
|
|
|
ms = time_in->tv_sec * MSPERSEC
|
|
|
|
+ (time_in->tv_usec + (USPERSEC / MSPERSEC) - 1)
|
|
|
|
/ (USPERSEC / MSPERSEC);
|
2018-02-07 23:16:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Cygwin internal */
|
2022-06-07 00:00:45 +08:00
|
|
|
static timeval
|
2000-02-18 03:38:33 +08:00
|
|
|
time_t_to_timeval (time_t in)
|
|
|
|
{
|
|
|
|
timeval res;
|
|
|
|
res.tv_sec = in;
|
|
|
|
res.tv_usec = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
/* Cygwin internal */
|
|
|
|
static const struct timespec *
|
|
|
|
timeval_to_timespec (const struct timeval *tvp, struct timespec *tmp)
|
|
|
|
{
|
|
|
|
if (!tvp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp[0].tv_sec = tvp[0].tv_sec;
|
2018-02-07 20:07:44 +08:00
|
|
|
tmp[0].tv_nsec = tvp[0].tv_usec * (NSPERSEC/USPERSEC);
|
2008-04-24 17:59:54 +08:00
|
|
|
if (tmp[0].tv_nsec < 0)
|
|
|
|
tmp[0].tv_nsec = 0;
|
2018-02-07 20:07:44 +08:00
|
|
|
else if (tmp[0].tv_nsec >= NSPERSEC)
|
|
|
|
tmp[0].tv_nsec = NSPERSEC - 1;
|
2008-04-24 17:59:54 +08:00
|
|
|
|
|
|
|
tmp[1].tv_sec = tvp[1].tv_sec;
|
2018-02-07 20:07:44 +08:00
|
|
|
tmp[1].tv_nsec = tvp[1].tv_usec * (NSPERSEC/USPERSEC);
|
2008-04-24 17:59:54 +08:00
|
|
|
if (tmp[1].tv_nsec < 0)
|
|
|
|
tmp[1].tv_nsec = 0;
|
2018-02-07 20:07:44 +08:00
|
|
|
else if (tmp[1].tv_nsec >= NSPERSEC)
|
|
|
|
tmp[1].tv_nsec = NSPERSEC - 1;
|
2008-04-24 17:59:54 +08:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Cygwin internal */
|
|
|
|
/* Convert a Win32 time to "UNIX" format. */
|
2022-06-07 00:00:45 +08:00
|
|
|
time_t
|
2013-06-14 17:09:41 +08:00
|
|
|
to_time_t (PLARGE_INTEGER ptr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* A file time is the number of 100ns since jan 1 1601
|
|
|
|
stuffed into two long words.
|
|
|
|
A time_t is the number of seconds since jan 1 1970. */
|
|
|
|
|
2013-06-14 17:09:41 +08:00
|
|
|
int64_t x = ptr->QuadPart;
|
2000-10-21 05:45:50 +08:00
|
|
|
|
|
|
|
/* pass "no time" as epoch */
|
|
|
|
if (x == 0)
|
|
|
|
return 0;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
x -= FACTOR; /* number of 100ns between 1601 and 1970 */
|
2018-02-07 20:07:44 +08:00
|
|
|
x /= NS100PERSEC; /* number of 100ns in a second */
|
2000-02-18 03:38:33 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2002-06-06 23:35:09 +08:00
|
|
|
/* Cygwin internal */
|
|
|
|
/* Convert a Win32 time to "UNIX" timestruc_t format. */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2013-06-14 17:09:41 +08:00
|
|
|
to_timestruc_t (PLARGE_INTEGER ptr, timestruc_t *out)
|
2002-06-06 23:35:09 +08:00
|
|
|
{
|
|
|
|
/* A file time is the number of 100ns since jan 1 1601
|
|
|
|
stuffed into two long words.
|
|
|
|
A timestruc_t is the number of seconds and microseconds since jan 1 1970
|
|
|
|
stuffed into a time_t and a long. */
|
2013-06-14 17:09:41 +08:00
|
|
|
int64_t x = ptr->QuadPart;
|
2002-06-06 23:35:09 +08:00
|
|
|
|
|
|
|
/* pass "no time" as epoch */
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
out->tv_sec = 0;
|
|
|
|
out->tv_nsec = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x -= FACTOR; /* number of 100ns between 1601 and 1970 */
|
2018-02-07 20:07:44 +08:00
|
|
|
out->tv_sec = x / NS100PERSEC;
|
|
|
|
out->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
|
2002-06-06 23:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
/* Get the current time as a "UNIX" timestruc_t format. */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2002-06-06 23:35:09 +08:00
|
|
|
time_as_timestruc_t (timestruc_t * out)
|
|
|
|
{
|
2013-06-14 17:09:41 +08:00
|
|
|
LARGE_INTEGER systime;
|
2002-06-06 23:35:09 +08:00
|
|
|
|
2013-06-14 23:41:17 +08:00
|
|
|
get_system_time (&systime);
|
2013-06-14 17:09:41 +08:00
|
|
|
to_timestruc_t (&systime, out);
|
2002-06-06 23:35:09 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* time: POSIX 4.5.1.1, C 4.12.2.4 */
|
|
|
|
/* Return number of seconds since 00:00 UTC on jan 1, 1970 */
|
2002-10-21 09:00:58 +08:00
|
|
|
extern "C" time_t
|
2000-02-18 03:38:33 +08:00
|
|
|
time (time_t * ptr)
|
|
|
|
{
|
|
|
|
time_t res;
|
2013-06-14 17:09:41 +08:00
|
|
|
LARGE_INTEGER systime;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2013-06-14 23:41:17 +08:00
|
|
|
get_system_time (&systime);
|
2013-06-14 17:09:41 +08:00
|
|
|
res = to_time_t (&systime);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (ptr)
|
|
|
|
*ptr = res;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
syscall_printf ("%d = time(%p)", res, ptr);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
int
|
|
|
|
utimens_worker (path_conv &win32, const struct timespec *tvp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-02-20 19:44:32 +08:00
|
|
|
int res = -1;
|
2005-10-24 07:47:45 +08:00
|
|
|
|
|
|
|
if (win32.error)
|
|
|
|
set_errno (win32.error);
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-10-24 07:47:45 +08:00
|
|
|
fhandler_base *fh = NULL;
|
|
|
|
bool fromfd = false;
|
|
|
|
|
2005-10-24 23:09:07 +08:00
|
|
|
cygheap_fdenum cfd (true);
|
2005-10-24 07:47:45 +08:00
|
|
|
while (cfd.next () >= 0)
|
|
|
|
if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE)
|
2007-08-14 22:48:52 +08:00
|
|
|
&& RtlEqualUnicodeString (cfd->pc.get_nt_native_path (),
|
|
|
|
win32.get_nt_native_path (),
|
2008-07-17 04:20:45 +08:00
|
|
|
cfd->pc.objcaseinsensitive ()))
|
2005-10-24 07:47:45 +08:00
|
|
|
{
|
|
|
|
fh = cfd;
|
|
|
|
fromfd = true;
|
|
|
|
break;
|
|
|
|
}
|
2005-10-20 00:50:43 +08:00
|
|
|
|
2005-10-24 07:47:45 +08:00
|
|
|
if (!fh)
|
|
|
|
{
|
|
|
|
if (!(fh = build_fh_pc (win32)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (fh->error ())
|
|
|
|
{
|
2010-01-12 18:14:59 +08:00
|
|
|
debug_printf ("got %d error from build_fh_pc", fh->error ());
|
2005-10-24 07:47:45 +08:00
|
|
|
set_errno (fh->error ());
|
2007-08-14 22:48:52 +08:00
|
|
|
}
|
2005-10-24 07:47:45 +08:00
|
|
|
}
|
2005-10-20 00:50:43 +08:00
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
res = fh->utimens (tvp);
|
2005-10-24 07:47:45 +08:00
|
|
|
|
|
|
|
if (!fromfd)
|
|
|
|
delete fh;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-10-20 00:50:43 +08:00
|
|
|
error:
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%R = utimes(%S, %p)", res, win32.get_nt_native_path (), tvp);
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-10-20 22:26:23 +08:00
|
|
|
/* utimes: POSIX/SUSv3 */
|
|
|
|
extern "C" int
|
2021-01-27 00:26:35 +08:00
|
|
|
utimes (const char *path, const struct timeval tvp[2])
|
2005-10-20 22:26:23 +08:00
|
|
|
{
|
2007-08-14 22:48:52 +08:00
|
|
|
path_conv win32 (path, PC_POSIX | PC_SYM_FOLLOW, stat_suffixes);
|
2008-04-24 17:59:54 +08:00
|
|
|
struct timespec tmp[2];
|
|
|
|
return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
|
2005-10-20 22:26:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BSD */
|
|
|
|
extern "C" int
|
2021-01-27 00:26:35 +08:00
|
|
|
lutimes (const char *path, const struct timeval tvp[2])
|
2005-10-20 22:26:23 +08:00
|
|
|
{
|
2007-08-14 22:48:52 +08:00
|
|
|
path_conv win32 (path, PC_POSIX | PC_SYM_NOFOLLOW, stat_suffixes);
|
2008-04-24 17:59:54 +08:00
|
|
|
struct timespec tmp[2];
|
|
|
|
return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
|
2005-10-20 22:26:23 +08:00
|
|
|
}
|
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
/* futimens: POSIX/SUSv4 */
|
2005-10-20 22:26:23 +08:00
|
|
|
extern "C" int
|
2021-01-27 00:26:35 +08:00
|
|
|
futimens (int fd, const struct timespec tvp[2])
|
2005-10-20 22:26:23 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
|
|
|
res = -1;
|
2005-11-08 01:08:08 +08:00
|
|
|
else if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE))
|
2008-04-24 17:59:54 +08:00
|
|
|
res = cfd->utimens (tvp);
|
2005-11-08 01:08:08 +08:00
|
|
|
else
|
2008-04-24 17:59:54 +08:00
|
|
|
res = utimens_worker (cfd->pc, tvp);
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%d = futimens(%d, %p)", res, fd, tvp);
|
2005-10-20 22:26:23 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-24 17:59:54 +08:00
|
|
|
/* BSD */
|
|
|
|
extern "C" int
|
2021-01-27 00:26:35 +08:00
|
|
|
futimes (int fd, const struct timeval tvp[2])
|
2008-04-24 17:59:54 +08:00
|
|
|
{
|
|
|
|
struct timespec tmp[2];
|
|
|
|
return futimens (fd, timeval_to_timespec (tvp, tmp));
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* utime: POSIX 5.6.6.1 */
|
2002-10-21 09:00:58 +08:00
|
|
|
extern "C" int
|
2005-02-09 04:59:41 +08:00
|
|
|
utime (const char *path, const struct utimbuf *buf)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
struct timeval tmp[2];
|
|
|
|
|
|
|
|
if (buf == 0)
|
|
|
|
return utimes (path, 0);
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("incoming utime act %lx", buf->actime);
|
2000-02-18 03:38:33 +08:00
|
|
|
tmp[0] = time_t_to_timeval (buf->actime);
|
|
|
|
tmp[1] = time_t_to_timeval (buf->modtime);
|
|
|
|
|
|
|
|
return utimes (path, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ftime: standards? */
|
2002-10-21 09:00:58 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
ftime (struct timeb *tp)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
if (gettimeofday (&tv, &tz) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tp->time = tv.tv_sec;
|
|
|
|
tp->millitm = tv.tv_usec / 1000;
|
|
|
|
tp->timezone = tz.tz_minuteswest;
|
|
|
|
tp->dstflag = tz.tz_dsttime;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-19 13:46:54 +08:00
|
|
|
extern "C" int
|
|
|
|
clock_gettime (clockid_t clk_id, struct timespec *tp)
|
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
clk_t *clock = get_clock (clk_id);
|
2011-05-18 01:08:10 +08:00
|
|
|
|
2018-11-27 20:47:02 +08:00
|
|
|
if (!clock)
|
2011-05-18 01:08:10 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2011-05-18 01:08:10 +08:00
|
|
|
}
|
2018-11-27 20:47:02 +08:00
|
|
|
__try
|
2004-01-19 13:46:54 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
return clock->nsecs (clk_id, tp);
|
2004-01-19 13:46:54 +08:00
|
|
|
}
|
2018-11-27 20:47:02 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2004-01-19 13:46:54 +08:00
|
|
|
}
|
2005-11-12 00:42:15 +08:00
|
|
|
|
2011-05-09 16:57:46 +08:00
|
|
|
extern "C" int
|
|
|
|
clock_settime (clockid_t clk_id, const struct timespec *tp)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
2011-05-18 01:08:10 +08:00
|
|
|
if (CLOCKID_IS_PROCESS (clk_id) || CLOCKID_IS_THREAD (clk_id))
|
|
|
|
/* According to POSIX, the privileges to set a particular clock
|
|
|
|
* are implementation-defined. On Linux, CPU-time clocks are not
|
|
|
|
* settable; do the same here.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
set_errno (EPERM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-27 20:47:02 +08:00
|
|
|
if (clk_id != CLOCK_REALTIME_COARSE && clk_id != CLOCK_REALTIME)
|
2011-05-09 16:57:46 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-27 20:47:02 +08:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
tv.tv_sec = tp->tv_sec;
|
|
|
|
tv.tv_usec = tp->tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
__except (EFAULT)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
__endtry
|
2011-05-09 16:57:46 +08:00
|
|
|
|
|
|
|
return settimeofday (&tv, NULL);
|
|
|
|
}
|
|
|
|
|
2018-11-27 20:47:02 +08:00
|
|
|
extern "C" int
|
|
|
|
clock_getres (clockid_t clk_id, struct timespec *tp)
|
2010-08-10 00:47:48 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
clk_t *clock = get_clock (clk_id);
|
2010-08-10 00:47:48 +08:00
|
|
|
|
2018-11-27 20:47:02 +08:00
|
|
|
if (!clock)
|
2005-11-12 00:42:15 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2005-11-12 00:42:15 +08:00
|
|
|
}
|
2018-11-27 20:47:02 +08:00
|
|
|
__try
|
2011-05-18 01:08:10 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
clock->resolution (tp);
|
2011-05-18 01:08:10 +08:00
|
|
|
}
|
2018-11-27 20:47:02 +08:00
|
|
|
__except (EFAULT)
|
2005-11-12 00:42:15 +08:00
|
|
|
{
|
2018-11-27 20:47:02 +08:00
|
|
|
return -1;
|
2005-11-12 00:42:15 +08:00
|
|
|
}
|
2018-11-27 20:47:02 +08:00
|
|
|
__endtry
|
2005-11-12 00:42:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
clock_setres (clockid_t clk_id, struct timespec *tp)
|
|
|
|
{
|
2018-11-27 20:59:37 +08:00
|
|
|
/* Don't use this function. It only exists in QNX. Just return
|
|
|
|
success on CLOCK_REALTIME for backward compat. */
|
2005-11-12 00:42:15 +08:00
|
|
|
if (clk_id != CLOCK_REALTIME)
|
|
|
|
{
|
2010-08-10 00:47:48 +08:00
|
|
|
set_errno (EINVAL);
|
2005-11-12 00:42:15 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-18 01:08:10 +08:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
clock_getcpuclockid (pid_t pid, clockid_t *clk_id)
|
|
|
|
{
|
2017-09-17 10:04:19 +08:00
|
|
|
if (pid != 0)
|
|
|
|
{
|
|
|
|
pinfo p (pid);
|
|
|
|
if (!p || !p->exists ())
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
2011-05-18 01:08:10 +08:00
|
|
|
*clk_id = (clockid_t) PID_TO_CLOCKID (pid);
|
|
|
|
return 0;
|
|
|
|
}
|