Trapping user shutdown/logout/restart in Cocoa/Foundation tool

  • Hi guys,

    I want to trap user user shutdown/logout/restart. For that I am using
    -(void)workspaceWillPowerOff:(NSNotification *)aNotification. But for this,
    it is necessary to have dock icon of an application. I searched the google &
    came to know that it is required to have dock icon to trap
    shutdown/restart/shutdown. My code is c++ code, but for  trapping
    shudown/restart/logout only, I am wrapping that C++ code in cocoa. I have
    two questions:
    1. Is there any other way to trap shutdown/restart/logout without requiring
    to have type of project as Cocoa (i.e. either through Foundation tool or
    like that).
    2. Is there any way to trap shutdown/restart/logout in cocoa application
    without requiring to have dock icon of an application?

    Please reply.

    Thanks,
    Palav

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • Le 4 janv. 08 à 15:38, parag vibhute a écrit :

    > Hi guys,
    >
    > I want to trap user user shutdown/logout/restart. For that I am using
    > -(void)workspaceWillPowerOff:(NSNotification *)aNotification. But
    > for this,
    > it is necessary to have dock icon of an application. I searched the
    > google &
    > came to know that it is required to have dock icon to trap
    > shutdown/restart/shutdown. My code is c++ code, but for  trapping
    > shudown/restart/logout only, I am wrapping that C++ code in cocoa. I
    > have
    > two questions:
    > 1. Is there any other way to trap shutdown/restart/logout without
    > requiring
    > to have type of project as Cocoa (i.e. either through Foundation
    > tool or
    > like that).
    > 2. Is there any way to trap shutdown/restart/logout in cocoa
    > application
    > without requiring to have dock icon of an application?
    >
    > Please reply.
    >
    > Thanks,
    > Palav

    1. Look like you want to use IORegisterForSystemPower();
    This is plain C so it works in command line tools and in Cocoa
    Application.
    There is also a Q&A that explains how to use it http://developer.apple.com/qa/qa2004/qa1340.html

    2. it probably works if you create a standard Cocoa Application and
    add the LSUIElement in the Info.plist.
    See http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/
    Articles/PListKeys.html

      for details about this key.

    Regards
    Jean-Daniel
  • Hi,

    Thanks for reply. I read QA1340 which you mentioned & after going throught
    it got following API:
    *
    IORegisterForSystemPower
    Connects the caller to the Root Power Domain IOService for the purpose of
    receiving Sleep, Wake, ShutDown, PowerUp notifications for the System.*

    Even though I have not implemented the code, I think above API will work for
    Shutdown & Restart. Am I correct?

    Also then what should I use to trap logout event as did not find any info
    regarding it in API reference?

    & yes I am using same thing i.e. LSUIElement to hide dock icon but as I told
    if I hide the dock icon then couldn't able to trap restart/shutdown/logout
    using *workspaceWillPowerOff *cocoa notification. So finding other
    alternative. As you have suggested to use QA1340, I will use
    *IORegisterForSystemPower
    *for trapping shutdown/restart.

    Thanks again,
    Palav

    On Jan 4, 2008 8:36 PM, Jean-Daniel Dupas <devlists...> wrote:

    >
    > Le 4 janv. 08 à 15:38, parag vibhute a écrit :
    >
    >> Hi guys,
    >>
    >> I want to trap user user shutdown/logout/restart. For that I am using
    >> -(void)workspaceWillPowerOff:(NSNotification *)aNotification. But
    >> for this,
    >> it is necessary to have dock icon of an application. I searched the
    >> google &
    >> came to know that it is required to have dock icon to trap
    >> shutdown/restart/shutdown. My code is c++ code, but for  trapping
    >> shudown/restart/logout only, I am wrapping that C++ code in cocoa. I
    >> have
    >> two questions:
    >> 1. Is there any other way to trap shutdown/restart/logout without
    >> requiring
    >> to have type of project as Cocoa (i.e. either through Foundation
    >> tool or
    >> like that).
    >> 2. Is there any way to trap shutdown/restart/logout in cocoa
    >> application
    >> without requiring to have dock icon of an application?
    >>
    >> Please reply.
    >>
    >> Thanks,
    >> Palav
    >
    > 1. Look like you want to use IORegisterForSystemPower();
    > This is plain C so it works in command line tools and in Cocoa
    > Application.
    > There is also a Q&A that explains how to use it
    > http://developer.apple.com/qa/qa2004/qa1340.html
    >
    > 2. it probably works if you create a standard Cocoa Application and
    > add the LSUIElement in the Info.plist.
    > See
    > http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/
    Articles/PListKeys.html

    > for details about this key.
    >
    > Regards
    > Jean-Daniel

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • Le 4 janv. 08 à 16:46, parag vibhute a écrit :

    > Hi,
    >
    > Thanks for reply. I read QA1340 which you mentioned & after going
    > throught it got following API:
    >
    > IORegisterForSystemPower
    > Connects the caller to the Root Power Domain IOService for the
    > purpose of receiving Sleep, Wake, ShutDown, PowerUp notifications
    > for the System.
    >
    > Even though I have not implemented the code, I think above API will
    > work for Shutdown & Restart. Am I correct?

    I did not try but addind a kIOMessageSystemWillRestart case and a
    kIOMessageSystemWillPowerOff case in the switch statement should be
    enough.

    > Also then what should I use to trap logout event as did not find any
    > info regarding it in API reference?

    Getting login/logout event from a command line tool is a bit tricky.
    You have to use the System Configuration frameworks. But there is
    another Q&A for this task ;-)

    http://developer.apple.com/qa/qa2001/qa1133.html
  • Hi,

    While going through code snippets given in QA, I observed that apple used *
    daemon* API to run process in background. In my cocoa application, I just
    added daemon(0,0) line in my main function & using LSUIElement, hide the
    application icon. Now even though dock icon is hidden, my application is
    able to trap logout/restart/shutdown in
    -(void)workspaceWillPowerOff:(NSNotification
    *)aNotification.  Want to know whether this way is correct? or is it having
    any drawback?

    Thanks for reply,
    Palav

    On Jan 4, 2008 9:50 PM, Jean-Daniel Dupas <devlists...> wrote:

    > Le 4 janv. 08 à 16:46, parag vibhute a écrit :
    >
    > Hi,
    >
    > Thanks for reply. I read QA1340 which you mentioned & after going throught
    > it got following API:
    > *
    > IORegisterForSystemPower
    > Connects the caller to the Root Power Domain IOService for the purpose of
    > receiving Sleep, Wake, ShutDown, PowerUp notifications for the System. *
    >
    > Even though I have not implemented the code, I think above API will work
    > for Shutdown & Restart. Am I correct?
    >
    >
    > I did not try but addind a *kIOMessageSystemWillRestart case and a **
    > kIOMessageSystemWillPowerOff case in the switch statement should be
    > enough.*
    >
    >
    > Also then what should I use to trap logout event as did not find any info
    > regarding it in API reference?
    >
    >
    > Getting login/logout event from a command line tool is a bit tricky. You
    > have to use the System Configuration frameworks. But there is another Q&A
    > for this task ;-)
    >
    > http://developer.apple.com/qa/qa2001/qa1133.html
    >
    >
    >
    >

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • Is anybody there?

    Please reply.

    Thanks,
    Palav

    On 1/5/08, parag vibhute <parag.vibhute...> wrote:
    > Hi,
    >
    > While going through code snippets given in QA, I observed that apple used *
    > daemon* API to run process in background. In my cocoa application, I just
    > added daemon(0,0) line in my main function & using LSUIElement, hide the
    > application icon. Now even though dock icon is hidden, my application is
    > able to trap logout/restart/shutdown in
    > -(void)workspaceWillPowerOff:(NSNotification
    > *)aNotification.  Want to know whether this way is correct? or is it having
    > any drawback?
    >
    > Thanks for reply,
    > Palav
    >
    >
    > On Jan 4, 2008 9:50 PM, Jean-Daniel Dupas <devlists...> wrote:
    >
    >> Le 4 janv. 08 à 16:46, parag vibhute a écrit :
    >>
    >> Hi,
    >>
    >> Thanks for reply. I read QA1340 which you mentioned & after going
    > throught
    >> it got following API:
    >> *
    >> IORegisterForSystemPower
    >> Connects the caller to the Root Power Domain IOService for the purpose of
    >> receiving Sleep, Wake, ShutDown, PowerUp notifications for the System. *
    >>
    >> Even though I have not implemented the code, I think above API will work
    >> for Shutdown & Restart. Am I correct?
    >>
    >>
    >> I did not try but addind a *kIOMessageSystemWillRestart case and a **
    >> kIOMessageSystemWillPowerOff case in the switch statement should be
    >> enough.*
    >>
    >>
    >> Also then what should I use to trap logout event as did not find any info
    >> regarding it in API reference?
    >>
    >>
    >> Getting login/logout event from a command line tool is a bit tricky. You
    >> have to use the System Configuration frameworks. But there is another Q&A
    >> for this task ;-)
    >>
    >> http://developer.apple.com/qa/qa2001/qa1133.html
    >>
    >>
    >>
    >>
    >
    >
    > --
    >
    > There are many things in your life that will catch your eye but only a few
    > will catch your heart....pursue those'.
    >

    --

    There are many things in your life that will catch your eye but only a
    few will catch your heart....pursue those'.
  • On 08 Jan 08, at 00:11, parag vibhute wrote:
    > Is anybody there?
    >
    > Please reply.

    No, there is nobody here. Our activity on this mailing list is a
    massive hoax.

    Seriously, though:

    On 1/5/08, parag vibhute <parag.vibhute...> wrote:
    > While going through code snippets given in QA, I observed that apple
    > used *
    > daemon* API to run process in background. In my cocoa application, I
    > just
    > added daemon(0,0) line in my main function & using LSUIElement, hide
    > the
    > application icon. Now even though dock icon is hidden, my
    > application is
    > able to trap logout/restart/shutdown in
    > -(void)workspaceWillPowerOff:(NSNotification
    > *)aNotification.  Want to know whether this way is correct? or is it
    > having
    > any drawback?

    It has some significant drawbacks. Read Tech Note 2083 [1]. In
    particular, daemon() is deprecated under Mac OS 10.5, and a number of
    frameworks (including Core Foundation!) are unsafe to call from a
    daemon (forked-without-exec) context.

    [1]: http://developer.apple.com/technotes/tn2005/tn2083.html
  • Hi andrew,

    Thanks for reply. I have a requirement to run the application, not to show
    application icon in dock & to trap shutdown, restart, logout. As Jean-Daniel
    said, there are other ways to capture the logout,restart,shutdown. But it
    seems that those ways are particularly created for daemon otherwise in cocoa
    application, it is rarely that anybody will use those techniques for
    capturing logout/restart/shutdown; programmer will use workspaceWillPowerOff
    notification. Those techniques are particularly useful to differentiate
    between logout, restart & shutdown notification. I went through technote
    which you suggested, but for creating daemon, I will need to implement plist
    & I can't do that. I can only make changes in code of my application. I
    can't create plist & stuff like that. I read that in order to control the
    daemon by launchd, do not use "daemon" API. Also checked that if you launch
    the application in C program using system api & concating "&" at the end of
    path (like system "./testapp &"), application runs in background & its
    parent process become launchd. But it doesn't affect. Yes you are right that
    technote mention that do not link daemon to non-daemonized framework. But I
    think that statement is only concerned if you want to relate daemon to inetd
    or launchd etc. Using "daemon" API only in code does not make application
    daemon.

    Any comments.

    Thanks,
    Palav

    On Jan 8, 2008 2:55 PM, Andrew Farmer <andfarm...> wrote:

    > On 08 Jan 08, at 00:11, parag vibhute wrote:
    >> Is anybody there?
    >>
    >> Please reply.
    >
    > No, there is nobody here. Our activity on this mailing list is a
    > massive hoax.
    >
    > Seriously, though:
    >
    > On 1/5/08, parag vibhute <parag.vibhute...> wrote:
    >> While going through code snippets given in QA, I observed that apple
    >> used *
    >> daemon* API to run process in background. In my cocoa application, I
    >> just
    >> added daemon(0,0) line in my main function & using LSUIElement, hide
    >> the
    >> application icon. Now even though dock icon is hidden, my
    >> application is
    >> able to trap logout/restart/shutdown in
    >> -(void)workspaceWillPowerOff:(NSNotification
    >> *)aNotification.  Want to know whether this way is correct? or is it
    >> having
    >> any drawback?
    >
    > It has some significant drawbacks. Read Tech Note 2083 [1]. In
    > particular, daemon() is deprecated under Mac OS 10.5, and a number of
    > frameworks (including Core Foundation!) are unsafe to call from a
    > daemon (forked-without-exec) context.
    >
    > [1]: http://developer.apple.com/technotes/tn2005/tn2083.html
    >

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • On 08 Jan 08, at 07:49, parag vibhute wrote:
    > Thanks for reply. I have a requirement to run the application, not
    > to show
    > application icon in dock & to trap shutdown, restart, logout. As
    > Jean-Daniel
    > said, there are other ways to capture the logout,restart,shutdown.
    > But it
    > seems that those ways are particularly created for daemon otherwise
    > in cocoa
    > application, it is rarely that anybody will use those techniques for
    > capturing logout/restart/shutdown; programmer will use
    > workspaceWillPowerOff
    > notification. Those techniques are particularly useful to
    > differentiate
    > between logout, restart & shutdown notification. I went through
    > technote
    > which you suggested, but for creating daemon, I will need to
    > implement plist
    > & I can't do that. I can only make changes in code of my
    > application. I
    > can't create plist & stuff like that. I read that in order to
    > control the
    > daemon by launchd, do not use "daemon" API. Also checked that if you
    > launch
    > the application in C program using system api & concating "&" at the
    > end of
    > path (like system "./testapp &"), application runs in background & its
    > parent process become launchd. But it doesn't affect. Yes you are
    > right that
    > technote mention that do not link daemon to non-daemonized
    > framework. But I
    > think that statement is only concerned if you want to relate daemon
    > to inetd
    > or launchd etc. Using "daemon" API only in code does not make
    > application
    > daemon.

    TN2083 makes one thing quite clear: daemon() should not be used in new
    applications, and particularly not in any application which uses the
    Cocoa or CoreFoundation frameworks.
  • Le 9 janv. 08 à 06:35, Andrew Farmer a écrit :

    > On 08 Jan 08, at 07:49, parag vibhute wrote:
    >> Thanks for reply. I have a requirement to run the application, not
    >> to show
    >> application icon in dock & to trap shutdown, restart, logout. As
    >> Jean-Daniel
    >> said, there are other ways to capture the logout,restart,shutdown.
    >> But it
    >> seems that those ways are particularly created for daemon otherwise
    >> in cocoa
    >> application, it is rarely that anybody will use those techniques for
    >> capturing logout/restart/shutdown; programmer will use
    >> workspaceWillPowerOff
    >> notification. Those techniques are particularly useful to
    >> differentiate
    >> between logout, restart & shutdown notification. I went through
    >> technote
    >> which you suggested, but for creating daemon, I will need to
    >> implement plist
    >> & I can't do that. I can only make changes in code of my
    >> application. I
    >> can't create plist & stuff like that. I read that in order to
    >> control the
    >> daemon by launchd, do not use "daemon" API. Also checked that if
    >> you launch
    >> the application in C program using system api & concating "&" at
    >> the end of
    >> path (like system "./testapp &"), application runs in background &
    >> its
    >> parent process become launchd. But it doesn't affect. Yes you are
    >> right that
    >> technote mention that do not link daemon to non-daemonized
    >> framework. But I
    >> think that statement is only concerned if you want to relate daemon
    >> to inetd
    >> or launchd etc. Using "daemon" API only in code does not make
    >> application
    >> daemon.
    >
    > TN2083 makes one thing quite clear: daemon() should not be used in
    > new applications, and particularly not in any application which uses
    > the Cocoa or CoreFoundation frameworks.
    >

    And that's not an issus, as daemon() is absolutly NOT required to use
    SystemConfiguration and IOKit frameworks. All information found in the
    previous Q&A can be used in the middle of an Objc Class in a Cocoa
    application without problem.
  • Some observations:

    1. I was launching cocoa application from terminal, in that case its parent
    process was Terminal. So when I used to logout the machine, terminal process
    was used to kill & so my application was also used to killed. Bcoz of this
    application was not able to trap the logout(bcoz it is terminated).
    2. Now in order to make launchd as parent process of my application, I wrote
    a simple C program & then through it, I launched the application using
    system API & using "&" at the end (for e.g.
    system("./testapp/Contents/MacOS/testapp &");). Now when I checked in
    activity monitor, parent process of my application was "launchd". More
    interesting observation is now application was able to logout using
    "workspaceWillPowerOff" notification. Please note that I did not use
    daemon() API in my code while testing this.
    3. Other way to make launchd as parent process of my appliation  is  using
    daemon() API, but as you said I should never link daemon() API with
    carbon/cocoa framework. So I think this is not preferred.

    But I would like to know comments regarding point 2.

    Thanks,
    Palav

    On Jan 9, 2008 1:40 PM, Jean-Daniel Dupas <devlists...> wrote:

    >
    > Le 9 janv. 08 à 06:35, Andrew Farmer a écrit :
    >
    >> On 08 Jan 08, at 07:49, parag vibhute wrote:
    >>> Thanks for reply. I have a requirement to run the application, not
    >>> to show
    >>> application icon in dock & to trap shutdown, restart, logout. As
    >>> Jean-Daniel
    >>> said, there are other ways to capture the logout,restart,shutdown.
    >>> But it
    >>> seems that those ways are particularly created for daemon otherwise
    >>> in cocoa
    >>> application, it is rarely that anybody will use those techniques for
    >>> capturing logout/restart/shutdown; programmer will use
    >>> workspaceWillPowerOff
    >>> notification. Those techniques are particularly useful to
    >>> differentiate
    >>> between logout, restart & shutdown notification. I went through
    >>> technote
    >>> which you suggested, but for creating daemon, I will need to
    >>> implement plist
    >>> & I can't do that. I can only make changes in code of my
    >>> application. I
    >>> can't create plist & stuff like that. I read that in order to
    >>> control the
    >>> daemon by launchd, do not use "daemon" API. Also checked that if
    >>> you launch
    >>> the application in C program using system api & concating "&" at
    >>> the end of
    >>> path (like system "./testapp &"), application runs in background &
    >>> its
    >>> parent process become launchd. But it doesn't affect. Yes you are
    >>> right that
    >>> technote mention that do not link daemon to non-daemonized
    >>> framework. But I
    >>> think that statement is only concerned if you want to relate daemon
    >>> to inetd
    >>> or launchd etc. Using "daemon" API only in code does not make
    >>> application
    >>> daemon.
    >>
    >> TN2083 makes one thing quite clear: daemon() should not be used in
    >> new applications, and particularly not in any application which uses
    >> the Cocoa or CoreFoundation frameworks.
    >>
    >
    > And that's not an issus, as daemon() is absolutly NOT required to use
    > SystemConfiguration and IOKit frameworks. All information found in the
    > previous Q&A can be used in the middle of an Objc Class in a Cocoa
    > application without
    > problem.
    >

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • Hello Palav,

    Am 09.01.2008 um 15:51 schrieb parag vibhute:

    > 2. Now in order to make launchd as parent process of my
    > application, I wrote
    > a simple C program & then through it, I launched the application

    Why not just using the "open" command from within the terminal?

    <x-man-page://open>

    "The open command opens a file (or a directory or URL), just as if
    you had double-clicked the file's icon."

    Best,
    Dirk Stegemann
  • No I can't bcoz I need to pass arguments to my application.

    Thanks,
    palav

    On Jan 9, 2008 8:05 PM, Dirk Stegemann (Mailing-Lists) <
    <lists.dirk.stegemann...> wrote:

    > Hello Palav,
    >
    > Am 09.01.2008 um 15:51 schrieb parag vibhute:
    >
    >> 2. Now in order to make launchd as parent process of my
    >> application, I wrote
    >> a simple C program & then through it, I launched the application
    >
    > Why not just using the "open" command from within the terminal?
    >
    > <x-man-page://open>
    >
    > "The open command opens a file (or a directory or URL), just as if
    > you had double-clicked the file's icon."
    >
    >
    > Best,
    > Dirk Stegemann
    >
    >
    >

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today