Skip to content

Commit

Permalink
doc: lead with example, add missing links
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Dec 20, 2023
1 parent bf5747d commit 3f85574
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Table of Contents
-----------------

* [Introduction](#introduction)
* [Usage](#usage)
* [Features](doc/features.md)
- [Delegating Reboot](doc/features.md#delegating-reboot)
- [Built-in Monitors](doc/features.md#built-in-monitors)
Expand Down Expand Up @@ -163,7 +162,6 @@ the `watchdogd` project, copyright © [Ron Leishman][].
[Coverity Scan]: https://scan.coverity.com/projects/6458
[Coverity Status]: https://scan.coverity.com/projects/6458/badge.svg
[GitHub]: http://github.com/troglobit/watchdogd
[ex1]: https://github.com/troglobit/watchdogd/blob/master/examples/ex1.c
[LICENSE]: https://github.com/troglobit/watchdogd/blob/master/LICENSE
[contrib]: https://github.com/troglobit/watchdogd/blob/master/.github/CONTRIBUTING.md
[Joachim Wiberg]: http://troglobit.com
Expand Down
70 changes: 38 additions & 32 deletions doc/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ debug log level, without having to restart a running daemon.


<a href="https://codedocs.xyz/troglobit/watchdogd/wdog_8h.html"><img
align="right" src="doc/api.png" alt="API" title="API docs"></a>
align="right" src="api.png" alt="API" title="API docs"></a>

libwdog API
-----------
Expand All @@ -24,6 +24,41 @@ To have `watchdogd` supervise a process, it must be instrumented with at
least a "subscribe" and a "kick" API call. Commonly this is achieved by
adding the `wdog_kick()` call to the main event loop.

🕮 <https://codedocs.xyz/troglobit/watchdogd/wdog_8h.html>

### Example

For other applications, identify your main loop, its max period time and
instrument it like this:

```C
int ack, wid;

/* Library will use process' name on NULL first arg. */
wid = wdog_subscribe(NULL, 10000, &ack);
if (-1 == wid)
; /* Error handling */

while (1) {
...
/* Ensure kick is called periodically, < 10 sec */
wdog_kick2(wid, &ack);
...
}
```

This example subscribe to the watchdog with a 10 sec timeout. The `wid`
is used in the call to `wdog_kick2()`, with the received `ack` value.
Which is changed every time the application calls `wdog_kick2()`, so it
is important the correct value is used. Applications should of course
check the return value of `wdog_subscribe()` for errors, that code is
left out for readability.

See also the [example/ex1.c][ex1] in the source distribution. This is
used by the automatic tests.

### API

All libwdog API functions, except `wdog_ping()`, return POSIX OK(0) or
negative value with `errno` set on error. The `wdog_subscribe()` call
returns a positive integer (including zero) for the watchdog `id`.
Expand Down Expand Up @@ -58,34 +93,5 @@ similar. For such libraries one can simply add a timer callback for the
kick to run periodically to monitor proper operation of the client.
### Example
For other applications, identify your main loop, its max period time and
instrument it like this:
```C
int ack, wid;
/* Library will use process' name on NULL first arg. */
wid = wdog_subscribe(NULL, 10000, &ack);
if (-1 == wid)
; /* Error handling */
while (1) {
...
/* Ensure kick is called periodically, < 10 sec */
wdog_kick2(wid, &ack);
...
}
```

This example subscribe to the watchdog with a 10 sec timeout. The `wid`
is used in the call to `wdog_kick2()`, with the received `ack` value.
Which is changed every time the application calls `wdog_kick2()`, so it
is important the correct value is used. Applications should of course
check the return value of `wdog_subscribe()` for errors, that code is
left out for readability.

See also the [example/ex1.c][ex1] in the source distribution. This is
used by the automatic tests.

[libuEv]: https://github.com/troglobit/libuev/
[ex1]: https://github.com/troglobit/watchdogd/blob/master/examples/ex1.c

0 comments on commit 3f85574

Please sign in to comment.