From 91263ea95ec16bb376245c6931ea28d65bdcdc88 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 27 Jul 2003 19:18:57 +0000 Subject: Moved to standard repository layout git-svn-id: file:///home/lennart/svn/public/ivcall/trunk@2 e0b13411-74c3-0310-b366-a0654dd0340f --- LICENSE | 340 ++++++++++++++++++++++++ Makefile.am | 35 +++ bootstrap.sh | 38 +++ configure.ac | 97 +++++++ doc/Makefile.am | 35 +++ doc/README.html.in | 131 ++++++++++ doc/style.css | 12 + man/Makefile.am | 30 +++ man/ivcall.1.xml.in | 111 ++++++++ src/Makefile.am | 21 ++ src/ivcall.c | 729 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lock.c | 130 ++++++++++ src/lock.h | 28 ++ src/util.c | 65 +++++ src/util.h | 28 ++ 15 files changed, 1830 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile.am create mode 100755 bootstrap.sh create mode 100644 configure.ac create mode 100644 doc/Makefile.am create mode 100644 doc/README.html.in create mode 100644 doc/style.css create mode 100644 man/Makefile.am create mode 100644 man/ivcall.1.xml.in create mode 100644 src/Makefile.am create mode 100644 src/ivcall.c create mode 100644 src/lock.c create mode 100644 src/lock.h create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d60c31a --- /dev/null +++ b/LICENSE @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..4826b6a --- /dev/null +++ b/Makefile.am @@ -0,0 +1,35 @@ +# $Id$ + +# This file is part of ivcall. +# +# ivcall is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ivcall is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ivcall; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +EXTRA_DIST=bootstrap.sh README LICENSE +SUBDIRS=src man doc + +MAINTAINERCLEANFILES = README +noinst_DATA = README + +README: + rm -f README + $(MAKE) -C doc README + ln -s doc/README README + +homepage: + test -d $$HOME/homepage/lennart + mkdir -p $$HOME/homepage/lennart/projects/ivcall + cp *.tar.gz $$HOME/homepage/lennart/projects/ivcall + cp doc/README.html doc/style.css doc/NEWS $$HOME/homepage/lennart/projects/ivcall + cp $$HOME/homepage/lennart/projects/ivcall/README.html $$HOME/homepage/lennart/projects/ivcall/index.html diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..73e6c40 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# $Id$ + +# This file is part of ivcall. +# +# ivcall is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ivcall is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ivcall; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +if [ "x$1" = "xam" ] ; then + set -ex + automake -a -c + ./config.status +else + set -ex + + make maintainer-clean || true + + rm -rf autom4te.cache + rm -f config.cache + + aclocal + autoheader + automake -a -c + autoconf -Wall + + ./configure "$@" +fi diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..b6740bc --- /dev/null +++ b/configure.ac @@ -0,0 +1,97 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +# $Id$ + +# This file is part of ivcall. +# +# ivcall is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ivcall is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ivcall; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +AC_PREREQ(2.57) +AC_INIT([ivcall], [0.2], [mzvipnyy@itaparica.org]) +AC_CONFIG_SRCDIR([src/ivcall.c]) +AC_CONFIG_HEADERS([config.h]) +AM_INIT_AUTOMAKE([foreign -Wall]) +AM_MAINTAINER_MODE + +# Checks for programs. +AC_PROG_CC + +# If using GCC specifiy some additional parameters +if test "x$GCC" = "xyes" ; then + CFLAGS="$CFLAGS -pipe -Wall" +fi + +AC_PROG_CPP +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_MAKE_SET + +AC_CHECK_FUNCS([alarm memset select strdup strerror strrchr]) +AC_CHECK_HEADERS([fcntl.h limits.h stdlib.h string.h sys/ioctl.h sys/time.h termios.h unistd.h]) + +AC_C_CONST +AC_C_VOLATILE +AC_FUNC_MALLOC +AC_FUNC_MEMCMP +AC_FUNC_REALLOC +AC_FUNC_SELECT_ARGTYPES +AC_HEADER_STDC +AC_PROG_GCC_TRADITIONAL +AC_TYPE_PID_T +AC_TYPE_SIGNAL +AC_TYPE_SIZE_T + +# LYNX documentation generation +AC_ARG_ENABLE(lynx, + AC_HELP_STRING([--disable-lynx], [Turn off lynx usage for documentation generation]), +[case "${enableval}" in + yes) lynx=yes ;; + no) lynx=no ;; + *) AC_MSG_ERROR(bad value ${enableval} for --disable-lynx) ;; +esac],[lynx=yes]) + +if test x$lynx = xyes ; then + AC_CHECK_PROG(have_lynx, lynx, yes, no) + + if test x$have_lynx = xno ; then + AC_MSG_ERROR([*** Sorry, you have to install lynx or use --disable-lynx ***]) + fi +fi + +AM_CONDITIONAL([USE_LYNX], [test "x$lynx" = xyes]) + +# XMLTOMAN manpage generation +AC_ARG_ENABLE(xmltoman, + AC_HELP_STRING([--disable-xmltoman], [Disable rebuilding of man pages with xmltoman]), +[case "${enableval}" in + yes) xmltoman=yes ;; + no) xmltoman=no ;; + *) AC_MSG_ERROR([bad value ${enableval} for --disable-xmltoman]) ;; +esac],[xmltoman=yes]) + +if test x$xmltoman = xyes ; then + AC_CHECK_PROG(have_xmltoman, xmltoman, yes, no) + + if test x$have_xmltoman = xno ; then + AC_MSG_WARN([*** Not rebuilding man pages as xmltoman is not found ***]) + xmltoman=no + fi +fi + +AM_CONDITIONAL([USE_XMLTOMAN], [test "x$xmltoman" = xyes]) + +AC_CONFIG_FILES([src/Makefile Makefile man/Makefile doc/Makefile doc/README.html]) +AC_OUTPUT diff --git a/doc/Makefile.am b/doc/Makefile.am new file mode 100644 index 0000000..a6b9bb7 --- /dev/null +++ b/doc/Makefile.am @@ -0,0 +1,35 @@ +# $Id$ + +# This file is part of ivcall. +# +# ivcall is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ivcall is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ivcall; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +noinst_DATA = README.html README +EXTRA_DIST = $(noinst_DATA) style.css README.html.in + +MAINTAINERCLEANFILES = README README.html +CLEANFILES = + +if USE_LYNX +README: README.html + lynx --dump $^ | sed 's,file://localhost/.*/doc/README.html,README,' > $@ + +CLEANFILES += README +endif + +tidy: README.html + tidy -e < README.html + +.PHONY: tidy diff --git a/doc/README.html.in b/doc/README.html.in new file mode 100644 index 0000000..bf7a739 --- /dev/null +++ b/doc/README.html.in @@ -0,0 +1,131 @@ + + + + + +ivcall @PACKAGE_VERSION@ + + + + +

ivcall @PACKAGE_VERSION@

+ +

Copyright 2002,2003 Lennart Poettering <mzvscyhtq [at] itaparica.org>

+ + + +

License

+ +

This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version.

+ +

This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details.

+ +

You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

+ +

News

+ +
Sun July 27 2003:

Version +0.2 released, changes include: many fixes, autoconf support

+ +

Overview

+ +

ivcall is a small utility which may be used to make automated +telephone calls with your isdn4linux supported ISDN +card. Outgoing calls are +supported as well as incoming calls. The audio data recieved +from the peer is written to STDOUT, audio data read from STDIN +is send to the peer. The audio data is in raw 8 bit uLaw 8 KHz format, +without any headers.

+ +

Status

+ +

Version @PACKAGE_VERSION@ is stable and feature complete.

+ +

Documentation

+ +

You may use ivcall as a Unix filter: + +

+ivcall LMSN RMSN < IFILE > OFILE
+
+ +

Replace LMSN by the local MSN and RMSN by the MSN +the program shall call. IFILE must be a raw uLAW 8 KHz +bytesized file, which is be played to the peer. OFILE is the +file the recorded audio data will be written to. It has the same +format as the input file. If no recording (or playback) is desired you +may pipe STDOUT (or STDIN) to /dev/null. + +

Four detailed usage information try:

+ +
+ivcall --help
+
+ +

If you want to create a sample playable with ivcall with your +soundcard, you'll need sox:

+ +
$ rec -t raw -r 8000 -U -b FILE
+ +

Don't forget to plug in a microphone and set the record volume with +aumix or something similar. However, it is recommended to create +audio samples with the telephone itself.

+ +

If you have a wave file you want to convert to a sample playable +with ivcall use sox as well:

+ +
$ sox IFILE -t raw -r 8000 -U -b OFILE
+ +

Requirements

+ +

A newer Linux Kernel (I think 2.0 is good enough, alltough I tested it +only with 2.4) with a compatible device driver (isdn4linux). + +

ivcall was developed and tested on Debian GNU/Linux +"testing" from July 2003, it should work on most other Linux +distributions since it uses GNU autoconf for source code configuration.

+ +

Installation

+ +

As this package is made with the GNU autotools you should run +./configure inside the distribution directory for configuring +the source tree. After that you should run make for +compilation and make install (as root) for installation of +ivcall.

+ +

Acknowledgements

+ +

For the developers of awag (Stefan Szomraky), minicom (Miquel van Smoorenburg) and vbox (Michael Herold), since I looked +on their source codes for learning how to use the isdn4linux modem emulation

+ +

Download

+ +

The newest release is always available from http://www.stud.uni-hamburg.de/~lennart/projects/ivcall/

+ +

The current release is @PACKAGE_VERSION@

+ +
+
Lennart Poettering <mzvscyhtq [at] itaparica.org>, July 2003
+
$Id$
+ + + diff --git a/doc/style.css b/doc/style.css new file mode 100644 index 0000000..0a40aef --- /dev/null +++ b/doc/style.css @@ -0,0 +1,12 @@ +/* $Id$ */ +body { color: black; background-color: white; margin: 0.5cm; } +a:link, a:visited { color: #900000; } +p { margin-left: 0.5cm; margin-right: 0.5cm; } +div.news-date { margin-left: 0.5cm; font-size: 80%; color: #4f0000; } +p.news-text { margin-left: 1cm; } +ul { margin-left: .5cm; } +ol { margin-left: .5cm; } +h1 { color: #00009F; } +h2 { color: #00009F; } +h3 { color: #00004F; margin-left: 0.5cm; } +pre { margin-left: .5cm; background-color: #f0f0f0; padding: 0.4cm;} diff --git a/man/Makefile.am b/man/Makefile.am new file mode 100644 index 0000000..10da8b5 --- /dev/null +++ b/man/Makefile.am @@ -0,0 +1,30 @@ +# $Id$ + +# This file is part of ivcall. +# +# ivcall is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ivcall is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ivcall; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +man_MANS = ivcall.1 + +EXTRA_DIST = $(man_MANS) ivcall.1.xml.in + +if USE_XMLTOMAN + +CLEANFILES = $(man_MANS) + +ivcall.1: ivcall.1.xml.in Makefile + sed -e 's,@sysconfdir\@,$(sysconfdir),g' -e 's,@sbindir\@,$(sbindir),g' -e 's,@PACKAGE_BUGREPORT\@,$(PACKAGE_BUGREPORT),g' $< | xmltoman - > $@ + +endif diff --git a/man/ivcall.1.xml.in b/man/ivcall.1.xml.in new file mode 100644 index 0000000..c861f27 --- /dev/null +++ b/man/ivcall.1.xml.in @@ -0,0 +1,111 @@ + + + + + + + + + + + ivcall [options] <source-msn> <destination-msn> + ivcall -a <options> <msn> + + + +

ivcall is a small utility which may be used to make automated + telephone calls with your isdn4linux supported ISDN card. Outgoing calls are + supported as well as incoming calls. The audio data recieved + from the peer is written to STDOUT, audio data read from STDIN + is send to the peer. The audio data is in raw 8 bit uLaw 8 KHz format, + without any headers.

+
+ + + + + + + + + + + + + + + + + + + + +
+

SIGINT ivcall will quit and terminate the connection.

+
+ +
+

ivcall was written by Lennart Poettering + <@PACKAGE_BUGREPORT@>. ivcall is available + at +

+
+ +
+

This man page was written using by Oliver Kurth.

+
+ +
diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..1fa04d5 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,21 @@ +# $Id$ + +# This file is part of ifplugd. +# +# ifplugd is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# ifplugd is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ifplugd; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +bin_PROGRAMS = ivcall +ivcall_SOURCES = ivcall.c lock.c lock.h util.c util.h + diff --git a/src/ivcall.c b/src/ivcall.c new file mode 100644 index 0000000..54c840d --- /dev/null +++ b/src/ivcall.c @@ -0,0 +1,729 @@ +/* $Id$ */ + +/* + * This file is part of ivcall. + * + * ivcall is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * ivcall is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ivcall; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" +#include "lock.h" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +// Where do lockfiles reside? +#ifndef LOCK_PATH +#define LOCK_PATH "/var/lock" +#endif + +// Those nifty DLE-sequences +#define DLE 0x10 +#define ETX 0x03 +#define DC4 0x14 + +// Baudrate for the TTY. Should be greater the 64000. +#define BAUD_RATE B115200 + +// Every single commands gets a timeout of 2 seconds. +#define COMMAND_TIMEOUT 2 + +// The default timeout for waiting that the call gets accepted. +#define DEFAULT_TIMEOUT 30 + +// Initialisation of the modem +const char *at_commands[] = { + "AT&F\n", "OK\r\n", + "ATI\n", "Linux ISDN\r\nOK", + "AT&B128\n", "OK\r\n", + "AT+FCLASS=8\n", "OK\r\n", + "AT+VSM=6\n", "OK\r\n", + "ATS18=1\n", "OK\r\n", + "ATS14=4\n", "OK\r\n", + "ATS13.4=1\n", "OK\r\n", + "ATS23=1\n", "OK\r\n", + 0 +}; + +// Local MSN +char *source_msn = NULL; + +// Remote MSN +char *destination_msn = NULL; + +// Modem-device +char *device = NULL; + +// Our name +char *appname = NULL; + +// The default timeout +int timeout = DEFAULT_TIMEOUT; + +// Enable debug-mode or not? +int debug = 0; + +// We got a ctrl-c +volatile int interrupted = 0; + +// Got an SIGARLM? +volatile int alarmed = 0; + +// Shall we wait for a call? +int answer = 0; + +// After how many RINGs we shall answer? +int rings = 1; + +// Saved TTY configuration +struct termios saved_termios; + +// Our ctrl-c handler +void sigint(int sig) { + const char *s = "Got SIGINT\n"; + write(2, s, strlen(s)); + interrupted = 1; +} + +void sigalrm(int sig) { + const char *s = "Timeout\n"; + write(2, s, strlen(s)); + alarmed = 1; +} + +// Waits for a certain string from the modem up to a certain time. +int tty_waitfor(int fd, const char *what, int t) { + static char buf[256]; + char *p = buf; + int r = -1, l; + + if (debug) + fprintf(stderr, "<- "); + + l = strlen(what); + + if (t == -1) + t = 0; + alarm(0); + alarmed = 0; + alarm(t); + + while (!interrupted && !alarmed) { + ssize_t s; + + if ((s = read(fd, p, 1)) <= 0) + break; + + if (debug) + fprintf(stderr, "%c", *p); + + p++; + + if (p - buf >= l && !strncmp(p-l, what, l)) { + r = 0; + break; + } + } + + + alarm(0); + alarmed = 0; + + if (debug) + fprintf(stderr, "\n"); + + return r; +} + +// Write a string to the modem +int tty_puts(int fd, const char *s) { + int l = strlen(s); + + if (loop_write(fd, s, l) != l) + return -1; + + if (tcdrain(fd) < 0) + return -1; + + if (debug) + fprintf(stderr, "-> %s", s); + + return 0; +} + +// Issue a command to the modem an wait for an answer +int tty_command(int fd, const char *cmd, const char* resp, int t) { + tcflush(fd, TCIFLUSH); + tty_puts(fd, cmd); + return tty_waitfor(fd, resp, t); +} + +// Hang up the modem +int hard_hangup(int fd) { + struct termios pts; + + tcflush(fd, TCIOFLUSH); + + if (tcgetattr(fd, &pts) < 0) + return -1; + + cfsetospeed(&pts, B0); + cfsetispeed(&pts, B0); + tcsetattr(fd, TCSANOW, &pts); + + sleep(1); + + if (tcgetattr(fd, &pts) < 0) + return -1; + cfsetospeed(&pts, BAUD_RATE); + cfsetispeed(&pts, BAUD_RATE); + tcsetattr(fd, TCSANOW, &pts); + + sleep(1); + + tcflush(fd, TCIFLUSH); + + return 0; +} + +// Opens the modem and sets the baud rate +int tty_open(const char *device) { + struct termios pts, pts2; + int fd = -1, n; + + if ((fd = open(device, O_RDWR|O_NDELAY)) < 0) + goto fail; + + if ((n = fcntl(fd, F_GETFL, 0)) < 0) + goto fail; + + if (fcntl(fd, F_SETFL, n & ~O_NDELAY) < 0) + goto fail; + + if (tcgetattr(fd, &saved_termios) < 0) + goto fail; + + memset(&pts, 0, sizeof(pts)); + pts.c_cflag = CRTSCTS | IGNPAR | HUPCL | CS8; + pts.c_iflag = IGNPAR; + pts.c_oflag = 0; + pts.c_lflag = 0; + + pts.c_cc[VMIN] = 1; + pts.c_cc[VTIME] = 0; + + cfsetospeed(&pts, BAUD_RATE); + cfsetispeed(&pts, BAUD_RATE); + + tcflush(fd, TCIOFLUSH); + if (!tcsetattr(fd, TCSANOW, &pts)) + if (!tcgetattr(fd, &pts2)) + if (!memcmp(&pts, &pts, sizeof(struct termios))) + return fd; + + tcsetattr(fd, TCSANOW, &saved_termios); + +fail: + + if (fd >= 0) + close(fd); + + return -1; +} + +// Closes the modem device and resets the baudrate +void tty_close(int fd) { + tcflush(fd, TCIOFLUSH); + tcsetattr(fd, TCSANOW, &saved_termios); + close(fd); +} + +// Detects the DCD line +int detect_carrier(int fd) { + unsigned int arg; + + if (ioctl(fd, TIOCMGET, &arg) < 0) + return -1; + + return arg & TIOCM_CAR ? 1 : 0; +} + +// Filters out DLE-sequences from modem +char *filter_dle(unsigned char *data, int *size, int *flag, int *quit) { + unsigned char *ret, *m, *s, *d; + int l; + + if (!(ret = malloc(*size))) + return NULL; + + for (l = *size, s = data, d = ret; l > 0; l--, s++) { + if (*flag) { + switch (*s) { + case DLE : + *(d++) = DLE; + break; + + case ETX : + case DC4 : + *quit = 1; + break; + + default : + // We recieved some DTMF signals, but we ignore them. + break; + } + + *flag = 0; + continue; + } + + if (*s == DLE) + *flag = 1; + else + *(d++) = *s; + + } + + *size = d-ret; + + if (!(m = realloc(ret, *size))) { + free(ret); + return NULL; + } + + return m; +} + +// Escape DLE-bytes for sending data to the modem +char* escape_dle(unsigned char *data, int *size) { + unsigned char *ret, *m, *s, *d; + int l, ms; + + if (!(ret = malloc(ms = (int) (*size*1.05)))) + return NULL; + + for (l = *size, s = data, d = ret; l > 0; l--, s++) { + + if (*s == DLE) { + *(d++) = DLE; + (*size)++; + + if (ms < *size) { + int n = d-ret; + + fprintf(stderr, "REALLOC!\n"); + + if (!(m = realloc(ret, ms = (int) (*size*1.05)))) { + free(ret); + return NULL; + } + ret = m; + d = ret+n; + } + } + + *(d++) = *s; + } + + if (!(m = realloc(ret, *size))) { + free(ret); + return NULL; + } + + return m; +} + + +// The function that does the modem work +int go(void) { + int i, flag, warned = 0, r = -1, fd = -1, quit; + char *incoming = NULL, *outgoing = NULL; + int incoming_idx = 0, incoming_len = 0, + outgoing_idx = 0, outgoing_len = 0; + int fm; + char fn[PATH_MAX]; + static char buf[1024]; + + if ((device ? device_lock(device, appname) : device_lock_first(appname, fn, sizeof(fn))) < 0) { + fprintf(stderr, "Could not get lock on device: %s\n", strerror(errno)); + goto fail; + } + + if (!device) + if (!(device = strdup(fn))) { + fprintf(stderr, "Out of memory!\n"); + goto fail; + } + + if ((fd = tty_open(device)) < 0) { + fprintf(stderr, "Could not open device: %s\n", strerror(errno)); + goto fail; + } + + for (i = 0; at_commands[i] != 0; i+=2) + if (tty_command(fd, at_commands[i], at_commands[i+1], COMMAND_TIMEOUT) < 0) { + fprintf(stderr, "Initialisation failure.\n"); + goto fail; + } + + snprintf(buf, sizeof(buf), "AT&E%s\n", source_msn); + if (tty_command(fd, buf, "OK\r\n", COMMAND_TIMEOUT) < 0) { + fprintf(stderr, "Initialisation failure.\n"); + goto fail; + } + + if (answer) + snprintf(buf, sizeof(buf), "ATS0=%i\n", rings); + else + snprintf(buf, sizeof(buf), "ATD%s\n", destination_msn); + + if (tty_command(fd, buf, "VCON\r\n", timeout) < 0) { + fprintf(stderr, "Failed to accept connection.\n"); + r = -2; + goto fail; + } + + if (tty_command(fd, "AT+VTX+VRX\n", "CONNECT\r\n\r\nCONNECT\r\n", COMMAND_TIMEOUT) < 0) { + fprintf(stderr, "Could not switch to full-duplex audio: %s\n", strerror(errno)); + goto fail; + } + + flag = quit= 0; + + if (fd > 1) + fm = fd+1; + else + fm = 1+1; + + while (!interrupted && (!quit || outgoing || incoming)) { + fd_set rfds, wfds; + int c, b; + + if ((c = detect_carrier(fd)) < 0) { + fprintf(stderr, "Failure reading DCD line: %s\n", strerror(errno)); + goto fail; + } else if (!c) { + if (outgoing) { + free(outgoing); + outgoing = NULL; + } + quit = 1; + break; + } + + b = 0; + FD_ZERO(&rfds); + if (!quit) { + if (!outgoing) { + FD_SET(0, &rfds); + b = 1; + } + if (!incoming) { + b = 1; + FD_SET(fd, &rfds); + } + } + + FD_ZERO(&wfds); + if (outgoing) { + b=1; + FD_SET(fd, &wfds); + } + if (incoming) { + b = 1; + FD_SET(1, &wfds); + } + + if (!b) + break; + + if ((r = select(fm, &rfds, &wfds, 0, 0)) < 0) { + if (errno == EINTR) + continue; + + fprintf(stderr, "select(): %s\n", strerror(errno)); + goto fail; + } + + + if (!outgoing && FD_ISSET(0, &rfds)) { + if ((outgoing_len = read(0, buf, sizeof(buf))) < 0) { + fprintf(stderr, "read(): %s\n", strerror(errno)); + goto fail; + } + + if (!outgoing_len) + quit = 1; + else { + outgoing = escape_dle(buf, &outgoing_len); + outgoing_idx = 0; + } + } + + if (outgoing && FD_ISSET(fd, &wfds)) { + ssize_t l; + if ((l = write(fd, outgoing+outgoing_idx, outgoing_len-outgoing_idx)) < 0) { + fprintf(stderr, "write(): %s\n", strerror(errno)); + goto fail; + } + + if (!l) + quit = 1; + else { + outgoing_idx += l; + + if (outgoing_idx >= outgoing_len) { + free(outgoing); + outgoing = NULL; + outgoing_len = outgoing_idx = 0; + } + } + } + + + if (!incoming && FD_ISSET(fd, &rfds)) { + if ((incoming_len = read(fd, buf, sizeof(buf))) < 0) { + fprintf(stderr, "read(): %s\n", strerror(errno)); + goto fail; + } + + if (!incoming_len) + quit = 1; + else { + incoming = filter_dle(buf, &incoming_len, &flag, &quit); + incoming_idx = 0; + } + } + + if (incoming && FD_ISSET(1, &wfds)) { + ssize_t l; + if (isatty(1)) { + if (!warned) { + fprintf(stderr, "I won't print out binary data to a tty. Pipe stdout to somewhere else.\n"); + warned = 1; + } + + free(incoming); + incoming = NULL; + incoming_len = incoming_idx = 0; + + } else { + if ((l = write(1, incoming+incoming_idx, incoming_len-incoming_idx)) < 0) { + fprintf(stderr, "write(1): %s\n", strerror(errno)); + goto fail; + } + + if (!l) + quit = 1; + else { + incoming_idx += l; + + if (incoming_idx >= incoming_len) { + free(incoming); + incoming = NULL; + incoming_len = incoming_idx = 0; + } + } + } + } + } + + + buf[0] = DLE; + buf[1] = DC4; + buf[2] = DLE; + buf[3] = ETX; + loop_write(fd, buf, 4); + tcdrain(fd); + tty_puts(fd, "ATH\n"); + + r = 0; + +fail: + + if (incoming) + free(incoming); + + if (outgoing) + free(outgoing); + + if (fd >= 0) { + hard_hangup(fd); + tty_puts(fd, "AT&F\n"); + tty_close(fd); + device_unlock(); + } + + return r; +} + +struct option long_options[] = { + {"help", 0, 0, 'h'}, + {"timeout", 1, 0, 't'}, + {"device", 1, 0, 'd'}, + {"verbose", 0, 0, 'v'}, + {"answer", 0, 0, 'a'}, + {"ring", 1, 0, 'r'}, + {"version", 0, 0, 'V'}, + {0, 0, 0, 0} +}; + +// Most important part of the program +void usage() { + printf("Usage: %s [options] \n" + " %s -a [options] \n" + "\n" + "Options:\n" + " -h, --help Shows this usage information\n" + " -t, --timeout=TIMEOUT Sets a timeout (%i)\n" + " -d, --device=DEVICE Selects a device (if not specified, the first available /dev/ttyInn is used)\n" + " -v, --verbose Enables debug mode (%s)\n" + " -a, --answer Wait for incoming call instead of calling out (%s)\n" + " -r, --ring=RINGS On incoming call answer after given RINGs (%i)\n" + " -V, --version Show %s version number\n", + appname, appname, + timeout, + debug ? "on" : "off", + answer ? "on" : "off", + rings, + appname); +} + +// Parses command line and executes go() +int main (int argc, char *argv[]) { + int c, r = 1; + + appname = basename(argv[0]); + + while((c = getopt_long(argc, argv, "ht:d:var:V", long_options, NULL)) >= 0) { + + switch (c) { + case 'V' : + printf("%s "VERSION"\n", appname); + r = 0; + goto finish; + + case 'h' : + usage(); + r = 0; + goto finish; + + case 't' : + if (optarg) { + if ((timeout = atoi(optarg)) < 1) { + fprintf(stderr, "Timeout must be >= 1.\n"); + goto finish; + } + } else { + fprintf(stderr, "Expected argument on timeout option.\n"); + goto finish; + } + break; + + case 'd' : + if (optarg) { + if (device) + free(device); + else + device = strdup(optarg); + } else { + fprintf(stderr, "Expected argument on device option.\n"); + goto finish; + } + break; + + case 'v' : + debug = !debug; + break; + + case 'a' : + answer = !answer; + break; + + case 'r' : + if (optarg) { + + if ((rings = atoi(optarg)) < 1) { + fprintf(stderr, "RINGs must be >= 1.\n"); + goto finish; + } + } else { + fprintf(stderr, "Expected argument on rings option.\n"); + goto finish; + } + break; + + case '?' : + goto finish; + } + } + + if (answer) { + if (optind != argc-1) { + fprintf(stderr, "You did not specify the listening MSN.\n"); + usage(); + goto finish; + } + + source_msn = strdup(argv[optind]); + } else { + if (optind != argc-2) { + fprintf(stderr, "You did not specify both destination and source MSNs.\n"); + usage(); + goto finish; + } + + source_msn = strdup(argv[optind]); + destination_msn = strdup(argv[optind+1]); + } + + if (!timeout) + timeout = answer ? -1 : DEFAULT_TIMEOUT; + + signal(SIGINT, &sigint); + signal(SIGALRM, &sigalrm); + signal(SIGPIPE, SIG_IGN); + siginterrupt(SIGINT, 1); + siginterrupt(SIGALRM, 1); + + r = go() < 0 ? 1 : 0; + +finish: + + if (source_msn) free (source_msn); + if (destination_msn) free(destination_msn); + if (device) free(device); + + return r; +} + diff --git a/src/lock.c b/src/lock.c new file mode 100644 index 0000000..465736c --- /dev/null +++ b/src/lock.c @@ -0,0 +1,130 @@ +/* $Id$ */ + +/* + * This file is part of ivcall. + * + * ivcall is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * ivcall is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ivcall; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lock.h" +#include "util.h" + +// Where do lockfiles reside? +#define LOCK_PATH "/var/lock" + +static char* lockfile = NULL; + +// Tries to lock a TTY-device, returns 0 on success +int device_lock(char *dev, char *appname) { + char buf[256], u[16]; + char path[PATH_MAX]; + int fd; + struct passwd *pwd; + char *username; + + assert(dev && !lockfile); + + if (access(LOCK_PATH, W_OK) < 0) + return -1; + + snprintf(path, sizeof(path), "%s/LCK..%s", LOCK_PATH, basename(dev)); + + if ((fd = open(path, O_RDONLY)) >= 0) { + int n = loop_read(fd, buf, sizeof(buf) - 1); + close(fd); + + if (n > 0) { + int pid; + + if (n == 4) + pid = *(int*) buf; + else { + buf[n] = 0; + sscanf(buf, "%d", &pid); + } + + if (pid > 0) { + if (kill((pid_t) pid, 0) < 0 && errno == ESRCH) { + fprintf(stderr, "Lockfile is stale. Overriding it.\n"); + unlink(path); + } else + return -1; + } + } + } + + if ((fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) + return -1; + + if ((pwd = getpwuid(getuid()))) + username = pwd->pw_name; + else + snprintf(username = u, sizeof(u), "%u", getuid()); + + snprintf(buf, sizeof(buf), "%10ld %s %.20s\n", (long)getpid(), appname, username); + + if (loop_write(fd, buf, strlen(buf)) < 0) { + close(fd); + return -1; + } + + close(fd); + + lockfile = strdup(path); + + return 0; +} + +// Tries to lock the first free device, returns 0 on success +int device_lock_first(char *appname, char *dev, size_t l) { + int i, found = 0; + + for (i = 2; i <= 20; i++) { + snprintf(dev, l, "/dev/ttyI%i", i); + + if (!device_lock(dev, appname)) { + found = 1; + break; + } + } + + if (found) + return 0; + else { + fprintf(stderr, "Could not find free device.\n"); + return -1; + } +} + +// Deletes the lockfile +void device_unlock(void) { + if (lockfile) { + unlink(lockfile); + free (lockfile); + lockfile = NULL; + } +} diff --git a/src/lock.h b/src/lock.h new file mode 100644 index 0000000..a1847c9 --- /dev/null +++ b/src/lock.h @@ -0,0 +1,28 @@ +#ifndef foolockhfoo +#define foolockhfoo + +/* $Id$ */ + +/* + * This file is part of ivcall. + * + * ivcall is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * ivcall is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ivcall; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +int device_lock(char *dev, char*appname); +int device_lock_first(char*appname, char *dev, size_t l); +void device_unlock(void); + +#endif diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..153a5c4 --- /dev/null +++ b/src/util.c @@ -0,0 +1,65 @@ +/* $Id$ */ + +/* + * This file is part of ivcall. + * + * ivcall is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * ivcall is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ivcall; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include +#include + +#include "util.h" + +ssize_t loop_read (int FILEDES, const void *BUFFER, size_t SIZE) { + int c = 0; + + while (SIZE > 0) { + int r; + + if ((r = read(FILEDES, (void*) BUFFER, SIZE)) <= 0) + break; + + SIZE -= r; + c += r; + BUFFER = ((void*) (((char*) BUFFER) + r)); + } + + return c; +} + +ssize_t loop_write(int FILEDES, const void *BUFFER, size_t SIZE) { + int c = 0; + + while (SIZE > 0) { + int r; + + if ((r = write(FILEDES, BUFFER, SIZE)) <= 0) + break; + + SIZE -= r; + c += r; + BUFFER = ((void*) (((char*) BUFFER) + r)); + } + + return c; +} + +// Returns a pointer to the filename in a path +char *basename(char *f) { + char *i = strrchr(f, '/'); + return i ? i+1 : f; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..1192ad4 --- /dev/null +++ b/src/util.h @@ -0,0 +1,28 @@ +#ifndef fooutilhfoo +#define fooutilhfoo + +/* $Id$ */ + +/* + * This file is part of ivcall. + * + * ivcall is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * ivcall is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ivcall; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +char *basename(char *f); +ssize_t loop_write(int FILEDES, const void *BUFFER, size_t SIZE); +ssize_t loop_read (int FILEDES, const void *BUFFER, size_t SIZE); + +#endif -- cgit