Get current date and time in kernel module - c

I'm developing a keyboard tracker to track all the keys pressed by the user along with time.
I found many blogs on getnstimeofday(struct timespec*) but it's deprecated in latest kernel version because of the "year 2038 problem on 32-bit architectures". Its replacement function is ktime_get_real_ts64 but I'm not able to understand it.
Can someone help me to give the basic idea to implement it or if you can share the working code?

You use it pretty much the same way you'd use getnstimeofday:
#include <linux/time.h>
#include <linux/timekeeping.h>
//...
struct timespec64 now;
ktime_get_real_ts64(&now);
struct tm tm_now;
time64_to_tm(now.tv_sec, 0, &tm_now);
//tm_now is similar to the userspace tm

Related

clock_gettime on MacOS

clock_gettime doesn't work on MacOS Sierra anymore. Pretty sure I had this compiling correctly before Xcode 8 came out. I am really kind of stuck on what to do to get it to compile correctly.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(){
struct timespec time1, time2;
clock_gettime(CLOCK_MONOTONIC,&time1);
//Some code I am trying to work out performance of...
clock_gettime(CLOCK_MONOTONIC,&time2);
printf("Time Taken: %ld",time2.tv_nsec - time1.tv_nsec);
}
Code like this simply fails to compile. I've been told the sierra timing library has changed. I get a compiler error for CLOCK_MONOTONIC not being defined and a warning for implicit declaration of clock_gettime, which turns into an error if I define CLOCK_MONOTONIC to something random, as it then just errors out during the linking stage.
Does anyone know of a fix or workaround to get the code compiling and executing?
I don't think CLOCK_MONOTONIC has been around in recent times.
I believe what you want is probably mach_absolute_time() plus a conversion, as documented here; this appears to be monotonic, and absolute (which are two different things as you probably know).
Other useful hints are to be found at the following related (but I think not duplicate) questions:
clock_gettime alternative in Mac OS X (but deals with a different clock type)
Monotonic clock on OSX (not C but objective C)

Adding new System Call in Minix

I am trying to create a new system call in Minix 3.3. At first i just want to create simple printmsg() call that will write "Hello World" on screen.
I looked various tutorials on internet and still couldn't find out solution.
I defined my sys call number in callnr.h like this #define PM_PRINTMSG (PM BASE + 48) and i increased number of sys calls #define NR_PM_CALLS 49.
In table.c I added CALL(PM_PRINTMSG) = doprintmsg.
In proto.h I described function prototype `int do_printmsg(void);
Function implementation is written in misc.c. I added #include <stdio.h> and made Hello World function int do printmsg(){ printf("I am a system call"); return 0; }
When I test my system call in user program _syscall(PM_PROC_NR, PM_PRINTMSG, &m); I don't get any errors but no printf is displayed.
So, is it possible to printf messages from system calls since i had to add <stdio.h> myself in misc.c or i missed some steps. I forgot to mention that i go in /usr/src/releasetools and type make services and make install respectively to recompile kernel.
I figured out what was the problem, so i will post answer if someone needs this in future. I did everything well in this example but i failed to compile kernel.
The location was correct which is usr/src/releasetools, but command needed is make hdboot. Also i figured out my PC somehow wasnt working well with this virtual machines and i had many errors while compiling even though i didn't change anything. When i switched to laptop everything worked fine.
My conclusion is sometimes there is just something wrong on your machine so you should try and test problems on different ones
In my opinion, with the continuous evolution of MINIX 3 and its series, it will be wise to only follow the developer's guide directly from the minix3.org website here
Although you managed to solve the problem yourself, the latest version of MINIX3 (MINIX 3.4) will follow a more advanced and suitable approach.
Please visit the link to learn more.
Many regards.
Ola

Cygwin undefined reference to adjtime

this is my first time posting a question here so be kind :).
I have a problem porting a linux application to windows (windows 7 64-bit) using cygwin. I had to rewrite some code that is not supported under windows and that doesn´t exist in cygwin (like adjtimex) and I have manage to compile the code but when I am building the application .exe file (by using makefiles) I get a link error:
gcc -Wl,-Map,ppsi.map2 -o ppsi ppsi.o -lrt -lc
ppsi.o: In function `win_time_adjust':
ppsi/time-win/win-time.c:74: undefined reference to `adjtime'
ppsi/time-win/win-time.c:74:(.text+0x1905): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `adjtime'
adjtime function is defined in which is included in the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h> /* timex.h not available on windows so we use sys/time.h*/
#include <ppsi/ppsi.h>
Here is how a part of the code looks like:
static int win_time_adjust(struct pp_instance *ppi, long offset_ns, long freq_ppb){
struct timeval t;
int ret;
t.tv_sec = 0;
t.tv_usec = 0;
if (offset_ns) {
t.tv_sec = offset_ns / 1000000;
t.tv_usec = offset_ns / 1000; /*microseconds */
}
ret = adjtime(&t, NULL);
pp_diag(ppi, time, 1, "%s: %li %li\n", __func__, offset_ns, freq_ppb);
return ret;
}
I have encountered a similar problem before and I solved by linking the missing library when compiling the application. I tired the same here and linked libc (since adjtime is suppose to be there) -lc when compiling but got the same error. I unsure how adjtime is undefined and I wonder if there is some cygwin library I should link instead?
I use cygwin (gcc version 4.9.2) when compiling
From my understanding, adjtime is a Linux-ism which likely crawled its way into the BSD world via FreeBSD. It's certainly not documented by any of the C or POSIX standards.
Even those which are documented by the C and POSIX standards often aren't implemented correctly in the Microsoft world, so it wouldn't surprise me that this function doesn't exist.
I'm very interested to hear more about this project of yours. Though a simplistic wrapper of some implementation-defined stuff can be written for the purposes of portability, I can only imagine highly niche usecases, for which there are better alternatives:
If you need something with a large amount of good, secure entropy to seed your pseudo-random number generator, you should use something provided by a cryptographic library.
If you want to conduct performance tests, you should learn how to use the profiler rather than rolling your own profiling code. Also, every "performance test" should be conducted as though through the eyes of a scientist observing the very first time; it's generally best not to let tests from previous projects guide optimisation for future projects. Instead, let the profile of the current project guide optimisation for the current project (on the current hardware), and you'll be cheering before you know it!

OS X lock screen with pure C?

I know this is possible, because I have a binary I wrote a couple years ago to do exactly this. Unfortunately, I didn't save the source code.
I know that it was written in pure C, and I called against an OS X API and was able to lock the screen. Googling for "pure C OS X lock screen" and similar strings isn't getting me much; I saw documentation on how to do it in Objective C but that's not what I'm looking for.
Anyone have any idea how I accomplished this previously?
One way to do this is by using a call to system():
#include <stdlib.h>
int main(void) {
system("/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend");
return 0;
}
You're probably thinking of CGSCreateLoginSession() -- it's a private function and not documented by Apple. See this question for an example of its use.

Looking for a good example of Linux mouse programming in C

I am trying to get my feet wet in C, and I am curious to know how you can control the mouse pointer, and simulate a click, etc. To help myself learn, this would be a simple example where the keyboard arrows & spacebar would simulate these effects. I started here:
http://www.cprogrammingreference.com/Tutorials/Advance_Tutorials/mouseprogramming.php
But since I am in a linux environment, these obviously fail compiling.
#include <dos.h>
#include <graphics.h>
Is there a better example for me to start with in linux? Is it as simple as including different header files and using the same code, or are the routines completely different for linux (as one would expect)?
UPDATE Linux only would work great, as I am just learning.
http://snippets.dzone.com/posts/show/2750
That shows a quick snippet on simulating a mouse click. From there you can look into the other functions in the Xlib library.
Also there's a tool that does the same things you're talking about, you might want to take a look at its source if you want to know more about it.
http://www.semicomplete.com/projects/xdotool/

Resources