File: example_module.c

package info (click to toggle)
vlock 2.2.2-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 488 kB
  • ctags: 324
  • sloc: ansic: 2,950; sh: 873; makefile: 236
file content (90 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download | duplicates (7)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* example_module.c -- example module for vlock,
 *                     the VT locking program for linux
 *
 * This program is copyright (C) 2007 Frank Benkstein, and is free
 * software.  It comes without any warranty, to the extent permitted by
 * applicable law.  You can redistribute it and/or modify it under the
 * terms of the Do What The Fuck You Want To Public License, Version 2,
 * as published by Sam Hocevar.  See http://sam.zoy.org/wtfpl/COPYING
 * for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

/* Do not use any vlock specific headers here unless you intend to
 * submit your module for inclusion. */

/* Include this header file to make sure the types of the dependencies
 * and hooks are correct. */
#include "vlock_plugin.h"

/* Declare dependencies.  Please see PLUGINS for their meaning.  Empty
 * dependencies can be left out. */
const char *preceeds[] = { "new", "all", NULL };
/* const char *succeeds[]; */
/* const char *requires[]; */
/* const char *needs[]; */
const char *depends[] = { "all", NULL };
/* const char *conflicts[]; */

/* Every hook has a void** argument ctx_ptr.  When they are called
 * ctx_ptr points to the same location and *ctx_ptr is initially set to
 * NULL.  Hook functions should pass state by defining a context struct
 * and saving the pointer to it in *ctx_ptr instead of using global
 * variables.
 */

struct example_context {
  int a;
  int b;
};

/* Do something that should happen at vlock's start here.  An error in
 * this hook aborts vlock. */
bool vlock_start(void **ctx_ptr)
{
  struct example_context *ctx = malloc(sizeof *ctx);

  if (ctx == NULL)
    return false;

  ctx->a = 23;
  ctx->b = 42;

  /* Save the context for use by the other hooks. */
  *ctx_ptr = ctx;

  return true;
}

/* Hooks that are not implemented should not be defined. */

/* Start a screensaver type action before the password prompt after a
 * timeout.  This hook must not block! */
/* bool vlock_save(void **); */

/* Abort a screensaver type action before the password prompt after a
 * timeout.  This hook must not block! */
/* bool vlock_save_abort(void **); */

/* Do something at the end of vlock.  Error returns are ignored here. */
bool vlock_end(void **ctx_ptr)
{
  struct example_context *ctx = *ctx_ptr;
  bool result = true;

  if (ctx != NULL) {
    result = (ctx->a == 23 && ctx->b == 42);

    free(ctx);

    if (!result)
      fprintf(stderr, "vlock-example_module: Whoops!\n");
  }

  return result;
}