Subsections

- Debugging
- Using a Debugger
- Memory Leaks
- Special Files
- When Implementing Incomplete Code
- Bacula Source File Structure
- Header Files
- Programming Standards
- Do Not Use
- Avoid if Possible
- Do Use Whenever Possible
- Indenting Standards
- Tabbing
- Don'ts
- Message Classes
- Debug Messages
- Error Messages
- Job Messages
- Queued Job Messages
- Memory Messages
- Bugs Database

#
Developing Bacula

Typically the simplest way to develop Bacula is to open one xterm window pointing to the source directory you wish to update; a second xterm window at the top source directory level, and a third xterm window at the bacula directory <top>/src/bacula. After making source changes in one of the directories, in the top source directory xterm, build the source, and start the daemons by entering:

make and

./startit then in the enter:

./console or

./gnome-console to start the Console program. Enter any commands for testing. For example: run kernsverify full.

Note, the instructions here to use ./startit are different from using a production system where the administrator starts Bacula by entering ./bacula start. This difference allows a development version of Bacula to be run on a computer at the same time that a production system is running. The ./startit strip starts Bacula using a different set of configuration files, and thus permits avoiding conflicts with any production system.

To make additional source changes, exit from the Console program, and in the top source directory, stop the daemons by entering:

./stopit then repeat the process.

##
Debugging

Probably the first thing to do is to turn on debug output.

A good place to start is with a debug level of 20 as in ./startit -d20. The startit command starts all the daemons with the same debug level. Alternatively, you can start the appropriate daemon with the debug level you want. If you really need more info, a debug level of 60 is not bad, and for just about everything a level of 200.

##
Using a Debugger

If you have a serious problem such as a segmentation fault, it can usually be found quickly using a good multiple thread debugger such as gdb. For example, suppose you get a segmentation violation in bacula-dir. You might use the following to find the problem:

<start the Storage and File daemons> cd dird gdb ./bacula-dir run -f -s -c ./dird.conf <it dies with a segmentation fault> where The -f option is specified on the run command to inhibit dird from going into the background. You may also want to add the -s option to the run command to disable signals which can potentially interfere with the debugging.

As an alternative to using the debugger, each Bacula daemon has a built in back trace feature when a serious error is encountered. It calls the debugger on itself, produces a back trace, and emails the report to the developer. For more details on this, please see the chapter in the main Bacula manual entitled ``What To Do When Bacula Crashes (Kaboom)''.

##
Memory Leaks

Because Bacula runs routinely and unattended on client and server machines, it may run for a long time. As a consequence, from the very beginning, Bacula uses SmartAlloc to ensure that there are no memory leaks. To make detection of memory leaks effective, all Bacula code that dynamically allocates memory MUST have a way to release it. In general when the memory is no longer needed, it should be immediately released, but in some cases, the memory will be held during the entire time that Bacula is executing. In that case, there MUST be a routine that can be called at termination time that releases the memory. In this way, we will be able to detect memory leaks. Be sure to immediately correct any and all memory leaks that are printed at the termination of the daemons.

##
Special Files

Kern uses files named 1, 2, ... 9 with any extension as scratch files. Thus any files with these names are subject to being rudely deleted at any time.

##
When Implementing Incomplete Code

Please identify all incomplete code with a comment that contains

    ***FIXME***

where there are three asterisks (*) before and after the word FIXME (in capitals) and no intervening spaces. This is important as it allows new programmers to easily recognize where things are partially implemented.

##
Bacula Source File Structure

The distribution generally comes as a tar file of the form bacula.x.y.z.tar.gz where x, y, and z are the version, release, and update numbers respectively.

Once you detar this file, you will have a directory structure as follows:

    |
    Tar file:
    |- depkgs
       |- mtx              (autochanger control program + tape drive info)
       |- sqlite           (SQLite database program)

    Tar file:
    |- depkgs-win32
       |- pthreads         (Native win32 pthreads library -- dll)
       |- zlib             (Native win32 zlib library)
       |- wx               (wxWidgets source code)

    Project bacula:
    |- bacula              (main source directory containing configuration
       |                    and installation files)
       |- autoconf         (automatic configuration files, not normally used
       |                    by users)
       |- intl             (programs used to translate)
       |- platforms        (OS specific installation files)
          |- redhat        (Red Hat installation)
          |- solaris       (Sun installation)
          |- freebsd       (FreeBSD installation)
          |- irix          (Irix installation -- not tested)
          |- unknown       (Default if system not identified)
       |- po               (translations of source strings)
       |- src              (source directory; contains global header files)
          |- cats          (SQL catalog database interface directory)
          |- console       (bacula user agent directory)
          |- dird          (Director daemon)
          |- filed         (Unix File daemon)
             |- win32      (Win32 files to make bacula-fd be a service)
          |- findlib       (Unix file find library for File daemon)
          |- gnome-console (GNOME version of console program)
          |- lib           (General Bacula library)
          |- stored        (Storage daemon)
          |- tconsole      (Tcl/tk console program -- not yet working)
          |- testprogs     (test programs -- normally only in Kern's tree)
          |- tools         (Various tool programs)
          |- win32         (Native Win32 File daemon)
             |- baculafd   (Visual Studio project file)
             |- compat     (compatibility interface library)
             |- filed      (links to src/filed)
             |- findlib    (links to src/findlib)
             |- lib        (links to src/lib)
             |- console    (beginning of native console program)
             |- wx-console (wxWidget console Win32 specific parts)
         |- wx-console     (wxWidgets console main source program)

    Project regress:
    |- regress             (Regression scripts)
       |- bin              (temporary directory to hold Bacula installed binaries)
       |- build            (temporary directory to hold Bacula source)
       |- scripts          (scripts and .conf files)
       |- tests            (test scripts)
       |- tmp              (temporary directory for temp files)
       |- working          (temporary working directory for Bacula daemons)

    Project docs:
    |- docs                (documentation directory)
       |- developers       (Developer's guide)
       |- home-page        (Bacula's home page source)
       |- manual           (html document directory)
       |- manual-fr        (French translation)
       |- manual-de        (German translation)
       |- techlogs         (Technical development notes);

    Project rescue:
    |- rescue              (Bacula rescue CDROM)
       |- linux            (Linux rescue CDROM)
          |- cdrom         (Linux rescue CDROM code)
            ...
       |- solaris          (Solaris rescue -- incomplete)
       |- freebsd          (FreeBSD rescue -- incomplete)

    Project gui:
    |- gui                 (Bacula GUI projects)
      |- bacula-web        (Bacula web php management code)
      |- bimagemgr         (Web application for burning CDROMs)

##
Header Files

Please carefully follow the scheme defined below as it permits in general only two header file includes per C file, and thus vastly simplifies programming. With a large complex project like Bacula, it isn't always easy to ensure that the right headers are invoked in the right order (there are a few kludges to make this happen -- i.e. in a few include files because of the chicken and egg problem, certain references to typedefs had to be replaced with void ).

Every file should include bacula.h. It pulls in just about everything, with very few exceptions. If you have system dependent ifdefing, please do it in baconfig.h. The version number and date are kept in version.h.

Each of the subdirectories (console, cats, dird, filed, findlib, lib, stored, ...) contains a single directory dependent include file generally the name of the directory, which should be included just after the include of bacula.h. This file (for example, for the dird directory, it is dird.h) contains either definitions of things generally needed in this directory, or it includes the appropriate header files. It always includes protos.h. See below.

Each subdirectory contains a header file named protos.h, which contains the prototypes for subroutines exported by files in that directory. protos.h is always included by the main directory dependent include file.

##
Programming Standards

For the most part, all code should be written in C unless there is a burning reason to use C++, and then only the simplest C++ constructs will be used. Note, Bacula is slowly evolving to use more and more C++.

Code should have some documentation -- not a lot, but enough so that I can understand it. Look at the current code, and you will see that I document more than most, but am definitely not a fanatic.

We prefer simple linear code where possible. Gotos are strongly discouraged except for handling an error to either bail out or to retry some code, and such use of gotos can vastly simplify the program.

Remember this is a C program that is migrating to a tiny subset of C++, so be conservative in your use of C++ features.

##
Do Not Use

- STL -- it is totally incomprehensible.

##
Avoid if Possible

- Using void * because this generally means that one must using casting, and in C++ casting is rather ugly. It is OK to use void * to pass structure address where the structure is not known to the routines accepting the packet (typically callback routines). However, declaring "void *buf" is a bad idea. Please use the correct types whenever possible.

- Using undefined storage specifications such as (short, int, long, long long, size_t ...). The problem with all these is that the number of bytes they allocate depends on the compiler and the system. Instead use Bacula's types (int8_t, uint8_t, int32_t, uint32_t, int64_t, and uint64_t). This guarantees that the variables are given exactly the size you want. Please try at all possible to avoid using size_t ssize_t and the such. They are very system dependent. However, some system routines may need them, so their use is often unavoidable.

- Returning a malloc'ed buffer from a subroutine -- someone will forget to release it.

- Heap allocation (malloc) unless needed -- it is expensive. Use POOL_MEM instead.

- Templates -- they can create portability problems.

- Fancy or tricky C or C++ code, unless you give a good explanation of why you used it.

- Too much inheritance -- it can complicate the code, and make reading it difficult (unless you are in love with colons)

##
Do Use Whenever Possible

- Locking and unlocking within a single subroutine.

- A single point of exit from all subroutines. A goto is perfectly OK to use to get out early, but only to a label named bail_out, and possibly an ok_out. See current code examples.

- Malloc and free within a single subroutine.

- Comments and global explanations on what your code or algorithm does.

##
Indenting Standards

We find it very hard to read code indented 8 columns at a time. Even 4 at a time uses a lot of space, so we have adopted indenting 3 spaces at every level. Note, indention is the visual appearance of the source on the page, while tabbing is replacing a series of up to 8 spaces from a tab character.

The closest set of parameters for the Linux indent program that will produce reasonably indented code are:

    -nbad -bap -bbo -nbc -br -brs -c36 -cd36 -ncdb -ce -ci3 -cli0
    -cp36 -d0 -di1 -ndj -nfc1 -nfca -hnl -i3 -ip0 -l85 -lp -npcs
    -nprs -npsl -saf -sai -saw -nsob -nss -nbc -ncs -nbfda

You can put the above in your .indent.pro file, and then just invoke indent on your file. However, be warned. This does not produce perfect indenting, and it will mess up C++ class statements pretty badly.

Braces are required in all if statements (missing in some very old code). To avoid generating too many lines, the first brace appears on the first line (e.g. of an if), and the closing brace is on a line by itself. E.g.

       if (abc) {
          some_code;
      }

Just follow the convention in the code. For example we I prefer non-indented cases.

       switch (code) {
       case 'A':
          do something
          break;
       case 'B':
          again();
          break;
       default:
          break;
      }

Avoid using // style comments except for temporary code or turning off debug code. Standard C comments are preferred (this also keeps the code closer to C).

Attempt to keep all lines less than 85 characters long so that the whole line of code is readable at one time. This is not a rigid requirement.

Always put a brief description at the top of any new file created describing what it does and including your name and the date it was first written. Please don't forget any Copyrights and acknowledgments if it isn't 100% your code. Also, include the Bacula copyright notice that is in src/c.

In general you should have two includes at the top of the an include for the particular directory the code is in, for includes are needed, but this should be rare.

In general (except for self-contained packages), prototypes should all be put in protos.h in each directory.

Always put space around assignment and comparison operators.

       a = 1;
       if (b >= 2) {
         cleanup();
      }

but your can compress things in a for statement:

       for (i=0; i < del.num_ids; i++) {
        ...

Don't overuse the inline if (?:). A full if is preferred, except in a print statement, e.g.:

       if (ua->verbose \&& del.num_del != 0) {
          bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
             del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
      }

Leave a certain amount of debug code (Dmsg) in code you submit, so that future problems can be identified. This is particularly true for complicated code likely to break. However, try to keep the debug code to a minimum to avoid bloating the program and above all to keep the code readable.

Please keep the same style in all new code you develop. If you include code previously written, you have the option of leaving it with the old indenting or re-indenting it. If the old code is indented with 8 spaces, then please re-indent it to Bacula standards.

If you are using vim, simply set your tabstop to 8 and your shiftwidth to 3.

##
Tabbing

Tabbing (inserting the tab character in place of spaces) is as normal on all Unix systems -- a tab is converted space up to the next column multiple of 8. My editor converts strings of spaces to tabs automatically -- this results in significant compression of the files. Thus, you can remove tabs by replacing them with spaces if you wish. Please don't confuse tabbing (use of tab characters) with indenting (visual alignment of the code).

##
Don'ts

Please don't use:

    strcpy()
    strcat()
    strncpy()
    strncat();
    sprintf()
    snprintf()

They are system dependent and un-safe. These should be replaced by the Bacula safe equivalents:

    char *bstrncpy(char *dest, char *source, int dest_size);
    char *bstrncat(char *dest, char *source, int dest_size);
    int bsnprintf(char *buf, int32_t buf_len, const char *fmt, ...);
    int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap);

See src/lib/bsys.c for more details on these routines.

Don't use the %lld or the %q printf format editing types to edit 64 bit integers -- they are not portable. Instead, use %s with edit_uint64(). For example:

       char buf[100];
       uint64_t num = something;
       char ed1[50];
       bsnprintf(buf, sizeof(buf), "Num=%s\n", edit_uint64(num, ed1));

Note: %lld is now permitted in Bacula code -- we have our own printf routines which handle it correctly. The edit_uint64() subroutine can still be used if you wish, but over time, most of that old style will be removed.

The edit buffer ed1 must be at least 27 bytes long to avoid overflow. See src/lib/edit.c for more details. If you look at the code, don't start screaming that I use lld. I actually use subtle trick taught to me by John Walker. The lld that appears in the editing routine is actually #define to a what is needed on your OS (usually ``lld'' or ``q'') and is defined in autoconf/configure.in for each OS. C string concatenation causes the appropriate string to be concatenated to the ``%''.

Also please don't use the STL or Templates or any complicated C++ code.

##
Message Classes

Currently, there are five classes of messages: Debug, Error, Job, Memory, and Queued.

##
Debug Messages

Debug messages are designed to be turned on at a specified debug level and are always sent to STDOUT. There are designed to only be used in the development debug process. They are coded as:

DmsgN(level, message, arg1, ...) where the N is a number indicating how many arguments are to be substituted into the message (i.e. it is a count of the number arguments you have in your message -- generally the number of percent signs (%)). level is the debug level at which you wish the message to be printed. message is the debug message to be printed, and arg1, ... are the arguments to be substituted. Since not all compilers support #defines with varargs, you must explicitly specify how many arguments you have.

When the debug message is printed, it will automatically be prefixed by the name of the daemon which is running, the filename where the Dmsg is, and the line number within the file.

Some actual examples are:

Dmsg2(20, ``MD5len=%d MD5=%s\n'', strlen(buf), buf);

Dmsg1(9, ``Created client %s record\n'', client->hdr.name);

##
Error Messages

Error messages are messages that are related to the daemon as a whole rather than a particular job. For example, an out of memory condition my generate an error message. They should be very rarely needed. In general, you should be using Job and Job Queued messages (Jmsg and Qmsg). They are coded as:

EmsgN(error-code, level, message, arg1, ...) As with debug messages, you must explicitly code the of arguments to be substituted in the message. error-code indicates the severity or class of error, and it may be one of the following:

M_ABORT | Causes the daemon to immediately abort. This should be used only in extreme cases. It attempts to produce a traceback.
M_ERROR_TERM | Causes the daemon to immediately terminate. This should be used only in extreme cases. It does not produce a traceback.
M_FATAL | Causes the daemon to terminate the current job, but the daemon keeps running
M_ERROR | Reports the error. The daemon and the job continue running
M_WARNING | Reports an warning message. The daemon and the job continue running
M_INFO | Reports an informational message.

There are other error message classes, but they are in a state of being redesigned or deprecated, so please do not use them. Some actual examples are:

Emsg1(M_ABORT, 0, ``Cannot create message thread: %s\n'', strerror(status));

Emsg3(M_WARNING, 0, ``Connect to File daemon %s at %s:%d failed. Retrying ...\n'', client->hdr.name, client->address, client->port);

Emsg3(M_FATAL, 0, ``bdird<filed: bad response from Filed to %s command: %d %s\n'', cmd, n, strerror(errno));

##
Job Messages

Job messages are messages that pertain to a particular job such as a file that could not be saved, or the number of files and bytes that were saved. They Are coded as:

    Jmsg(jcr, M\_FATAL, 0, "Text of message");

A Jmsg with M_FATAL will fail the job. The Jmsg() takes varargs so can have any number of arguments for substituted in a printf like format. Output from the Jmsg() will go to the Job report. <br> If the Jmsg is followed with a number such as Jmsg1(...), the number indicates the number of arguments to be substituted (varargs is not standard for #defines), and what is more important is that the file and line number will be prefixed to the message. This permits a sort of debug from user's output.

##
Queued Job Messages

Queued Job messages are similar to Jmsg()s except that the message is Queued rather than immediately dispatched. This is necessary within the network subroutines and in the message editing routines. This is to prevent recursive loops, and to ensure that messages can be delivered even in the event of a network error.

##
Memory Messages

Memory messages are messages that are edited into a memory buffer. Generally they are used in low level routines such as the low level device file dev.c in the Storage daemon or in the low level Catalog routines. These routines do not generally have access to the Job Control Record and so they return error essages reformatted in a memory buffer. Mmsg() is the way to do this.

##
Bugs Database

We have a bugs database which is at: http://bugs.bacula.org, and as a developer you will need to respond to bugs, perhaps bugs in general if you have time, otherwise just bugs that correspond to code that you wrote.

If you need to answer bugs, please be sure to ask the Project Manager (currently Kern) to give you Developer access to the bugs database. This allows you to modify statuses and close bugs.

The first thing is if you want to take over a bug, rather than just make a note, you should assign the bug to yourself. This helps other developers know that you are the principal person to deal with the bug. You can do so by going into the bug and clicking on the Update Issue button. Then you simply go to the Assigned To box and select your name from the drop down box. To actually update it you must click on the Update Information button a bit further down on the screen, but if you have other things to do such as add a Note, you might wait before clicking on the Update Information button.

Generally, we set the Status field to either acknowledged, confirmed, or feedback when we first start working on the bug. Feedback is set when we expect that the user should give us more information.

Normally, once you are reasonably sure that the bug is fixed, and a patch is made and attached to the bug report, and/or in the SVN, you can close the bug. If you want the user to test the patch, then leave the bug open, otherwise close it and set Resolution to Fixed. We generally close bug reports rather quickly, even without confirmation, especially if we have run tests and can see that for us the problem is fixed. However, in doing so, it avoids misunderstandings if you leave a note while you are closing the bug that says something to the following effect: We are closing this bug because ... If for some reason, it does not fix your problem, please feel free to reopen it, or to open a new bug report describing the problem".

We do not recommend that you attempt to edit any of the bug notes that have been submitted, nor to delete them or make them private. In fact, if someone accidentally makes a bug note private, you should ask the reason and if at all possible (with his agreement) make the bug note public.

If the user has not properly filled in most of the important fields (platorm, OS, Product Version, ...) please do not hesitate to politely ask him. Also, if the bug report is a request for a new feature, please politely send the user to the Feature Request menu item on www.bacula.org. The same applies to a support request (we answer only bugs), you might give the user a tip, but please politely refer him to the manual and the Getting Support page of www.bacula.org.