XOpenDisplay fails from udev event - xlib

I am trying to open the X display like so:
disp = XOpenDisplay(NULL);
When I run this from my users shell it works fine but if it is run from a udev event (Ubuntu 10.10) the function call returns NULL. I wondered if this has something to do with the differing environment so have tried XOpenDisplay(":0.0") also but no avail.
Does anyone know why this happens?

XOpenDisplay(NULL) would inspect the DISPLAY environment variable, which usually is not set in udev context. To be able to use XOpenDisplay(something) (including something=NULL), you need the access key to the X server instance.
The XAUTHORITY environment variable is to be set to the location of the key file if you want to use XOpenDisplay or any graphical program that makes use of it. If this env var is empty, ~/.Xauthority is used as a fallback.
Trying to start some graphical program from udev is a bad idea (read: something is wrong in your approach and design): You don't know the X display number. You cannot guess it either, because there may be very well more than one X server active. And then you need to have its key file, which is not always possible to determine or obtain either. udev may run as root, but there are things like NFS mounts with root_squash, and the XAUTHORITY variable with which people can relocate their key file.
The way graphical problems work these days is that the user, or the desktop environment s/he is using, has to start a background program specifically listening for certain events. Just so that you cannot simply intrude on people's screens, but have to abide by their event notification system.

Related

Create file or registry key without calling NTDLL.DLL

I know that ntdll is always present in the running process but is there a way (not necessarily supported/stable/guaranteed to work) to create a file/key without ever invoking ntdll functions?
NTDLL is at the bottom of the user-mode hierarchy, some of its functions switch to kernel mode to perform their tasks. If you want to duplicate its code then I suppose there is nothing stopping you from decompiling NtCreateFile to figure out how it works. Keep in mind that on 32-bit Windows there are 3 different instructions used to enter kernel mode (depending on the CPU type), the exact way and where the transition code lives changes between versions and the system call ids change between versions (and even service packs). You can find a list of system call ids here.
I assume you are doing this to avoid people hooking your calls? Detecting your calls? Either way, I can't recommend that you try to do this. Having to test on a huge set of different Windows versions is unmanageable and your software might break on a simple Windows update at any point.
You could create a custom kernel driver that does the work for you but then you are on the hook for getting all the security correct. At least you would have documented functions to call in the kernel.
Technically, registry is stored in %WINDIR%\System32\config / %WINDIR%\SysWOW64\config, excepted your own user's registry which is stored in your own profile, in %USERPROFILE%\NTUSER.DAT.
And now, the problems...
You don't normally have even a read access to this folder, and this is true even from an elevated process. You'll need to change (and mess up a lot...) the permissions to simply read it.
Even for your own registry, you can't open the binary file - "Sharing violation"... So, for system/local machine registries... You can't in fact open ANY registry file for the current machine/session. You would need to shut down your Windows and mount its system drive in another machine/OS to be able to open - and maybe edit - registry files.
Real registry isn't a simple file like the .reg files. It's a database (you can look here for some elements on its structure). Even when having a full access to the binary files, it won't be fun to add something inside "from scratch", without any sotware support.
So, it's technically possible - after all, Windows does it, right? But I doubt that it can be done in a reasonable amount of time, and I simply can't see any benefit from doing that since, as you said, ntdll is ALWAYS present, loaded and available to be used.
If the purpose is to hack the current machine and/or bypass some lack of privileges, it's a hopeless approach, since you'll need even more privileges to do it - like being able to open your case and extract the system drive or being able to boot on another operating system on the same machine... If it's possible, then there is already tools to access the offline Windows, found on a well-known "Boot CD", so still no need to write in registry without any Windows support.

Is there a way to execute a shell script whenever a HID device is connected either directly or over bluetooth?

I'm taking advantage of macOS's hidutil command to perform custom key mappings, as per this link from Apple here. The issue is we're using a KVM which keeps disconnecting, then re-connecting the keyboard and every time that happens, our custom key maps get wiped out and we have to manually re-execute the script.
I'm trying to find if there's some system event we can monitor, or some launch agent or similar that we can utilize to execute the script for us automatically.
If there is any other way you can think of that would persist these settings, that would work too.
Ref: https://developer.apple.com/library/archive/technotes/tn2450/_index.html

Set up permanent flag in OS

I want to create some kind of "permanent flag" in OS, where my program runs. It will be saved somewhere, it will be easliy to check it is set or not. I need that to check, did my program already started before. I thought to base that on just creating empty file, but I do not think it is optimal solution. Can you give me any advice what should I do?
I am targeting only Linux.

Mac OS X app sandbox issue

I am writing a command line tool. There I'd like to create a directory in the /Users/user_name/Library directory. When I run that program from Terminal as a root, then everything is ok, but the problem is when I want to use that tool as a backend for cups (that program is called, also as root, when I print something). The program is called, but in the system.log file I found the error: sandboxd: deny file-write-*. I'm a new Mac OS X programmer and I'm not sure how to fix it. Anybody can help me?
Since Snow Leopard cups is running in a sandbox that prevents it from misbehaving - like accessing user's home. Depending on your OS X version you can get away by using another directory, not user's home. On Lion it is even more restrictive than that. The sandboxing rules are built-in to the cups binary (as opposed to sb scripts), so your only luck may be replacing cups with a version that specifically allows access to what you need in the sandbox (if you really want to go there, the rules are defined by cupsdCreateProfile in cups/scheduler/process.c). AFAICT there is nothing you can do in your application, because sandboxing is designed to prevent exactly what you are trying to do.
Another alternative is to not run cups as root, because sandboxing is only used when cups is run as root.

running Mathematica remotely on macs

Here is what I want to do:
I want to run Mathematica on another Mac from my Mac (both Snow Leopards). I want to do this because the remote Mac has multiple cores/processors while my local Mac is rather shabby. I would like to have the front end still locally (i.e. the graphical interface).
What I've learned:
I used to do this type of thing from multiple Linux machines and was expecting to have similar success for Mac-to-Mac operation. However no such luck.
The problem seems to be a display issue (front end).
Mac front end runs in Aqua while X11 is what is really needed (this is why there is no problem on Unix). While Macs have X11, for some reason Mathematica can't use it.
So how do I get around this issue?
Possible solutions that I have had to rule out are: 1. screen sharing. Not practical since someone else will be using the remote Mac on another account. Screen sharing only uses the active screen. 2. Installing Unix on the remote computer. Not possible in my situation.
Thanks.
You should be able to set up a remote kernel on the other Mac. This is done through the Evaluation > Kernel Configurations menu item. The you can set the remote kernel for a given notebook using Evaluation > Notebook's Kernel or globally via Evaluation > Default Kernel.
I haven't done this in a while, and it's sometimes useful to test things from a terminal with something like
ssh <user>#<remote.machine.com> </path/to/remote/Mathematica.app/Contents/MacOS/MathKernel>
Why not use the command line kernel? I have a script math which does:
#!/bin/bash
rlwrap /Applications/Mathematica.app/Contents/MacOS/MathKernel
I built rlwrap from source, but basically that tool gives you readline behaviors. You can just do
ssh remote-machine /Applications/Mathematica.app/Contents/MacOS/MathKernel
The only solution, I believe, is for you to upgrade to OS X Lion. It allows simultaneous screen sharing sessions where each user can control the screen for their own account:
http://www.apple.com/macosx/whats-new/features.html#screensharing

Resources