Recent

Author Topic: Starting chromium-browser from within a lazarus program  (Read 8314 times)

BLL

  • Sr. Member
  • ****
  • Posts: 276
Starting chromium-browser from within a lazarus program
« on: February 20, 2018, 12:46:50 pm »
Hi,
Can anyone help me? My lazarus program needs to be run with gksudo as some commands such as setting system dat/time can't be done without.
I want to be able to start the chromium-browser, telling to start maximized and passing it a url.

It won't start unless I use the -no-sandbox switch, which then gives me a stability warning in chrome!

If I use:
Code: Pascal  [Select][+][-]
  1. runcommand('chromium-browser', ['--no-sandbox', 'https://www.bbc.com/weather/6355177], s);

it works, but I want chrome maximized, but if I try:
Code: Pascal  [Select][+][-]
  1. runcommand('chromium-browser', ['--no-sandbox --start-maximized', 'https://www.bbc.com/weather/6355177], s);
then it ignores the sandbox switch and warns me to restart as a normal user

Is there some way around this please?

Thanks

Brian

RasPi 3, jessie, foc 3.0.0, lazarus 1.7
« Last Edit: February 20, 2018, 01:50:03 pm by BLL »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #1 on: February 20, 2018, 01:16:37 pm »
So you are starting chromium as root (you ran your application with gksudo) and you are starting it without the sandbox. Isn't that really, really, really dangerous?

First of all, maybe you should try some other options as running chromium with the sandbox option (for instance with --user-data-dir=/home/someuser or --temp-profile or something).

Even better would be to run it as a user (su - username -c "chromium --user-data-dir=/foo/bar").

And even beter would be to not run your program as root and find another solution to changing the system date/time (for instance initiate a different process of your application as root, to change the date/time).

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #2 on: February 20, 2018, 10:28:54 pm »
Hi rvk,
Thanks for the reply although I don't profess to understand it! Thus far, I have only ever started the browser from the menu! I don't know what a sandbox is!
I will have to look carefully at chrome's switches and try to understand what is going on.
I spent ages trying to run my program without gksudo, but the result was linux 1, me nil!!
Why is everything about linux so damn complicated? What ever happened to KISS?

In frustration.

Brian

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #3 on: February 20, 2018, 10:43:32 pm »
Thanks for the reply although I don't profess to understand it!
Well, in that case it's defenitly not a good idea to use --no-sandbox.
You should only use the --no-sandbox if you know what you're doing.
It severely decreases the security of chromium and I can't imagine that's what you want.

The safest option all round would be to just run your program in user-space. Why do you need to change/set the date/time? If you need to do that, maybe you can separate that part of your program. But running with gksudo or sudo is just asking for trouble if you don't know the implications.

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #4 on: February 20, 2018, 10:52:52 pm »
Hi
I don't profess to be an expert - I am slowly learning. The reason I need to set the system date/time is that my program reads a gps and uses that to set the RasPi's date/time as the RasPi has no rtc and my RasPi is installed in my caravan (RV) and there may or may not be an internet connection available to set the system date/time.
If there is, all I want to do is to be able to start chrome and display the BBC weather page for my location in the simplest way possible. My program runs just fine with gksudo and as I said, I have not been able to get it to do so without, despite countless hours of trying.

Brian

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #5 on: February 20, 2018, 11:09:49 pm »
I have tried one of your suggestions by trying at the command prompt:
root@CaravanPi:/home/pi# su - pi -c "chromium-browser --usr-data-dir=/home/pi"
I get a raft of errors:
bootstrap_helper: /usr/lib/chromium-browser/nacl_helper: Cannot open ELF file!  errno=2
[1:1:0220/230841.590165:ERROR:nacl_fork_delegate_linux.cc(315)] Bad NaCl helper startup ack (0 bytes)
[24634:24634:0220/230841.624092:ERROR:browser_main_loop.cc(495)] Failed to put Xlib into threaded mode.
[24634:24634:0220/230841.641387:ERROR:browser_main_loop.cc(272)] Gtk: cannot open display:

I have no idea what it all means!

Brian

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #6 on: February 20, 2018, 11:14:33 pm »
The reason I need to set the system date/time is that my program reads a gps and uses that to set the RasPi's date/time as the RasPi has no rtc and my RasPi is installed in my caravan (RV) and there may or may not be an internet connection available to set the system date/time.
Ah, ok. I wouldn't do that with a Lazarus program. You could write a separate FPC (or Lazarus) program which does that but there are much simpler solution for that.
For instance: http://blog.petrilopia.net/linux/raspberry-pi-set-time-gps-dongle/
But you can also write a very small FPC program (clean without GUI) and run that every few hours. It could be a very small program of just a dozen lines or so.

Quote
If there is, all I want to do is to be able to start chrome and display the BBC weather page for my location in the simplest way possible.
If you separated the date/time setting from your program, you can run your program as normal user. Did you try it then, starting chromium? Does that work?

Quote
My program runs just fine with gksudo and as I said, I have not been able to get it to do so without, despite countless hours of trying.
I don't know why chromium won't start as root (haven't got a pi handy). But either way, running chromium as root is not wise (the stability warning should say as much).

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #7 on: February 21, 2018, 10:30:33 am »
Hi rvk,
I also use the gps to extract lat/long info as well as setting time and date so separating it out is not so convenient.
Brian

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #8 on: February 21, 2018, 10:33:11 am »
I also use the gps to extract lat/long info as well as setting time and date so separating it out is not so convenient.
No, I mean you can create a small project which only does that, extract lat/long and date/time and set it. All without GUI. You could save the lat/long to a tmp file which you can read in another program. But the utility without GUI can be small and run as root.

Never run chromium in that same program. That should be separated to user-space (run as user only).

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #9 on: February 21, 2018, 12:10:17 pm »
Hi rvk
Solved it! I am running chrome as user pi with (one of your suggestions):

Code: Pascal  [Select][+][-]
  1. info := 'sudo -u pi chromium-browser --start-maximized https://www.bbc.com/weather/6355177';
  2.  runcommand('/bin/sh', ['-c', info], info);

Works happily with no major surgery on my program!

Next question - what is the easiest way to determine from my lazarus program whether the RasPi currently has an internet connection, so I can grey out the button which calls the runcommand if there's no internet connection available?

Thanks again

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #10 on: February 21, 2018, 02:28:50 pm »
Next question - what is the easiest way to determine from my lazarus program whether the RasPi currently has an internet connection, so I can grey out the button which calls the runcommand if there's no internet connection available?
You could try to retrieve a webpage.
You could use http://checkip.amazonaws.com/. In that case you also get your IP directly back.

Something like this:
Code: Pascal  [Select][+][-]
  1. uses fpHttpClient;
  2.  
  3. function IsInternetConnectionActive: Boolean;
  4. begin
  5.   with TfpHttpClient.Create(nil) do
  6.   try
  7.     try
  8.       Get('http://checkip.amazonaws.com/');
  9.       Result := true;
  10.     except
  11.       Result := false;
  12.     end;
  13.   finally
  14.     Free;
  15.   end;
  16. end;
  17.  
  18. procedure TForm1.Button1Click(Sender: TObject);
  19. begin
  20.   if IsInternetConnectionActive then
  21.     Self.Caption := 'Internet active'
  22.   else
  23.     Self.Caption := 'Internet DOWN';
  24. end;
(Note, if you run this in the IDE with no internet connection, you also get an exception-dialog which you can "Continue")

This only checks if checkip.amazonaws.com is available. If that service is down it will give you false while the rest of the internet could still be available, but at least it's a start.

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Starting chromium-browser from within a lazarus program
« Reply #11 on: February 21, 2018, 02:46:51 pm »
This only checks if checkip.amazonaws.com is available. If that service is down it will give you false while the rest of the internet could still be available, but at least it's a start.

Since he wants www.bbc.com, he could simply use that :D

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #12 on: February 21, 2018, 03:38:32 pm »
Hi
Thanks for the code. Unfortunately, I think that fpHttpClient is in the webLaz package which will NOT install into lazarus on my Pi and requests for help elicited no responses. I had wanted to include a simple webserver into the program but ended writing a simple one in C and calling that because webLaz won't install.

Brian

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Starting chromium-browser from within a lazarus program
« Reply #13 on: February 21, 2018, 03:57:24 pm »
Unfortunately, I think that fpHttpClient is in the webLaz package which will NOT install into lazarus on my Pi and requests for help elicited no responses.
fphttpclient is in the fcl-web package. http://wiki.lazarus.freepascal.org/fphttpclient
And as far as I know that's available on RPI.

I don't even think you need to include anything. Just add fpHttpClient to your uses and use my code snippet. Does that not work or give an error?

Edit: I just tried this in a Rasbian VM (Stretch) and it worked without adding anything.
« Last Edit: February 21, 2018, 04:29:10 pm by rvk »

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starting chromium-browser from within a lazarus program
« Reply #14 on: February 22, 2018, 10:33:04 am »
Hi, adding uses fclHttpClient and then compiling gives an error from sslsockets, saying that it can't find openssl. There is no sign of a package fclweb in the components directory.

Brian

 

TinyPortal © 2005-2018