XLib Disconnection Callback












0















I use XLib and XRand to get some information about the connected displays on an embedded system.



class Foo {
private:
Display *_display{};

public:
Foo() {
_display = XOpenDisplay(":0.0");
}

void getSomeInfo() const {
/* Get some info with _display */
}

~Foo() {
XCloseDisplay(_display);
}
}


The problem is, X server can be shut down (for low power or some other purposes) after the creation of Foo instance and before calling getSomeInfo(), which causes immediate death of my application because XLib tries to exit application in case of errors.



Is there any mechanism, like a callback, which would allow me to understand that X Server went down and I should not use the _display pointer anymore ?










share|improve this question























  • Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

    – Uli Schlachter
    Nov 29 '18 at 16:24
















0















I use XLib and XRand to get some information about the connected displays on an embedded system.



class Foo {
private:
Display *_display{};

public:
Foo() {
_display = XOpenDisplay(":0.0");
}

void getSomeInfo() const {
/* Get some info with _display */
}

~Foo() {
XCloseDisplay(_display);
}
}


The problem is, X server can be shut down (for low power or some other purposes) after the creation of Foo instance and before calling getSomeInfo(), which causes immediate death of my application because XLib tries to exit application in case of errors.



Is there any mechanism, like a callback, which would allow me to understand that X Server went down and I should not use the _display pointer anymore ?










share|improve this question























  • Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

    – Uli Schlachter
    Nov 29 '18 at 16:24














0












0








0








I use XLib and XRand to get some information about the connected displays on an embedded system.



class Foo {
private:
Display *_display{};

public:
Foo() {
_display = XOpenDisplay(":0.0");
}

void getSomeInfo() const {
/* Get some info with _display */
}

~Foo() {
XCloseDisplay(_display);
}
}


The problem is, X server can be shut down (for low power or some other purposes) after the creation of Foo instance and before calling getSomeInfo(), which causes immediate death of my application because XLib tries to exit application in case of errors.



Is there any mechanism, like a callback, which would allow me to understand that X Server went down and I should not use the _display pointer anymore ?










share|improve this question














I use XLib and XRand to get some information about the connected displays on an embedded system.



class Foo {
private:
Display *_display{};

public:
Foo() {
_display = XOpenDisplay(":0.0");
}

void getSomeInfo() const {
/* Get some info with _display */
}

~Foo() {
XCloseDisplay(_display);
}
}


The problem is, X server can be shut down (for low power or some other purposes) after the creation of Foo instance and before calling getSomeInfo(), which causes immediate death of my application because XLib tries to exit application in case of errors.



Is there any mechanism, like a callback, which would allow me to understand that X Server went down and I should not use the _display pointer anymore ?







xlib xrandr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 16:02









Murat ŞekerMurat Şeker

1,1751921




1,1751921













  • Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

    – Uli Schlachter
    Nov 29 '18 at 16:24



















  • Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

    – Uli Schlachter
    Nov 29 '18 at 16:24

















Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

– Uli Schlachter
Nov 29 '18 at 16:24





Does "use XCB instead of Xlib" count as an answer? If so, I can provide that as an answer. XCB never calls exit for you and it actually provides some error status codes.

– Uli Schlachter
Nov 29 '18 at 16:24












1 Answer
1






active

oldest

votes


















1














I fear that your only option is to use XSetIOErrorHandler and then do something ugly.



From https://tronche.com/gui/x/xlib/event-handling/protocol-errors/XSetIOErrorHandler.html:




The XSetIOErrorHandler() sets the fatal I/O error handler. Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.




The "do something ugly" that I would suggest is to use setjmp and longjmp: Whenever you call any Xlib functions, you setjmp before. Your I/O error handling function then longjmps away to get away from the I/O error without your process exiting.






share|improve this answer
























  • I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

    – Murat Şeker
    Dec 3 '18 at 10:02











  • Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

    – Uli Schlachter
    Dec 3 '18 at 11:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396936%2fxlib-disconnection-callback%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I fear that your only option is to use XSetIOErrorHandler and then do something ugly.



From https://tronche.com/gui/x/xlib/event-handling/protocol-errors/XSetIOErrorHandler.html:




The XSetIOErrorHandler() sets the fatal I/O error handler. Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.




The "do something ugly" that I would suggest is to use setjmp and longjmp: Whenever you call any Xlib functions, you setjmp before. Your I/O error handling function then longjmps away to get away from the I/O error without your process exiting.






share|improve this answer
























  • I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

    – Murat Şeker
    Dec 3 '18 at 10:02











  • Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

    – Uli Schlachter
    Dec 3 '18 at 11:04
















1














I fear that your only option is to use XSetIOErrorHandler and then do something ugly.



From https://tronche.com/gui/x/xlib/event-handling/protocol-errors/XSetIOErrorHandler.html:




The XSetIOErrorHandler() sets the fatal I/O error handler. Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.




The "do something ugly" that I would suggest is to use setjmp and longjmp: Whenever you call any Xlib functions, you setjmp before. Your I/O error handling function then longjmps away to get away from the I/O error without your process exiting.






share|improve this answer
























  • I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

    – Murat Şeker
    Dec 3 '18 at 10:02











  • Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

    – Uli Schlachter
    Dec 3 '18 at 11:04














1












1








1







I fear that your only option is to use XSetIOErrorHandler and then do something ugly.



From https://tronche.com/gui/x/xlib/event-handling/protocol-errors/XSetIOErrorHandler.html:




The XSetIOErrorHandler() sets the fatal I/O error handler. Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.




The "do something ugly" that I would suggest is to use setjmp and longjmp: Whenever you call any Xlib functions, you setjmp before. Your I/O error handling function then longjmps away to get away from the I/O error without your process exiting.






share|improve this answer













I fear that your only option is to use XSetIOErrorHandler and then do something ugly.



From https://tronche.com/gui/x/xlib/event-handling/protocol-errors/XSetIOErrorHandler.html:




The XSetIOErrorHandler() sets the fatal I/O error handler. Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.




The "do something ugly" that I would suggest is to use setjmp and longjmp: Whenever you call any Xlib functions, you setjmp before. Your I/O error handling function then longjmps away to get away from the I/O error without your process exiting.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '18 at 16:24









Uli SchlachterUli Schlachter

5,0131729




5,0131729













  • I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

    – Murat Şeker
    Dec 3 '18 at 10:02











  • Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

    – Uli Schlachter
    Dec 3 '18 at 11:04



















  • I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

    – Murat Şeker
    Dec 3 '18 at 10:02











  • Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

    – Uli Schlachter
    Dec 3 '18 at 11:04

















I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

– Murat Şeker
Dec 3 '18 at 10:02





I have to say that I'm impressed :) but this kind of tricks are not allowed on our codebase.

– Murat Şeker
Dec 3 '18 at 10:02













Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

– Uli Schlachter
Dec 3 '18 at 11:04





Good for you that such hacks are not allowed. Bad for you that then your only option is Xlib, I guess. (You are not allowed to interpose libc's exit() to replace it with your own no-op function, right? ;-) )

– Uli Schlachter
Dec 3 '18 at 11:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396936%2fxlib-disconnection-callback%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings