Version 0.1.0 from FTP
[asterisk/asterisk.git] / apps / app_skel.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Skeleton application
5  * 
6  * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include <pthread.h>
24
25 static pthread_mutex_t skellock = PTHREAD_MUTEX_INITIALIZER;
26
27 static int usecnt=0;
28
29 static char *tdesc = "Trivial skeleton Application";
30
31 static char *app = "skel";
32
33 struct skeluser {
34         struct ast_channel *chan;
35         struct skeluser *next;
36 } *users = NULL;
37
38 static int skel_exec(struct ast_channel *chan, void *data)
39 {
40         int res=0;
41         struct skeluser *u, *ul=NULL;
42         if (!data) {
43                 ast_log(LOG_WARNING, "skel requires an argument (filename)\n");
44                 return -1;
45         }
46         if (!(u=malloc(sizeof(struct skeluser)))) {
47                 ast_log(LOG_WARNING, "Out of memory\n");
48                 return -1;
49         }
50         pthread_mutex_lock(&skellock);
51         u->chan = chan;
52         u->next = users;
53         users = u;
54         usecnt++;
55         pthread_mutex_unlock(&skellock);
56         /* Do our thing here */
57         pthread_mutex_lock(&skellock);
58         u = users;
59         while(u) {
60                 if (ul)
61                         ul->next = u->next;
62                 else
63                         users = u->next;
64                 u = u->next;
65         }
66         usecnt--;
67         pthread_mutex_unlock(&skellock);
68         return res;
69 }
70
71 int unload_module(void)
72 {
73         struct skeluser *u;
74         pthread_mutex_lock(&skellock);
75         u = users;
76         while(u) {
77                 /* Hang up anybody who is using us */
78                 ast_softhangup(u->chan);
79                 u = u->next;
80         }
81         pthread_mutex_unlock(&skellock);
82         return ast_unregister_application(app);
83 }
84
85 int load_module(void)
86 {
87         return ast_register_application(app, skel_exec);
88 }
89
90 char *description(void)
91 {
92         return tdesc;
93 }
94
95 int usecount(void)
96 {
97         int res;
98         pthread_mutex_lock(&skellock);
99         res = usecnt;
100         pthread_mutex_unlock(&skellock);
101         return res;
102 }