atarijookie / ce-atari

Official repository for Cosmos Ex software
http://joo.kie.sk/?page_id=384
34 stars 9 forks source link

Block USB keyboard in linux #134

Open atarijookie opened 7 years ago

atarijookie commented 7 years ago

I swear to flying spaghetti monster that I've created this one, but I can't find it. If it's here somewhere, we can link them and close this one.

When using USB keyboard through CE, the input not only goes to ST, but also to linux console on RPi, which is auto-logged in in Raspbian.

Things to do: 1) disable auto login, so user won't be logged in automatically (seen through HDMI) 2) disable linux console input from USB, add some switching between linux console and Atari IKDB

@mikrosk - do you have any idea how to solve 2), or at least do the preparation for switching that keyboard, like you're currently doing for your IKBD fake joysticks switching?

mikrosk commented 7 years ago

If Linux ce_conf works the same way the Atari version does (i.e. sending 'alive' pings to the CE), you'll get this for free in #73. I have it implemented since a couple of weeks ago.

mikrosk commented 7 years ago

But I'm not sure what are you trying to prevent? So if I have a text editor open on Atari and I run CE_CONF in Linux, you don't want to see moving down in the text editor if I'm moving down in the menu?

atarijookie commented 7 years ago

But I'm not sure what are you trying to prevent?

Currently: Open a text editor / e-mail editor / game, and you - for some magical reasons - want to write a readme.txt / some e-mail / your geek name containing 'rm -rf /' and bam - you just unknowingly deleted your RPi linux files, while you were typing something on Atari through USB keyboard. The same goes for 'shutdown', 'reboot' and so on.

New behavior: Until you press the magical combination, you can type on Atari 'rm -rf /' and not delete your whole linux in CE.

atarijookie commented 7 years ago

PS: replying to this was difficult: https://www.youtube.com/watch?v=_TwCIxy1ExE

tin-nl commented 7 years ago

Incidently I just observed a similar issue: I rebooted my CE during some tests by accident just by pressing CTRL-ALT-DEL via USB during boot :) So if say, someone is waiting eagerly for CE to boot (cough @mikrosk cough ;-) ), he might reboot the CE while rebooting his ST via keyboard...

And: if ce_main is needed to activate console input via keyboard, troubleshooting a device might get problematic...

atarijookie commented 7 years ago

I rebooted my CE during some tests by accident just by pressing CTRL-ALT-DEL via USB during boot :)

Does that Raspbian / Yocto react to Ctrl-Alt-Del? I didn't know that :)

And: if ce_main is needed to activate console input via keyboard, troubleshooting a device might get problematic...

Only for the troubleshooting on TV + USB keyboard - the ssh to device shouldn't be affected. If ce_main would be running, then even switching the for linux on and off shouldn't be a problem, the only case would be a problem when the ce_main refuses to run for some reason (or keeps terminating, when HW doesn't respond).

tin-nl commented 7 years ago

Does that Raspbian / Yocto react to Ctrl-Alt-Del? I didn't know that :)

Yeah, surprising news to me, too :) Yocto isn't affected by this.

Only for the troubleshooting on TV + USB keyboard - the ssh to device shouldn't be affected.

Yeah, I know. But that's (HDMI/Kbd) exactly the thing that people w/o network or with borked up CE/network config might need to unbrick their device. No idea for a better solution atm, though :/

mikrosk commented 7 years ago

Hehe, this is a fascinating problem, had no idea at all. I use Atari keyboard for my IKBD stuff (with occasional testing for its USB counterpart but never for typing), wow.

Well, if you says that you're actually typing in command line, the problem lies elsewhere, not in CE_CONF. You must first disable autologin (as you say) plus you must somehow prevent even having login prompt at all. And frankly, I have no clue how to achieve this.

mikrosk commented 7 years ago

After some thinking -- we need that autologin because of "F8" functionality (i.e. switching to bash from Atari's CE_CONF)? If so, this is perhaps fixable with asking for login/password instead of skipping directly to command line.

But about disabling login prompt after boot... no clue. We could perhaps autostart an app in the foreground (!) which would eat all input (which could be possible to terminate via CTRL+C on HDMI) ? (with this hack we perhaps wouldn't need to disable autologin, just open another terminal on F8).

atarijookie commented 7 years ago

We need that autologin because of "F8" functionality (i.e. switching to bash from Atari's CE_CONF)?

NO, that is done through fork() and exec(), that HDMI console is not needed, unless you want to try to resolve some issue with CE on TV.

I wouldn't disable login prompt, I for now I would just hope nobody writes 'root[enter]ce[enter]' on USB keyboard while doing something on Atari ;)

Btw., if I remember correctly, the typical login program could be replaced with something else, which would just wait for a special key combination (e.g. Ctrl - Shift - F8) and that one would exec then the typical login prompt...

But I guess leaving it at login prompt would be probably just fine most of the time.

mikrosk commented 7 years ago

What about that idea of yours, the magic hotkey? Can't we replace /bin/login with a program which listens for LCTRL+LALT+RCTRL+RALT+DEL (impossible to do on Atari keyboard) and then show the login prompt? And if you reboot/power cycle the device, you're again in the fake login.

miniupnp commented 7 years ago

F11 or F12 are good magic hotkeys too ;)

mikrosk commented 7 years ago

F11 or F12 are good magic hotkeys too ;)

Not really, they are mapped to UNDO/HELP (not in master but in my branch for #73)

miniupnp commented 7 years ago

why not map Page Up / Page Down to Undo/help ? It is what hatari does

mikrosk commented 7 years ago

Historically I remember it from Eiffel that way -- PU/PD was mapped to SHIFT+UP/SHIFT+DOWN (something I plan to do after #73, it requires multi mapping from one code to many).

atarijookie commented 7 years ago

What about that idea of yours, the magic hotkey? Can't we replace /bin/login...

Yeah, that could be done, too, but it seems like too much work, and might behave differently on Yocto and Raspbian, while no one (yet) has complained about this not-blocked USB keyboard. So I would just go with no-autologin and no-ctrl-alt-del for now, and later the rest...

I'm still having trouble with #100

mikrosk commented 7 years ago

@atarijookie: sounds reasonable although now I will be much more cautious when using USB keyboards. :D

atarijookie commented 7 years ago

Disabling ctrl-alt-del:

sudo rm /lib/systemd/system/ctrl-alt-del.target
sudo ln -s /dev/null /lib/systemd/system/ctrl-alt-del.target
sudo systemctl daemon-reload

Disable auto-login:

ln -fs /lib/systemd/system/getty@.service
 /etc/systemd/system/getty.target.wants/getty@tty1.service
miniupnp commented 7 years ago

I'm just thinking, is there a way for the ce_main_app to grab exclusive access of the usb keyboard ? The magic key would then be to release that exclusive access :)

atarijookie commented 7 years ago

I'm just thinking, is there a way for the ce_main_app to grab exclusive access of the usb keyboard ?

I'm not sure, in theory yes, but didn't see / try that... But good idea :+1:

miniupnp commented 7 years ago

http://stackoverflow.com/questions/1698423/how-can-you-take-ownership-of-a-hid-device/1698686#1698686

ioctl(fd, EVIOCGRAB, (void)1); // grab exclusive .. ioctl(fd, EVIOCGRAB, (void)0); // release

atarijookie commented 7 years ago

Wow, nice, thanks ;) If @mikrosk adds another magical keyboard shortcut, adding this one would be easy ;) Or even MiKRO could then add it while he's doing his IKBD enhancements...

tin-nl commented 7 years ago

Disabling C-A-D and grab all input via ce_main sounds good! There should probably be some text on the console above the login explaining the keystroke to unlock the keyboard.

atarijookie commented 7 years ago

I've added disabling Ctrl-Alt-Del and disabling auto-login into shellscripts/copynewscripts.sh

There should probably be some text on the console above the login explaining the keystroke to unlock the keyboard.

Agree, I would just add it into /etc/motd which currently shows 'Raspbian Jessie running on CosmosEx device.'

@mikrosk - Will you do this improvement while you're playing with magical key strokes, or should I do it?

mikrosk commented 7 years ago

EDIT: deleted, I'm an idiot. ;)

Sure, I can take a look. So just to verify we understand each other:

Correct?

atarijookie commented 7 years ago

Correct?

No :)

mikrosk commented 7 years ago

Ah, right. I've mistaken CE_CONF for ce_main_app, assuming that it can't run in background the whole time (I'm tired :-P).

It's a good concept, this could really work nicely.

EDIT: that being said, I fail to see why do we need these:

at all?

tin-nl commented 7 years ago

Ctrl+Alt+Del sequence for reboot will be disabled (done in the script, applied with next update)

I think that even in the above scenario the user could reboot the CE by accident while ce_main isn't running - we don't know yet if ce_main starts early enough to prevent that.

That accidental reboot probably happened to to users already (but probably unbeknown to them).

mikrosk commented 7 years ago

I think that even in the above scenario the user could reboot the CE by accident while ce_main isn't running - we don't know yet if ce_main starts early enough to prevent that.

So the assumption is that the systemd flag kicks in earlier?

And why do we need to disable autologin?

atarijookie commented 7 years ago

I think that even in the above scenario the user could reboot the CE by accident while ce_main isn't running - we don't know yet if ce_main starts early enough to prevent that.

No no, by executing this (in the script):

ln -fs /dev/null /lib/systemd/system/ctrl-alt-del.target

It will disable ctrl-alt-del reboot forever (until changed back), so after that even when linux is booting and ce_main_app is not running, it will not reboot RPi.

And why do we need to disable autologin?

In case you still would be typing in console, while your keyboard grabbing feature is still not available ;)

mikrosk commented 7 years ago

while your keyboard grabbing feature is still not available ;)

Why it wouldn't be available?

miniupnp commented 7 years ago

Le 10.01.2017 à 10:31, Miro Kropacek a écrit :

while your keyboard grabbing feature is still not available ;)

Why it wouldn't be available?

until the ce_main_app is launched I guess...

atarijookie commented 7 years ago

while your keyboard grabbing feature is still not available ;) Why it wouldn't be available?

Because you haven't done it, commited it and pushed it to master branch? :)

until the ce_main_app is launched I guess...

No, it's a linux system setting - once you set it, it's there, independent of ce_main_app running or not.

tin-nl commented 7 years ago

It will disable ctrl-alt-del reboot forever (until changed back), so after that even when linux is booting and ce_main_app is not running, it will not reboot RPi.

@atarijookie: I know, I was just trying to paint a picture that might happen if we don't do this :) Edit: In that comment I was replying to Mikro, but did that poorly, sorry.

miniupnp commented 7 years ago

the ce_main_app will be grabbing the keyboard. But until it is runing, the USB keyboard events still goes to the system console. That's why it is needed to disable CTRL-ALT-DEL and disable autologin : the user can be logged a few seconds before ce_main_app is launched. Except if you can make sure autologin happens AFTER ce_main_app is able to grab USB keyboard exclusivity

miniupnp commented 7 years ago

I can code USB Keyboard grabbing, but I have no HDMI screen to test ;)

mikrosk commented 7 years ago

@miniupnp I don't have HDMI either. :-P IMO that autologin off is not needed (yes jookie, after I commit it ;)). My memory is vague here but from what I remember login happens at a certain init level so we just have to make sure to run ce_main_app on a lower one (what is the case right now, I believe).

tin-nl commented 7 years ago

@mikrosk: on raspbian ce_main starts directly after mountall. @miniupnp: I can test that (via dvi adapter).

miniupnp commented 7 years ago

@tin-nl : could you test my branch https://github.com/miniupnp/ce-atari/tree/ikbd ? only grabbing is implemented :)

tin-nl commented 7 years ago

Cool, sure :) I'll test yocto & raspbian.

tin-nl commented 7 years ago

@miniupnp; works fine on Raspbian - takes keyboard as soon as ce_main runs (faaaar before login), no input possible, input is routed to ST nicely. I can finally reboot my STe via keyboard w/o reboot the CE by accident ;-)

Could you shoot me a yocto binary? I can't cross compile to yocto ATM as it seems...

miniupnp commented 7 years ago

@tin-nl http://nanard.free.fr/CosmosEx/cosmosex_yocto_20170110.tar.gz

tin-nl commented 7 years ago

@miniupnp: Thanks a lot - works on yocto, too :) Couldn't check IKBD forwarding aka if ce_main still gets events (I see no reason why it shouldn't).

I also can confirm that ce_main releases USB on SIGINT/KILL (ce_stop.sh). Dunno what happens if it's crashing, though.

miniupnp commented 7 years ago

@tin-nl well it would be a linux kernel bug if the process keep exclusive access after exit, whatever the reason of the exit (crash, SIGTERM/SIGKILL/etc)

miniupnp commented 7 years ago

@atarijookie / @mikrosk : should I merge that code right away, or wait for the "magic key" to be implemented for release ? I can implement it if you tell me which magic key combination you want

mikrosk commented 7 years ago

@miniupnp: go ahead, I wont be able to do it earlier than during the weekend and it's possible the last pending tasks for #73 will eat my all time.

atarijookie commented 7 years ago

I don't have a preference on Magic Key Combination, so maybe MiKRO should tell ;)

miniupnp commented 7 years ago

I've implemented it with LCTRL+LALT+RCTRL+RALT if you're OK, I merge

atarijookie commented 7 years ago

I'm fine with it, and how about @mikrosk ?

mikrosk commented 7 years ago

@mikrosk is fine with that as well. So what's left to be implemented, if anything?