Page 1 of 1

How to import Linux file containing sd_notify()

Posted: Thu Jan 30, 2025 4:20 pm
by swhite
Hi

I am not an experienced Linux user so I wondering how to import a library so that I can use sd_notify() to notify systemd of the status of my service. There seem to be files in multiple folders that might be candidates for the import but I am not sure which one should be used. So any guidance on the proper way to import the library containing sd_notify() would be appreciated.

Thanks,
Simon

Re: How to import Linux file containing sd_notify()

Posted: Thu Jan 30, 2025 5:24 pm
by StarBootics
I don't if the following code will help but ...

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Linux Notify
; File Name : Linux Notify.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : May 9th, 2020 
; Last Update : May 9th, 2020
; PureBasic code : V6.11 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Notify
  
  Enumeration
    
    #URGENCY_LOW
    #URGENCY_NORMAL
    #URGENCY_CRITICAL
    
    #ICON_ERROR
    #ICON_INFORMATION
    #ICON_QUESTION
    #ICON_WARNING
    
  EndEnumeration
  
  Interface Notify
    
    SetSummary(P_Summary.s)
    SetBody(P_Body.s)
    SetExpireTime(P_ExpireTime.l = 5000)
    SetUrgency(P_Urgency.i = -1)
    SetIcon(P_Icon.i =-1)
    Send()
    Free()
    
  EndInterface
  
  Declare.i New(P_Summary.s = "", P_Body.s = "", P_ExpireTime.l = 5000, P_Urgency.i = -1, P_Icon.i = -1)
  
EndDeclareModule

Module Notify
  
  Structure Private_Members
    
    VirtualTable.i
    Summary.s
    Body.s
    ExpireTime.l
    UrgencyString.s
    IconString.s
    
  EndStructure
  
  Procedure SetSummary(*This.Private_Members, P_Summary.s)
    
    *This\Summary = P_Summary
    
  EndProcedure
  
  Procedure SetBody(*This.Private_Members, P_Body.s)
    
    *This\Body = P_Body
    
  EndProcedure
  
  Procedure SetExpireTime(*This.Private_Members, P_ExpireTime.l = 5000)
    
    *This\ExpireTime = P_ExpireTime
    
  EndProcedure
  
  Procedure SetUrgency(*This.Private_Members, P_Urgency.i = -1)
    
    Select P_Urgency
        
      Case #URGENCY_LOW
        *This\UrgencyString = "--urgency=low"
        
      Case #URGENCY_NORMAL
        *This\UrgencyString = "--urgency=normal"
        
      Case #URGENCY_CRITICAL
        *This\UrgencyString = "--urgency=critical"
        
      Default 
        *This\UrgencyString = "--urgency=normal"
        
    EndSelect
    
  EndProcedure
  
  Procedure SetIcon(*This.Private_Members, P_Icon.i = -1)
    
    Select P_Icon
        
      Case #ICON_ERROR
        *This\IconString = "--icon=dialog-error"
        
      Case #ICON_INFORMATION
        *This\IconString = "--icon=dialog-information"
        
      Case #ICON_QUESTION
        *This\IconString = "--icon=dialog-question"
        
      Case #ICON_WARNING
        *This\IconString = "--icon=dialog-warning"
        
      Default
        *This\IconString = "--icon=dialog-information"
        
    EndSelect
    
  EndProcedure
  
  Procedure Send(*This.Private_Members)
    
    If Not RunProgram("notify-send", #DQUOTE$ + *This\Summary + #DQUOTE$ + " " + #DQUOTE$ + *This\Body + #DQUOTE$ + *This\IconString + " " + *This\UrgencyString + " --expire-time=" + Str(*This\ExpireTime), "")
      Debug "'notify-send' not available!"
      Debug "install: sudo apt-get install libnotify-bin"
    EndIf
    
  EndProcedure
  
  Procedure Free(*This.Private_Members)
    
    FreeStructure(*This)
    
  EndProcedure

  Procedure.i New(P_Summary.s = "", P_Body.s = "", P_ExpireTime.l = 5000, P_Urgency.i = -1, P_Icon.i = -1)
    
    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
    
    SetSummary(*This, P_Summary)
    SetBody(*This, P_Body)
    SetExpireTime(*This, P_ExpireTime)
    SetUrgency(*This, P_Urgency)
    SetIcon(*This, P_Icon)
    
    ProcedureReturn *This
  EndProcedure
  
  DataSection
    START_METHODS:
    Data.i @SetSummary()
    Data.i @SetBody()
    Data.i @SetExpireTime()
    Data.i @SetUrgency()
    Data.i @SetIcon()
    Data.i @Send()
    Data.i @Free()
    END_METHODS:
  EndDataSection
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  MyNotify.Notify::Notify = Notify::New("My Application 2", "A Desktop-Notification with 'notify-send'\nand a 2. line", 10000)
  MyNotify\Send()
  MyNotify\Free()
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Feel free to modify, to fit your standards.

Best regards
StarBootics

Re: How to import Linux file containing sd_notify()

Posted: Thu Jan 30, 2025 5:49 pm
by swhite
Thank-you but I am looking for the sd-notify() used with systemd services. I am not sure but I may have found the correct method:

Code: Select all

Import "/usr/lib64/libsystemd.so"
   sd_notify.i(environ.i,status.s)
EndImport

sd_notify(0,"READY=1")
 

Re: How to import Linux file containing sd_notify()

Posted: Thu Jan 30, 2025 5:51 pm
by Fred
you usually don't link directly with the .so, but using the -l flag (or the use of pkg-config):

Code: Select all

Import "-lsystemd"
   sd_notify.i(environ.i,status.s)
EndImport

sd_notify(0,"READY=1")

Re: How to import Linux file containing sd_notify()

Posted: Sat Feb 01, 2025 2:50 pm
by swhite
Thanks Fred for your help.