Linux daemon with elements?

I’m just curious if it is possible to create a Linux daemon with Elements?

I’ve never attempted any Linux development but it’s something that I’m considering for the future.

very much so, yes. we’re lookin at providing a sample or template soon (@jonaa), but in essence, a unix dæmon really is just a command line app, so that template should be a good starting point…

Cursory googling finds a few links such as http://www.unixguide.net/unix/programming/1.7.shtml, http://www.enderunix.org/docs/eng/daemon.php and that should pretty direct translate to any of the Elements languages, when using Island/Linux As promised, I’ll have someone look at turning this into a template, sample and/or tutorial next week, but maybe these help to get you started now.

(Pro tip: Paste & Convert Objective-C also handles most regular. C code, as well.

This is a rough (untested) port from http://www.enderunix.org/docs/eng/daemon.php. All compiles except that mask doesn’t;t seem to be defined; I’ll have someone check into why.

namespace DæmonApplication;

uses
  rtl;

type
  Program = class
  public

    class method Main(args: array of String): Int32;
    begin
      // add your own code here
      writeLn('The magic happens here.');
      
      var f := fork();
      if f <0  then exit(1); /* fork error */
      if f > 0 then exit(0); /* parent exits */
      /* child (daemon) continues */
      
      setsid(); /* obtain a new process group */
      
      for d: Integer := getdtablesize()-1 downto 0 do
        close(d); /* close all descriptors */

      var i := open("/dev/null", O_RDWR); /* open stdin */
      dup(i); /* stdout */
      dup(i); /* stderr */
      
      mask($17); /* $17 = octal 27 | This will restrict file creation mode to 750 (complement of 027). */
      chdir("/servers/");
      
      var lfp := open('exampled.lock', O_RDWR or O_CREAT, 416);
      if lfp < 0 then exit(1);
      if lockf(lfp, F_TLOCK, 0) < 0 then exit(0);
      //sprintf(str, '%d'#10, getpid());
      //&write(lfp, str, strlen(str));
      
      //signal(SIG_IGN,SIGCHLD); /* child terminate signal */

      method Signal_Handler(sig: Integer);
      begin
        case sig of
          SIGHUP: ; /* rehash the server */
          SIGTERM: rtl.exit(0); /* finalize the server */
        end;
      end;

      signal(SIGHUP, @Signal_Handler); /* hangup signal */
      signal(SIGTERM, @Signal_Handler); /* software termination signal from kill */

      method log_message(filename: ^AnsiChar; message: ^AnsiChar);
      begin
        var logfile: ^FILE;
        logfile := fopen(filename, 'a');
        if assigned(logfile) then begin
          fprintf(logfile, '%s'#10, message);
          fclose(logfile);
        end;
      end;

      log_message("conn.log", "connection accepted");
      log_message("error.log", "can not open file");

      openlog("mydaemon",LOG_PID,LOG_DAEMON);
      //syslog(LOG_INFO, "Connection from host %d", callinghostname);
      syslog(LOG_ALERT, "Database Error !");
      closelog();


    end;

  end;

end.

Thanks, logged as bugs://83084 (for mask)

Thank you very much!

1 Like

any time!

let me know what works and what (if anything) doesn’t…