Recent

Author Topic: Converting launch_activate_socket() from C to Pascal - issues  (Read 1601 times)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1589
  • Former Delphi 1-7, 10.2 user
After having successfully converted  the SampleD C daemon code into working, non-deprecated C code (ie replacing the deprecated ASL logging with OSLog; replacing the myriad deprecated launch_ functions with launch_activate_socket) my quest to convert it into Pascal has hit a pot hole, er, wall.

My working, converted, C code:

Code: C  [Select][+][-]
  1. #include <launch.h>
  2. [...]
  3.  int *listening_fd_array;
  4.  size_t listening_fds_count;
  5.  int err;
  6. [...]
  7. err = launch_activate_socket("MySocket", &listening_fd_array, &listening_fds_count);
  8. if (err != 0) {
  9.    os_log_error(OS_LOG_DEFAULT, "launch_activate_socket() failed error %d (%s)", err, strerror(err));
  10. }
  11.  
  12. if (NULL == listening_fd_array) {
  13.     os_log_error(OS_LOG_DEFAULT, "No known sockets found to answer requests on!");
  14.     retval = EXIT_FAILURE;
  15.     goto done;
  16. }

My failed Pascal attempt:

Code: Pascal  [Select][+][-]
  1. function launch_activate_socket(const name: string; var fds: PInteger; cnt: size_t): integer; external name '_launch_activate_socket';
  2.  
  3. [...]
  4. var
  5.   err : integer;
  6.   listening_fd_array: PInteger;
  7.   listening_fds_count: size_t;
  8.  
  9. [...]
  10. err := launch_activate_socket('MySocket', listening_fd_array, listening_fds_count);
  11. if(err <> 0) then
  12.   begin
  13.     if(err = ENOENT) then
  14.       writeln('Error: Socket not found in plist file (', err, ')')
  15.     else if(err = ESRCH) then
  16.       writeln('Error: Calling process is not managed by launchd (', err, ')')
  17.     else if(err = EALREADY) then
  18.       writeln('Error: Socket already activated (', err, ')')
  19.     else
  20.       writeln('Error: ', err);
  21.   end;
  22.  
  23. if(Nil = listening_fds_count) then
  24.   begin
  25.     writeln('No known sockets found to answer requests on!');
  26.     goto Done;
  27.   end;

Where did I go wrong? It compiles successfully, but errors out with ESRCH and "No known sockets...".
Lazarus 2.3 2021-08-10 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.3 r65500 FPC 3.3.1 r49634 macOS 11.6 aarch64 Xcode 13
Lazarus 2.3 2021-08-11 FPC 3.2.2 FreeBSD 13.0 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

PascalDragon

  • Hero Member
  • *****
  • Posts: 3338
  • Compiler Developer
Re: Converting launch_activate_socket() from C to Pascal - issues
« Reply #1 on: May 04, 2021, 01:10:37 pm »
I have really strong doubts that launch_activate_socket as a C function takes a Pascal string instead of a PChar. Also shouldn't your cnt parameter be a var (or out) as well?
(Also I doubt that Nil = listening_fds_count does indeed compile)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1589
  • Former Delphi 1-7, 10.2 user
Re: Converting launch_activate_socket() from C to Pascal - issues
« Reply #2 on: May 04, 2021, 02:45:51 pm »
Dang (cut 'n paste errors from another version):
- Nil should have been 0
- string should have been pchar

Thanks - I'd missed the missing var. No more spurious errors :)
Lazarus 2.3 2021-08-10 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.3 r65500 FPC 3.3.1 r49634 macOS 11.6 aarch64 Xcode 13
Lazarus 2.3 2021-08-11 FPC 3.2.2 FreeBSD 13.0 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

PascalDragon

  • Hero Member
  • *****
  • Posts: 3338
  • Compiler Developer
Re: Converting launch_activate_socket() from C to Pascal - issues
« Reply #3 on: May 05, 2021, 09:07:36 am »
Thanks - I'd missed the missing var. No more spurious errors :)

You're welcome. :)

 

TinyPortal © 2005-2018