Hot answers tagged

17

Consider starting the child process in a sandbox. /usr/bin/perl is likely safe from a malicious user since /usr/bin is typically locked down by the root user(s). But /home/demetri/bin/awklikeperl.pl could be replaced if the owner is not careful with permissions. Putting the process in a sandbox will not only protect the rest of your system from attack, ...


13

No it is not safe: Classic off-by-one error. You reserve space for 3 additional entries with command[argc+3] but you add 4. argv[i+1] and command[i+4] will be out of bounds on the last iteration (last valid index is argv[argc-1] and command[argc+2] respectively) execv expects a NULL terminated array of NULL terminated strings. So the last entry in commands ...


9

Note that DateTimeOffset.ToUnixTimeSeconds and DateTimeOffset.FromUnixTimeSeconds exist as of .NET 4.6. Consider using those instead. If you really need a nullable, you can still wrap the framework methods in an extension method. Also, generally you should be using DateTimeOffset instead of DateTime unless you explicitly want to ignore timezones (e.g. ...


8

(This is in addition to what @200_success already said about exit codes and error handling.) Your main questions I'm pretty sure, here is lot of stuff, which I missed or did incorrectly (or not the "Python way"). The convention is to use CamelCase for class names. So MysqlUserDb would be better for your class. See PEP8 for more details. Don't mix ...


8

Unfortunately, as far as I know, there is no chunks methods in the standard library. But this makes things rather neat. from itertools import chain, islice def chunks(iterable, n): "chunks(ABCDE,2) => AB CD E" iterable = iter(iterable) while True: yield chain([next(iterable)], islice(iterable, n-1)) l = ... file_large = 'large_file.txt' ...


8

A few notes: clock_gettime() is not standard C, which means it's not guaranteed to be implemented on any system that supports C (including many Linux platforms, the few who implement it stuff it into time.h, but there are only a handful of standardized functions in that header). Relying on it for "portability" is not something you should do. There is no ...


8

It's not an error to ask for help When -h is passed, the help() function shouldn't exit with non-zero status, because it's done what was asked for. Conversely, when we pass no arguments, we should exit with non-zero, and we should write the message to the standard error stream. I'd write that as help() { local tab=$'\t' cat <<END $0 - run ...


7

Don't Repeat Yourself You start/stop/restart apache2 and mysql the same way, so you don't need to duplicate their code. You could create a generalized function that can handle these or any standard service well, like this: startstop_service() { cmd=$1 name=$2 sudo service $name $cmd } Naming You renamed the universal and intuitive command ...


7

grep is most likely an overkill. After setting IFS=":", the $PATH is conveniently split into words. Then the presence of the directory can be determined in a simple loop IFS=":" for pathdir in $PATH; do if [ $pathdir == $1]; then return; fi done # Now restore IFS and modify path as needed.


7

Style You seem to be adhering to PEP8, which is usually a good thing. Many of your functions have descriptive names, but your variables do not. p, s, x, d, w and c are terrible. k1 and k2 are also lousy index names, but since they're used only in a relatively small loop it's less problematic. I have no idea why they're called k1 and k2 while code should be ...


7

A couple of small things: If you're specifically looking for executable files, you might use which instead of whereis command. From this answer, you can see the difference between the two using whatis: $ whatis which which (1) - shows the full path of (shell) commands $ whatis whereis whereis (1) - locate the binary, source,...


7

Portability The code uses pthread which is a POSIX (Portable Operating System Interface) structure. If the code uses POSIX for one thing, it might be better to use POSIX for all things to remain POSIX compliant. This means that instead of using bzero((char*)&serv_add, sizeof(struct sockaddr_in)); to zero out the serv_add variable it might be better use ...


6

Whitespace is not a precious resource: your code is far too dense for my taste fewer semicolons, more newlines. try to limit your line length to 90 chars for readability You rely upon the presence of ~/Music, but never test that it exists. add mkdir -p "$HOME/Music" at the top Variable names: stick with one style, and don't make that style UPPER_CASE (...


6

Your fprintf() call can be written more simply using perror(): perror("Failed to exec")


6

The error handling seems to be problematic. By convention, a program should exit with a non-zero status to indicate that an error has occurred. By calling sys.exit(), you are indicating that the command completed successfully. Whatever process is calling your script will have a hard time determining whether the database operations succeeded or failed. ...


6

I have found a couple of things that could help you improve your code. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. In particular you should never use it in a header file. Use the appropriate headers Instead of using stdint.h you should instead use: #include <...


6

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE. You have problems with mktemp. -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1). On GNU/Linux: -t interpret TEMPLATE as a single file name component, relative to a ...


5

I don't have the time to do a completely thorough review at the moment, but here are some things that I noticed that may help you improve your code. Don't use #import Although it is supported by some compilers, code which is intended to be reused should avoid non-standard extensions. In this case, it's also not really needed because all of the files ...


5

I am not familiar enough with process control to find any of the problems you seek, so treat this as a general review, for what it is worth, starting from the top: I don't know what advantage your IGNORE_ERRORS macro has over a simple (void) cast for an ignored return value. I imagine you have a good reason for using _exit in child_startup_err rather than ...


5

I can't help you with GNU style, but here are some things that may help you improve your code. Close files when you are done with them The program will automatically close files when the program terminates, but it's good to close them as soon as you're done with them. This helps prevent the problem of running out of file handles. Simplify the ...


5

You could consider terminating with the exit codes from <sysexits.h>, especially if the client is expecting them. For instance, if fork() fails, return or call exit() with EX_OSERR. It may be helpful to make the 127 error code a macro with a relevant name, since it's probably not a very commonly-known error code, especially returned from main(). I ...


5

I didn't quite understand why I had to use a while loop with waitpid To loop or not to loop should be a well-thought design decision. The main reason for waiting in the loop is to monitor child's life for events other than exit (e.g. when the WUNTRACED option is set). Since you do not set this option, the only remaining reason is to deal with interrupts. ...


5

I think you need to rethink your assumptions. For starters it's not obvious at all that non-blocking would be faster than blocking. The big difference between your two programs is that the non-blocking one can handle multiple clients and your blocking one can't. So it's reasonable to suggest that the support might cost some performance. If you start multiple ...


5

Partial result ffmpeg -v warning -i video1.mkv -i video2.mkv -filter_complex '[0:v]scale=400:400,pad=800:400 [0:q]; [1:v]scale=400:400[1:q]; [0:q][1:q]overlay=400:0' -f nut -c:v rawvideo -c:a copy - | mplayer -noconsolecontrols -cache-min 1 -cache 1024000 - Dummy video input removed Filenames now separate command line aguments, not parts of a longer string ...


5

You don't round inputs before the epoch start correctly. You should round towards minus infinity, not towards the epoch start. When you add x seconds to a DateTime is should add x seconds to the unix time stamp. Switching between upwards and downwards rounding breaks that property. Obtaining the date from a DateTime should give the same result as obtaining ...


5

There's definitely an easier way of doing this. You can use os.path.realpath() to get the canonical path. os.path.realpath(path) Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system). Example: from os.path import realpath def simplify_path(...


5

If you're using Java 8, you can use StringJoiner. /** * JEcho writes any command line argument to the standard output; each argument * is separated by a single whitespace and end with a newline (you can * specify '-n' to suppress the newline). * * This program doesn't interpret common backslash-escaped characters (for * exampe '\n' or '\c'). */ ...


5

Use 'shellcheck' to spot common problems 200236.sh:2:12: warning: Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames. [SC2010] 200236.sh:2:18: note: Use ./*glob* or -- *glob* so names with dashes won't become options. [SC2035] 200236.sh:4:8: note: Don't use variables in the printf format string. Use printf "..%...


5

the posted code does not cleanly compile! Compile with warnings enabled, then fix those warnings. for gcc, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11 Note: other compilers use different options to produce the same results. in function: setup_myfile() there are statements like: return -1; However, that only gets execution ...


4

Some guidelines to follow: If possible, as it is in your case, work with absolute path. You'll have at the beginning to do more or less something like: dir = '/home/rik/' files = [os.path.join(dir,file) for file in os.listdir(dir)] But everything should be easier and straighforward later on. You need to move the default behaviour - files = os.listdir('.') ...


Only top voted, non community-wiki answers of a minimum length are eligible